74e3d076c460dacc798cb1660cc64dc45c338b5c
[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 django.conf import settings
6 from django.utils.translation import ugettext as _
7 from django.utils.safestring import mark_safe
8 from django.utils.html import escape
9 from django.utils.text import truncate_words
10 from models import *
11
12
13 class AttributeInline(generic.GenericTabularInline):
14         ct_field = 'entity_content_type'
15         ct_fk_field = 'entity_object_id'
16         model = Attribute
17         extra = 1
18         classes = ('collapse-closed',)
19         allow_add = True
20
21
22 class RelationshipInline(generic.GenericTabularInline):
23         ct_field = 'entity_content_type'
24         ct_fk_field = 'entity_object_id'
25         model = Relationship
26         extra = 1
27         classes = ('collapse-closed',)
28         allow_add = True
29
30
31 class EntityAdmin(admin.ModelAdmin):
32         inlines = [AttributeInline, RelationshipInline]
33         save_on_top = True
34
35
36 class CollectionMemberInline(admin.TabularInline):
37         fk_name = 'collection'
38         model = CollectionMember
39         extra = 1
40         classes = ('collapse-closed',)
41         allow_add = True
42
43
44 class CollectionAdmin(admin.ModelAdmin):
45         inlines = [CollectionMemberInline]
46
47
48 class NodeAdmin(EntityAdmin):
49         pass
50
51
52 class ModelLookupWidget(forms.TextInput):
53         # is_hidden = False
54         
55         def __init__(self, content_type, attrs=None):
56                 self.content_type = content_type
57                 super(ModelLookupWidget, self).__init__(attrs)
58         
59         def render(self, name, value, attrs=None):
60                 related_url = '../../../%s/%s/' % (self.content_type.app_label, self.content_type.model)
61                 if attrs is None:
62                         attrs = {}
63                 if not attrs.has_key('class'):
64                         attrs['class'] = 'vForeignKeyRawIdAdminField'
65                 output = super(ModelLookupWidget, self).render(name, value, attrs)
66                 output += '<a href="%s" class="related-lookup" id="lookup_id_%s" onclick="return showRelatedObjectLookupPopup(this);">' % (related_url, name)
67                 output += '<img src="%simg/admin/selector-search.gif" width="16" height="16" alt="%s" />' % (settings.ADMIN_MEDIA_PREFIX, _('Lookup'))
68                 output += '</a>'
69                 if value:
70                         value_class = self.content_type.model_class()
71                         try:
72                                 value_object = value_class.objects.get(pk=value)
73                                 output += '&nbsp;<strong>%s</strong>' % escape(truncate_words(value_object, 14))
74                         except value_class.DoesNotExist:
75                                 pass
76                 return mark_safe(output)
77
78
79 class RedirectAdmin(NodeAdmin):
80         fieldsets = (
81                 (None, {
82                         'fields': ('slug', 'target', 'status_code')
83                 }),
84                 ('URL/Tree/Hierarchy', {
85                         'classes': ('collapse', 'collapse-closed'),
86                         'fields': ('parent',)
87                 }),
88         )
89
90
91 class FileAdmin(NodeAdmin):
92         prepopulated_fields = {'slug': ('file',)}
93         fieldsets = (
94                 (None, {
95                         'fields': ('file', 'slug', 'mimetype')
96                 }),
97                 ('URL/Tree/Hierarchy', {
98                         'classes': ('collapse', 'collapse-closed'),
99                         'fields': ('parent',)
100                 }),
101         )
102
103
104 class PageAdmin(NodeAdmin):
105         prepopulated_fields = {'slug': ('title',)}
106         fieldsets = (
107                 (None, {
108                         'fields': ('title', 'slug', 'template')
109                 }),
110                 ('URL/Tree/Hierarchy', {
111                         'classes': ('collapse', 'collapse-closed'),
112                         'fields': ('parent',)
113                 }),
114         )
115         list_display = ('title', 'path', 'template')
116         list_filter = ('template',)
117         search_fields = ['title', 'slug', 'contentlets__content']
118         
119         def get_fieldsets(self, request, obj=None, **kwargs):
120                 fieldsets = list(self.fieldsets)
121                 if obj: # if no obj, creating a new page, thus no template set, thus no containers
122                         page = obj
123                         template = page.template
124                         contentlet_containers, contentreference_containers = template.containers
125                         for container_name in contentlet_containers:
126                                 fieldsets.append((('Container: %s' % container_name), {
127                                         'fields': (('contentlet_container_content_%s' % container_name), ('contentlet_container_dynamic_%s' % container_name))
128                                 }))
129                         for container_name, container_content_type in contentreference_containers:
130                                 fieldsets.append((('Container: %s' % container_name), {
131                                         'fields': (('contentreference_container_%s' % container_name),)
132                                 }))
133                 return fieldsets
134         
135         def get_form(self, request, obj=None, **kwargs):
136                 form = super(PageAdmin, self).get_form(request, obj, **kwargs)
137                 if obj: # if no obj, creating a new page, thus no template set, thus no containers
138                         page = obj
139                         template = page.template
140                         contentlet_containers, contentreference_containers = template.containers
141                         for container_name in contentlet_containers:
142                                 initial_content = None
143                                 initial_dynamic = False
144                                 try:
145                                         contentlet = page.contentlets.get(name__exact=container_name)
146                                         initial_content = contentlet.content
147                                         initial_dynamic = contentlet.dynamic
148                                 except Contentlet.DoesNotExist:
149                                         pass
150                                 form.base_fields[('contentlet_container_content_%s' % container_name)] = forms.CharField(label='Content', widget=forms.Textarea(), initial=initial_content, required=False)
151                                 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)
152                         for container_name, container_content_type in contentreference_containers:
153                                 initial_content = None
154                                 try:
155                                         initial_content = page.contentreferences.get(name__exact=container_name, content_type=container_content_type).content.pk
156                                 except ContentReference.DoesNotExist:
157                                         pass
158                                 form.base_fields[('contentreference_container_%s' % container_name)] = forms.ModelChoiceField(label='References', widget=ModelLookupWidget(container_content_type), initial=initial_content, required=False, queryset=container_content_type.model_class().objects.all())
159                 return form
160         
161         def save_model(self, request, page, form, change):
162                 page.save()
163                 template = page.template
164                 contentlet_containers, contentreference_containers = template.containers
165                 for container_name in contentlet_containers:
166                         if (('contentlet_container_content_%s' % container_name) in form.cleaned_data) and (('contentlet_container_dynamic_%s' % container_name) in form.cleaned_data):
167                                 content = form.cleaned_data[('contentlet_container_content_%s' % container_name)]
168                                 dynamic = form.cleaned_data[('contentlet_container_dynamic_%s' % container_name)]
169                                 contentlet, created = page.contentlets.get_or_create(name=container_name, defaults={'content': content, 'dynamic': dynamic})
170                                 if not created:
171                                         contentlet.content = content
172                                         contentlet.dynamic = dynamic
173                                         contentlet.save()
174                 for container_name, container_content_type in contentreference_containers:
175                         if ('contentreference_container_%s' % container_name) in form.cleaned_data:
176                                 content = form.cleaned_data[('contentreference_container_%s' % container_name)]
177                                 contentreference, created = page.contentreferences.get_or_create(name=container_name, defaults={'content': content})
178                                 if not created:
179                                         contentreference.content = content
180                                         contentreference.save()
181
182
183 class TemplateAdmin(admin.ModelAdmin):
184         prepopulated_fields = {'slug': ('name',)}
185         fieldsets = (
186                 (None, {
187                         'fields': ('parent', 'name', 'slug')
188                 }),
189                 ('Documentation', {
190                         'classes': ('collapse', 'collapse-closed'),
191                         'fields': ('documentation',)
192                 }),
193                 (None, {
194                         'fields': ('code',)
195                 }),
196                 ('Advanced', {
197                         'classes': ('collapse','collapse-closed'),
198                         'fields': ('mimetype',)
199                 }),
200         )
201         save_on_top = True
202         save_as = True
203
204
205 admin.site.register(Collection, CollectionAdmin)
206 admin.site.register(Redirect, RedirectAdmin)
207 admin.site.register(File, FileAdmin)
208 admin.site.register(Page, PageAdmin)
209 admin.site.register(Template, TemplateAdmin)