Fixing a double declaration of the monthname filter, and renaming ap_monthname to...
[philo.git] / admin / pages.py
1 from django.contrib import admin
2 from django import forms
3 from philo.admin import widgets
4 from philo.admin.base import COLLAPSE_CLASSES
5 from philo.admin.nodes import ViewAdmin
6 from philo.models.pages import Page, Template, Contentlet, ContentReference
7
8
9 class PageAdmin(ViewAdmin):
10         add_form_template = 'admin/philo/page/add_form.html'
11         fieldsets = (
12                 (None, {
13                         'fields': ('title', 'template')
14                 }),
15         )
16         list_display = ('title', 'template')
17         list_filter = ('template',)
18         search_fields = ['title', 'contentlets__content']
19         
20         def get_fieldsets(self, request, obj=None, **kwargs):
21                 fieldsets = list(self.fieldsets)
22                 if obj: # if no obj, creating a new page, thus no template set, thus no containers
23                         template = obj.template
24                         if template.documentation:
25                                 fieldsets.append(('Template Documentation', {
26                                         'description': template.documentation
27                                 }))
28                         contentlet_containers, contentreference_containers = template.containers
29                         for container_name in contentlet_containers:
30                                 fieldsets.append((('Container: %s' % container_name), {
31                                         'fields': (('contentlet_container_content_%s' % container_name), ('contentlet_container_dynamic_%s' % container_name))
32                                 }))
33                         for container_name, container_content_type in contentreference_containers:
34                                 fieldsets.append((('Container: %s' % container_name), {
35                                         'fields': (('contentreference_container_%s' % container_name),)
36                                 }))
37                 return fieldsets
38         
39         def get_form(self, request, obj=None, **kwargs):
40                 form = super(PageAdmin, self).get_form(request, obj, **kwargs)
41                 if obj: # if no obj, creating a new page, thus no template set, thus no containers
42                         page = obj
43                         template = page.template
44                         contentlet_containers, contentreference_containers = template.containers
45                         for container_name in contentlet_containers:
46                                 initial_content = None
47                                 initial_dynamic = False
48                                 try:
49                                         contentlet = page.contentlets.get(name__exact=container_name)
50                                         initial_content = contentlet.content
51                                         initial_dynamic = contentlet.dynamic
52                                 except Contentlet.DoesNotExist:
53                                         pass
54                                 form.base_fields[('contentlet_container_content_%s' % container_name)] = forms.CharField(label='Content', widget=forms.Textarea(), initial=initial_content, required=False)
55                                 form.base_fields[('contentlet_container_dynamic_%s' % container_name)] = forms.BooleanField(label='Dynamic', help_text='Specify whether this content contains dynamic template code', initial=initial_dynamic, required=False)
56                         for container_name, container_content_type in contentreference_containers:
57                                 initial_content = None
58                                 try:
59                                         initial_content = page.contentreferences.get(name__exact=container_name, content_type=container_content_type).content.pk
60                                 except (ContentReference.DoesNotExist, AttributeError):
61                                         pass
62                                 form.base_fields[('contentreference_container_%s' % container_name)] = forms.ModelChoiceField(label='References', widget=widgets.ModelLookupWidget(container_content_type), initial=initial_content, required=False, queryset=container_content_type.model_class().objects.all())
63                 return form
64         
65         def save_model(self, request, page, form, change):
66                 page.save()
67                 template = page.template
68                 contentlet_containers, contentreference_containers = template.containers
69                 for container_name in contentlet_containers:
70                         if (('contentlet_container_content_%s' % container_name) in form.cleaned_data) and (('contentlet_container_dynamic_%s' % container_name) in form.cleaned_data):
71                                 content = form.cleaned_data[('contentlet_container_content_%s' % container_name)]
72                                 dynamic = form.cleaned_data[('contentlet_container_dynamic_%s' % container_name)]
73                                 contentlet, created = page.contentlets.get_or_create(name=container_name, defaults={'content': content, 'dynamic': dynamic})
74                                 if not created:
75                                         contentlet.content = content
76                                         contentlet.dynamic = dynamic
77                                         contentlet.save()
78                 for container_name, container_content_type in contentreference_containers:
79                         if ('contentreference_container_%s' % container_name) in form.cleaned_data:
80                                 content = form.cleaned_data[('contentreference_container_%s' % container_name)]
81                                 try:
82                                         contentreference = page.contentreferences.get(name=container_name)
83                                 except ContentReference.DoesNotExist:
84                                         contentreference = ContentReference(name=container_name, page=page, content_type=container_content_type)
85                                 else:
86                                         if content == None:
87                                                 contentreference.delete()
88                                 
89                                 if content is not None:
90                                         contentreference.content_id = content.id
91                                         contentreference.save()
92
93
94 class TemplateAdmin(admin.ModelAdmin):
95         prepopulated_fields = {'slug': ('name',)}
96         fieldsets = (
97                 (None, {
98                         'fields': ('parent', 'name', 'slug')
99                 }),
100                 ('Documentation', {
101                         'classes': COLLAPSE_CLASSES,
102                         'fields': ('documentation',)
103                 }),
104                 (None, {
105                         'fields': ('code',)
106                 }),
107                 ('Advanced', {
108                         'classes': COLLAPSE_CLASSES,
109                         'fields': ('mimetype',)
110                 }),
111         )
112         save_on_top = True
113         save_as = True
114         list_display = ('__unicode__', 'slug', 'get_path',)
115
116
117 admin.site.register(Page, PageAdmin)
118 admin.site.register(Template, TemplateAdmin)