• Aucun résultat trouvé

Setting Up the Models

Dans le document Pro Django (Page 168-171)

฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀

and also for assigning credit to the proper authors when themes get promoted.

฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀

a security risk that would expose way too much information to just anyone.

฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀

everyone.

฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀

shouldn’t be able to make any changes.

฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀

others—should be used on all portions of the site.

฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀

to better style other aspects of the site.

That’s quite a list of things that need to be covered, and individual sites may have even more requirements. It’s not quite as bad as it may seem on the surface, as Django already has many things in place to make those problems easy to solve.

Setting Up the Models

The first order of business is to make a place for templates to be stored in the database. In standard Django fashion, this is done with a model, with fields for the various properties of the template. For this application, a theme consists of a few various pieces of information:

฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀

฀ ฀ ฀ ฀ ฀ ฀ ฀

฀ ฀ ฀ ฀ ฀ ฀

฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀

฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀ ฀

selected a theme still have one to use

Most of this information will only be used by the theme object itself, as only the main block of text will be passed in to the template. It’s easy to think of a theme as a template in its own right, where it’s simultaneously a set of data that gets stored in the database and a set of instructions that are used to render HTML. Python provides a way to make that notion explicit and offers a simple way to deal with themes.

By using multiple inheritance, it’s possible for a theme to be both a model and a template, behaving in whichever way is necessary for the task at hand. The class inherits from `f]jck*

`^*ik`aho*Ik`ah and `f]jck*pailh]pa*Pailh]pa, and [[ejep[[$% is overridden to initialize both sides separately:

bnki`f]jck*`^eilknpik`aho bnki`f]jckeilknppailh]pa

bnki`f]jck*_kjpne^*]qpd*ik`ahoeilknpQoan bnkipdaiao*i]j]canoeilknpPdaiaI]j]can _h]ooPdaia$ik`aho*Ik`ah(pailh]pa*Pailh]pa%6 A@EPEJC(LAJ@EJC(=LLNKRA@9n]jca$/%

OP=PQO[?DKE?AO9$

$A@EPEJC(q#A`epejc#%(

$LAJ@EJC(q#Laj`ejc=llnkr]h#%(

$=LLNKRA@(q#=llnkra`#%(

%

]qpdkn9ik`aho*BknaecjGau$Qoan%

pepha9ik`aho*?d]nBeah`$i]t[hajcpd9.11%

pailh]pa[opnejc9ik`aho*PatpBeah`$%

_oo9ik`aho*QNHBeah`$jqhh9Pnqa(^h]jg9Pnqa%

op]pqo9ik`aho*Oi]hhEjpacanBeah`$_dke_ao9OP=PQO[?DKE?AO(`ab]qhp9A@EPEJC%

