8. Usare la programmazione ad oggetti su un sito web

Una della caratteristiche più potenti di CherryPy è che potete realmente usare un approccio object oriented per "programmare" il vostro sito web.

Quando guardiamo un sito web complesso, realizziamo che alcune parti hanno cose in comune:

In entrambi i casi, OOP provvede una soluzione elegante al problema e minimizza il codice richiesto per implementare la soluzione.

Per mostrarci come è possibile tutto questo, creeremo un sito web che ha due versioni: una in Inglese ed una in Francese. Non cambia solo il testo, ma anche i colori ed il modo di visualizzare i moduli.

Digitiamo il seguente codice:

#######################
CherryClass Airline abstract:
#######################
function:
    def localize(self, stri):
        return self.dictionnary.get(stri, stri)
mask:
    def header(self):
        <html><body>
            <center>
                <H1 py-eval="self.localize('Welcome to CherryPy airline')"></H1>
                <div py-if="self==airlineFrench">
                    <a py-attr="request.base+'/airlineEnglish/index'" href="">
                        Click here for English version
                    </a>
                </div><div py-else>
                    <a py-attr="request.base+'/airlineFrench/index'" href="">
                        Cliquez ici pour la version française
                    </a>
                </div>
                <br><br><br><br>
    def footer(self):
            </center>
        </body></html>
    def squareWithText(self, title, text):
        <table border=0 cellspacing=0 cellpadding=1 width=200><tr>
            <td py-attr="self.borderColor" bgColor="">
                <table border=0 cellspacing=0 cellpadding=5><tr>
                    <td py-attr="self.insideColor" bgColor=""
                        align=center width=198 py-eval="'<b>%s</b><br><br>%s'
                                %(title,text)">
                    </td>
                </tr></table>
            </td>
        </tr></table>
view:
    def bookAFlight(self):
        page=self.header()
        page+=self.squareWithText(self.localize('Booking a flight'),
            self.localize('To book a flight, think about where you want to go, and you should dream about it tonight'))
        page+=self.footer()
        return page


#######################
CherryClass AirlineFrench(Airline):
#######################
variable:
    insideColor='#FFFF99'
    borderColor='#FF6666'
    dictionnary={
        'Welcome to CherryPy airline': 'Bienvenue chez CherryPy airline',
        'Booking a flight': 'Réserver un vol',
        'To book a flight, think about where you want to go, and you should 
        dream about it tonight':
            'Pour réserver un vol, pensez très fort à la destination, et vous 
            devriez en rêver cette nuit'
    }    
view:
    def index(self):
        page=self.header()
        page+=self.squareWithText('Réserver un vol', 'Pour réserver un vol, 
        cliquez sur <a href="%s/bookAFlight">réserver</a>'%self.getPath())+'<br>'
        page+=self.squareWithText('Présentation', 'CherryPy airline est la compagnie
        qui vous emmène au 7ème ciel')
        page+=self.footer()
        return page

#######################
CherryClass AirlineEnglish(Airline):
#######################
variable:
    insideColor='#00CCFF'
    borderColor='#3333FF'
    dictionnary={}    
view:
    def index(self):
        page=self.header()
        page+=self.squareWithText('Presentation', 'CherryPy airline is the company 
        that will take you to cloud 9')+'<br>'
        page+=self.squareWithText('Book a flight', '<a href="%s/bookAFlight">Click here</a> 
        to book a flight'%self.getPath())
        page+=self.footer()
        return page

#######################
CherryClass Root(AirlineEnglish):
#######################

Questo programma usa alcune caratteristiche di CherryPy. Proviamo a capire come funzionano:

L'idea è di usare una classe CherryClass generica (Airline) che contiene funzioni, mask e view che sono comuni ad entrambe le versioni (Inglese e Francese) o il sito web. Quindi usiamo 2 classi CherryClasses (AirlineFrench e AirlineEnglish) per implemetare azioni speifiche per ogni versione.

Abbiamo usato due modi differenti per implementare ogni versione:

Questo esempio mostra anche alcune nuove caratteristiche di CherryPy:

Nel prossimo capitolo, impareremo come dividere il nostro codice in diversi file sorgente...

See About this document... for information on suggesting changes.