MegaMark16 / django-cms-themes

A way to quickly load and swap out template packs (or themes) for Django CMS
BSD 3-Clause "New" or "Revised" License
49 stars 15 forks source link

Validation fails on setting page template #3

Closed black-puppydog closed 12 years ago

black-puppydog commented 12 years ago

I have installed the app into a pretty verbatim django-cms project. Database migration went smoothly. I can upload themes and configure my site to use one of them. Now when I add/edit a page I can select the theme's templates, but validation fails:

Value u'content.html' is not a valid option.

or something like that (I use the german translation).

What I think is happening (and I am still pretty much at the beginning of grasping the whole django thing, let alone django-cms and python debugging) is the following: Accoding to the debugger the template dirs should be set correctly but only after the cms.models.Pagemodel class is loaded and its template_choices attribute is set:

template_choices = [(x, _(y)) for x,y in settings.CMS_TEMPLATES]

When setting the property in the admin this code is never reached, so the old template_choices without the theme templates remains unchanged and validation fails.

I use: django 1.3.1 django_cms 2.2 django_cms_themes 1..0.8

black-puppydog commented 12 years ago

After some debugging I can with some certainty (mind that I'm still no expert python debugging guru) say that validation fails because the template_choices attribute is not updated when the templates app is loaded.

I tried just changing the template name in the database by hand which made the page render correctly. I can add and edit its content since on these actions no validation is done on the page object but only associations are added and the richtext elements I tried save without errors.

As I said, I'm still pretty new to django (I have been toying around with it for a while but only occasionally and for fun) so I have no clue how to fix this. One Idea would be to make a change in the django-cms app and reload the choices on saving the page (don't know if this is possible) which would effectively make this a cms issue. but since it only occurs with the templates app I still think it belongs here.

MegaMark16 commented 12 years ago

hey @bUbU87, you're correct about why this issue is occurring, and if you change your page templates through the front end toolbar instead of through the admin it works perfectly, just not in the admin. I've worked on it some but unfortunately I haven't been able to figure out how to monkey patch the admin change form to allow it to validate.

yakky commented 12 years ago

Ok, this is a really dirty hack, and i do think that's an horrible way to accomplish this, but unregistering page object from admin, patching the class and the reregister Page does the trick:

from cms.models import Page
from cms.admin.pageadmin import PageAdmin
admin.site.unregister(Page)
t = Page._meta.get_field_by_name("template")[0]
template_choices = [(x, _(y)) for x,y in settings.CMS_TEMPLATES]
t.choices.extend(template_choices)
admin.site.register(Page, PageAdmin)

I put this code in admin.py to avoid patching the page object in the "normal" execution flow

MegaMark16 commented 12 years ago

A recent pull request I merged in has fixed this issue. Thanks everyone who helped with the fix and with testing it!