eo[`ab]qhp9ik`aho*>kkha]jBeah`$%

k^fa_po9PdaiaI]j]can$%

`ab[[ejep[[$oahb(&]nco(&&gs]nco%6

oqlan$%skj#pskngdana(^a_]qoapdapsk[[ejep[[$%

iapdk`oecj]pqnao]__alp`ebbanajpoapokb]ncqiajpo ik`aho*Ik`ah*[[ejep[[$oahb(&]nco(&&gs]nco%

pailh]pa*Pailh]pa*[[ejep[[$oahb(oahb*pailh]pa[opnejc(

knecej9naln$oahb%(j]ia9qje_k`a$oahb%%

`abo]ra$oahb%6

eboahb*eo[`ab]qhp6

Oej_akjhukjapdaia_]j^apdaoepa)se`a`ab]qhp(]jujasik`ahpd]p eo`abeja`]o`ab]qhpiqopnaikrapda`ab]qhpoappejcbnki]jukpdan pdaia^abkna_kiieppejcpkpda`]p]^]oa*

oahb*k^fa_po*]hh$%*ql`]pa$eo[`ab]qhp9B]hoa%

oqlan$Pdaia(oahb%*o]ra$%

`ab[[qje_k`a[[$oahb%6 napqnjoahb*pepha

That’s enough to get the themes themselves stored in the database, but it still doesn’t cover how a user can select a theme to use while browsing the site. Ordinarily, that would be set up as a BknaecjGau on the model that references Pdaia, but since the Qoan model is outside our control, something else will need to be done.

Whenever an application needs to store user- centric information, such as preferences, the proper way to go about it is to add a user profile model. Django’s official documentation3 covers this in detail, but the basic idea is that a model can be declared to hold user profiles, allowing applications to use it easily.

A site can only have one user profile model, and it makes little sense to hijack that model solely for the purpose of supporting themes. An ideal situation would be to simply add a BknaecjGau to a general- purpose profile and reference that field on the profile model in the pdaiao application.

Note

This could be done automatically, by using the =QPD[LNKBEHA[IK@QHA setting to identify the pro-file model and add a new field using the model’s ]``[pk[_h]oo$% method. But since it only ever needs to be applied to one model and won’t change after being applied, it makes more sense to just add the field by hand.

Doing this right requires some extra effort beyond just making it work. Like any good Django application, this should be as generic and reusable as possible, so it’s important to keep assumptions to an absolute minimum. Even hard- coding things like the name of the field used to store a user’s theme can seriously impair a site’s ability to use themes.

The matter of the theme’s field name is one of the biggest issues facing the reusability of this application. The key to solving it is to specify the field name on a per- site basis, a job best suited for site- wide settings. Adding settings shouldn’t be a first choice for implementing new features, but this is an example where it just isn’t feasible any other way.

In order to avoid potential clashes with other applications that may also use their own settings, it’s important to always choose a name with a prefix that’s specific to the applica-tion. Since this is a theme application, and the setting is for specifying the field to be used for a user’s selected theme, we’ll call it PDAIA[LNKBEHA[BEAH@. It’s also important to provide defaults wherever possible. In this example, the name of the field is fairly arbitrary, but pdaia is as sensible as any, so we’ll use that.

Note

This isn’t the only available approach. We could also go with a I]juPkI]juBeah` that relates directly from Pdaia to Qoan, avoiding the issues with the profiles entirely. That would raise another issue to deal with, since the pdaiao application would have to make sure only one theme gets stored per user.

That’s not difficult on its own, but going with the profile adds one additional benefit: users can manage their preferred theme right alongside their other profile settings, using whatever application already manages their profiles.

Since this is a way to retrieve themes from the database, it’s best to place the code for it on a custom manager, rather than directly on the Pdaia model itself. There are actually two

3. dppl6++lnk`f]jck*_ki+qoan)lnkbeha+

different methods that would be useful here, both for getting themes based on the user. One is for retrieving a user’s selected theme, using the PDAIA[LNKBEHA[BEAH@ setting, while the other is for retrieving the themes a user has created.

bnki`f]jck*`^eilknpik`aho bnki`f]jck*_kjbeilknpoappejco _h]ooPdaiaI]j]can$ik`aho*I]j]can%6

`ab^u[]qpdkn$oahb(qoan%6

=_kjrajeaj_aiapdk`bknnapnearejcpdapdaiao]qoand]o]qpdkna`*

Oej_apdakjhupeiasa#hh^anapnearejcpdaiao^u]qpdkneosdaj pdau#na^aejca`epa`(pdeo]hokheiepopdamqanupkpdkoapdaiao pd]pd]raj#puap^aajoq^ieppa`bknnareas*

napqnjoahb*behpan$]qpdkn9oahb(op]pqo9oahb*ik`ah*A@EPEJC%

`abcap[_qnnajp[pdaia$oahb(qoan%6

beah`[j]ia9cap]ppn$oappejco(#PDAIA[LNKBEHA[BEAH@#(#pdaia#%

napqnjcap]ppn$qoan*cap[lnkbeha$%(beah`[j]ia%

With this manager in place, it’s easy to retrieve themes for a specific user, both those that user can edit, and the one that user should use when browsing the site. Having these shortcuts in place helps make views simpler, allowing them to focus on the business they really have to do. The whole point of a site- wide theme is that it’s used for every view, so clearly something else needs to be done to accommodate that.

Dans le document Pro Django (Page 168-171)