Initial commit.
[philo.git] / admin.py
1 from django.contrib import admin
2 from django.contrib.contenttypes import generic
3 from django.contrib.contenttypes.models import ContentType
4 from django import forms
5 from models import *
6
7
8 class AttributeInline(generic.GenericTabularInline):
9         ct_field = 'entity_content_type'
10         ct_fk_field = 'entity_object_id'
11         model = Attribute
12         extra = 1
13         classes = ('collapse-closed',)
14         allow_add = True
15
16
17 class RelationshipInline(generic.GenericTabularInline):
18         ct_field = 'entity_content_type'
19         ct_fk_field = 'entity_object_id'
20         model = Relationship
21         extra = 1
22         classes = ('collapse-closed',)
23         allow_add = True
24
25
26 class EntityAdmin(admin.ModelAdmin):
27         inlines = [AttributeInline, RelationshipInline]
28         save_on_top = True
29
30
31 class CollectionMemberInline(admin.TabularInline):
32         fk_name = 'collection'
33         model = CollectionMember
34         extra = 1
35         classes = ('collapse-closed',)
36         allow_add = True
37
38
39 class CollectionAdmin(admin.ModelAdmin):
40         inlines = [CollectionMemberInline]
41
42
43 class TemplateAdmin(admin.ModelAdmin):
44         prepopulated_fields = {'slug': ('name',)}
45         fieldsets = (
46                 (None, {
47                         'fields': ('parent', 'name', 'slug')
48                 }),
49                 ('Documentation', {
50                         'classes': ('collapse', 'collapse-closed'),
51                         'fields': ('documentation',)
52                 }),
53                 (None, {
54                         'fields': ('code',)
55                 }),
56                 ('Advanced', {
57                         'classes': ('collapse','collapse-closed'),
58                         'fields': ('mimetype',)
59                 }),
60         )
61         save_on_top = True
62         save_as = True
63
64
65 class PageAdmin(EntityAdmin):
66         prepopulated_fields = {'slug': ('title',)}
67         fieldsets = (
68                 (None, {
69                         'fields': ('title', 'template')
70                 }),
71                 ('URL/Tree/Hierarchy', {
72                         'classes': ('collapse', 'collapse-closed'),
73                         'fields': ('parent', 'slug')
74                 }),
75         )
76         list_display = ('title', 'path', 'template')
77         list_filter = ('template',)
78         search_fields = ['title', 'slug', 'contentlets__content']
79         
80         def get_fieldsets(self, request, obj=None, **kwargs):
81                 fieldsets = list(self.fieldsets)
82                 if obj: # if no obj, creating a new page, thus no template set, thus no containers
83                         page = obj
84                         template = page.template
85                         containers = template.containers
86                         if len(containers) > 0:
87                                 for container in containers:
88                                         fieldsets.append((('Container: %s' % container), {
89                                                 'fields': (('container_content_%s' % container), ('container_dynamic_%s' % container))
90                                         }))
91                 return fieldsets
92         
93         def get_form(self, request, obj=None, **kwargs):
94                 form = super(PageAdmin, self).get_form(request, obj, **kwargs)
95                 if obj: # if no obj, creating a new page, thus no template set, thus no containers
96                         page = obj
97                         template = page.template
98                         containers = template.containers
99                         for container in containers:
100                                 initial_content = None
101                                 initial_dynamic = False
102                                 try:
103                                         contentlet = page.contentlets.get(name__exact=container)
104                                         initial_content = contentlet.content
105                                         initial_dynamic = contentlet.dynamic
106                                 except Contentlet.DoesNotExist:
107                                         pass
108                                 form.base_fields[('container_content_%s' % container)] = forms.CharField(label='Content', widget=forms.Textarea(), initial=initial_content, required=False)
109                                 form.base_fields[('container_dynamic_%s' % container)] = forms.BooleanField(label='Dynamic', help_text='Specify whether this content contains dynamic template code', initial=initial_dynamic, required=False)
110                 return form
111         
112         def save_model(self, request, page, form, change):
113                 page.save()
114                 
115                 template = page.template
116                 containers = template.containers
117                 for container in containers:
118                         if (("container_content_%s" % container) in form.cleaned_data) and (("container_dynamic_%s" % container) in form.cleaned_data):
119                                 content = form.cleaned_data[('container_content_%s' % container)]
120                                 dynamic = form.cleaned_data[('container_dynamic_%s' % container)]
121                                 contentlet, created = page.contentlets.get_or_create(name=container, defaults={'content': content, 'dynamic': dynamic})
122                                 if not created:
123                                         contentlet.content = content
124                                         contentlet.dynamic = dynamic
125                                         contentlet.save()
126
127
128 admin.site.register(Collection, CollectionAdmin)
129 admin.site.register(Page, PageAdmin)
130 admin.site.register(Template, TemplateAdmin)