Added admin form validation to prevent:
[philo.git] / admin.py
index 73dea0a..f63b576 100644 (file)
--- a/admin.py
+++ b/admin.py
@@ -8,6 +8,7 @@ from django.utils.safestring import mark_safe
 from django.utils.html import escape
 from django.utils.text import truncate_words
 from models import *
+from django.core.exceptions import ValidationError, ObjectDoesNotExist
 
 
 class AttributeInline(generic.GenericTabularInline):
@@ -94,6 +95,40 @@ class ModelLookupWidget(forms.TextInput):
                                pass
                return mark_safe(output)
 
+class NodeForm(forms.ModelForm):
+       
+       def __init__(self, *args, **kwargs):
+               super(NodeForm, self).__init__(*args, **kwargs)
+               instance = self.instance
+               try:
+                       self.fields['parent'].queryset = instance.node_ptr.__class__.objects.exclude(id=instance.id)
+               except ObjectDoesNotExist:
+                       pass
+       
+       def clean(self):
+               cleaned_data = self.cleaned_data
+               parent = self.cleaned_data['parent']
+               self.instance.validate_parents(parent)
+                       
+               try:
+                       Node.objects.get(slug=self.cleaned_data['slug'], parent=parent)
+                       raise ValidationError("A node with that path (parent and slug) already exists.")
+               except ObjectDoesNotExist:
+                       pass
+               
+               return cleaned_data
+
+class PageAdminForm(NodeForm):
+       class Meta:
+               model=Page
+
+class RedirectAdminForm(NodeForm):
+       class Meta:
+               model=Redirect
+               
+class FileAdminForm(NodeForm):
+       class Meta:
+               model=File
 
 class PageAdmin(EntityAdmin):
        prepopulated_fields = {'slug': ('title',)}
@@ -109,6 +144,7 @@ class PageAdmin(EntityAdmin):
        list_display = ('title', 'path', 'template')
        list_filter = ('template',)
        search_fields = ['title', 'slug', 'contentlets__content']
+       form = PageAdminForm
        
        def get_fieldsets(self, request, obj=None, **kwargs):
                fieldsets = list(self.fieldsets)
@@ -174,11 +210,16 @@ class PageAdmin(EntityAdmin):
                                        contentreference.save()
 
 class RedirectAdmin(admin.ModelAdmin):
-       list_display=('slug', 'target', 'status_code',)
+       list_display=('slug', 'target', 'path', 'status_code',)
        list_filter=('status_code',)
+       form = RedirectAdminForm
+
+class FileAdmin(admin.ModelAdmin):
+       form=FileAdminForm
+       list_display=('slug', 'mimetype', 'path', 'file',)
 
 admin.site.register(Collection, CollectionAdmin)
 admin.site.register(Redirect, RedirectAdmin)
-admin.site.register(File)
+admin.site.register(File, FileAdmin)
 admin.site.register(Page, PageAdmin)
 admin.site.register(Template, TemplateAdmin)