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):
)
save_on_top = True
save_as = True
+ list_display = ('__unicode__', 'slug', 'get_path',)
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',)}
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)
contentreference.content = content
contentreference.save()
+class RedirectAdmin(admin.ModelAdmin):
+ 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)
-admin.site.register(File)
+admin.site.register(Redirect, RedirectAdmin)
+admin.site.register(File, FileAdmin)
admin.site.register(Page, PageAdmin)
admin.site.register(Template, TemplateAdmin)
from django.template import Template as DjangoTemplate
from django.template import TemplateDoesNotExist
from django.template import Context, RequestContext
-from django.core.exceptions import ObjectDoesNotExist
+from django.core.exceptions import ObjectDoesNotExist, ValidationError
try:
import json
except ImportError:
return (obj, remainder)
raise self.model.DoesNotExist('%s matching query does not exist.' % self.model._meta.object_name)
-
class TreeModel(models.Model):
objects = TreeManager()
parent = models.ForeignKey('self', related_name='children', null=True, blank=True)
path = getattr(self, field, '?')
parent = self.parent
while parent:
+ self.validate_parent(parent)
path = getattr(parent, field, '?') + pathsep + path
parent = parent.parent
return path
class Meta:
abstract = True
+
+ def validate_parents(self, parent=None):
+ if parent == None:
+ parent = self.parent
+
+ while parent:
+ try:
+ self.validate_parent(parent)
+ parent = parent.parent
+ except ObjectDoesNotExist:
+ return # because it likely means the child doesn't exist
+
+ def validate_parent(self, parent):
+ #Why doesn't this stop the Admin site from saving a model with itself as parent?
+ if self == parent:
+ raise ValidationError("A %s can't be its own parent." % self.__class__.__name__)
+
+ def clean(self):
+ super(TreeModel, self).clean()
+ self.validate_parents()
+
+ def save(self, *args, **kwargs):
+ self.clean()
+ super(TreeModel, self).save(*args, **kwargs)
class TreeEntity(TreeModel, Entity):
@property
def instance(self):
return self.instance_type.get_object_for_this_type(id=self.id)
+
+ def validate_parent(self, parent):
+ if self.instance == parent.instance:
+ raise ValidationError("A %s can't be its own parent." % self.__class__.__name__)
def get_path(self, pathsep='/', field='slug'):
path = getattr(self.instance, field, '?')
def render_to_response(self, request, path=None, subpath=None):
return HttpResponseServerError()
+
+ class Meta:
+ unique_together=(('parent', 'slug',),)
class MultiNode(Node):
class Page(Node):
"""
- Represents an HTML page. The page will have a number of related Contentlets
- depending on the template selected - but these will appear only after the
- page has been saved with that template.
+ Represents an HTML page. The page will have a number of related Contentlets depending on the template selected - but these will appear only after the page has been saved with that template.
"""
template = models.ForeignKey(Template, related_name='pages')
title = models.CharField(max_length=255)