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