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