Merge branch 'master' into advanced-containers
[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                         contentlet_containers, contentreference_containers = template.containers
86                         for container_name in contentlet_containers:
87                                 fieldsets.append((('Container: %s' % container_name), {
88                                         'fields': (('contentlet_container_content_%s' % container_name), ('contentlet_container_dynamic_%s' % container_name))
89                                 }))
90                         for container_name, container_content_type in contentreference_containers:
91                                 fieldsets.append((('Container: %s' % container_name), {
92                                         'fields': (('contentreference_container_%s' % container_name),)
93                                 }))
94                 return fieldsets
95         
96         def get_form(self, request, obj=None, **kwargs):
97                 form = super(PageAdmin, self).get_form(request, obj, **kwargs)
98                 if obj: # if no obj, creating a new page, thus no template set, thus no containers
99                         page = obj
100                         template = page.template
101                         contentlet_containers, contentreference_containers = template.containers
102                         for container_name in contentlet_containers:
103                                 initial_content = None
104                                 initial_dynamic = False
105                                 try:
106                                         contentlet = page.contentlets.get(name__exact=container_name)
107                                         initial_content = contentlet.content
108                                         initial_dynamic = contentlet.dynamic
109                                 except Contentlet.DoesNotExist:
110                                         pass
111                                 form.base_fields[('contentlet_container_content_%s' % container_name)] = forms.CharField(label='Content', widget=forms.Textarea(), initial=initial_content, required=False)
112                                 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)
113                         for container_name, container_content_type in contentreference_containers:
114                                 initial_content = None
115                                 try:
116                                         initial_content = page.contentreferences.get(name__exact=container_name, content_type=container_content_type)
117                                 except ContentReference.DoesNotExist:
118                                         pass
119                                 form.base_fields[('contentreference_container_%s' % container_name)] = forms.ModelChoiceField(label='References', initial=initial_content, required=False, queryset=container_content_type.model_class().objects.all())
120                 return form
121         
122         def save_model(self, request, page, form, change):
123                 page.save()
124                 template = page.template
125                 contentlet_containers, contentreference_containers = template.containers
126                 for container_name in contentlet_containers:
127                         if (('contentlet_container_content_%s' % container_name) in form.cleaned_data) and (('contentlet_container_dynamic_%s' % container_name) in form.cleaned_data):
128                                 content = form.cleaned_data[('contentlet_container_content_%s' % container_name)]
129                                 dynamic = form.cleaned_data[('contentlet_container_dynamic_%s' % container_name)]
130                                 contentlet, created = page.contentlets.get_or_create(name=container_name, defaults={'content': content, 'dynamic': dynamic})
131                                 if not created:
132                                         contentlet.content = content
133                                         contentlet.dynamic = dynamic
134                                         contentlet.save()
135                 for container_name, container_content_type in contentreference_containers:
136                         if ('contentreference_container_%s' % container_name) in form.cleaned_data:
137                                 content = form.cleaned_data[('contentreference_container_%s' % container_name)]
138                                 contentreference, created = page.contentreferences.get_or_create(name=container_name, defaults={'content': content})
139                                 if not created:
140                                         contentreference.content = content
141                                         contentreference.save()
142
143
144 admin.site.register(Collection, CollectionAdmin)
145 admin.site.register(Redirect)
146 admin.site.register(File)
147 admin.site.register(Page, PageAdmin)
148 admin.site.register(Template, TemplateAdmin)