Last random entry before more organized approach. Added documentation/admin display...
authormelinath <stephen.r.burrows@gmail.com>
Mon, 14 Jun 2010 19:30:43 +0000 (15:30 -0400)
committermelinath <stephen.r.burrows@gmail.com>
Mon, 14 Jun 2010 19:38:32 +0000 (15:38 -0400)
admin.py
models.py
templatetags/collections.py [new file with mode: 0644]
templatetags/containers.py

index af97bde..506267d 100644 (file)
--- a/admin.py
+++ b/admin.py
@@ -45,6 +45,7 @@ class CollectionMemberInline(admin.TabularInline):
 
 class CollectionAdmin(admin.ModelAdmin):
        inlines = [CollectionMemberInline]
+       list_display = ('name', 'description', 'get_count')
 
 
 class TreeForm(forms.ModelForm):
@@ -52,10 +53,13 @@ class TreeForm(forms.ModelForm):
                super(TreeForm, self).__init__(*args, **kwargs)
                instance = self.instance
                instance_class=self.get_instance_class()
-               try:
-                       self.fields['parent'].queryset = instance_class.objects.exclude(id=instance.id)
-               except ObjectDoesNotExist:
-                       pass
+               
+               if instance_class is not None:
+                       try:
+                               self.fields['parent'].queryset = instance_class.objects.exclude(id=instance.id)
+                       except ObjectDoesNotExist:
+                               pass
+                       
                self.fields['parent'].validators = [TreeParentValidator(*self.get_validator_args())]
        
        def get_instance_class(self):
@@ -132,7 +136,7 @@ class ModelLookupWidget(forms.TextInput):
 
 class NodeForm(TreeForm):
        def get_instance_class(self):
-               return self.instance.node_ptr.__class__
+               return Node
                
        def get_validator_args(self):
                return [self.instance, 'instance']
index d3bf5d4..c62d7eb 100644 (file)
--- a/models.py
+++ b/models.py
@@ -99,12 +99,18 @@ class Entity(models.Model):
        
        class Meta:
                abstract = True
-
+       
 
 class Collection(models.Model):
        name = models.CharField(max_length=255)
        description = models.TextField(blank=True, null=True)
-
+       
+       def get_count(self):
+               return self.members.count()
+       get_count.short_description = 'Members'
+       
+       def __unicode__(self):
+               return self.name
 
 class CollectionMemberManager(models.Manager):
        use_for_related_fields = True
@@ -120,6 +126,9 @@ class CollectionMember(models.Model):
        member_content_type = models.ForeignKey(ContentType, verbose_name='Member type')
        member_object_id = models.PositiveIntegerField(verbose_name='Member ID')
        member = generic.GenericForeignKey('member_content_type', 'member_object_id')
+       
+       def __unicode__(self):
+               return '%s - %s' % (self.collection, self.member)
 
 
 class TreeManager(models.Manager):
@@ -289,7 +298,7 @@ class File(Node):
 class Template(TreeModel):
        name = models.CharField(max_length=255)
        documentation = models.TextField(null=True, blank=True)
-       mimetype = models.CharField(max_length=255, null=True, blank=True, help_text='Default: %s' % settings.DEFAULT_CONTENT_TYPE)
+       mimetype = models.CharField(max_length=255, null=True, blank=True, help_text='Default: %s' % settings.DEFAULT_CONTENT_TYPE, default=settings.DEFAULT_CONTENT_TYPE)
        code = models.TextField(verbose_name='django template code')
        
        @property
@@ -359,7 +368,7 @@ class Template(TreeModel):
 
 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 a page - something which is rendered according to a template. 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)
diff --git a/templatetags/collections.py b/templatetags/collections.py
new file mode 100644 (file)
index 0000000..ed8c54e
--- /dev/null
@@ -0,0 +1,43 @@
+from django import template
+from django.conf import settings
+
+
+register = template.Library()
+
+
+class MembersofNode(template.Node):
+       def __init__(self, collection, model, as_var):
+               self.collection = template.Variable(collection)
+               self.model = template.Variable(model)
+               self.as_var = as_var
+               
+       def render(self, context):
+               try:
+                       collection = self.collection.resolve(context)
+                       model = self.model.resolve(context)
+                       context[self.as_var] = collection.members.with_model(model)
+               except:
+                       pass
+               return settings.TEMPLATE_STRING_IF_INVALID
+       
+       
+def do_membersof(parser, token):
+       """
+       {% membersof <collection> with <model> as <var> %}
+       """
+       params=token.split_contents()
+       tag = params[0]
+       
+       if len(params) < 6:
+               raise template.TemplateSyntaxError('"%s" template tag requires six parameters' % tag)
+               
+       if params[2] != 'with':
+               raise template.TemplateSyntaxError('"%s" template tag requires the third parameter to be "with"' % tag)
+               
+       if params[4] != 'as':
+               raise template.TemplateSyntaxError('"%s" template tag requires the fifth parameter to be "as"' % tag)
+       
+       return MembersofNode(collection=params[1], model=params[3], as_var=params[5])
+
+
+register.tag('membersof', do_membersof)
\ No newline at end of file
index ca5e1e9..2e40e53 100644 (file)
@@ -29,7 +29,7 @@ class ContainerNode(template.Node):
        def render(self, context):
                content = settings.TEMPLATE_STRING_IF_INVALID
                if 'page' in context:
-                       container_content = self.get_container_content(context['page'])
+                       container_content = self.get_container_content(context)
                
                if self.nodelist_main is None:
                        self.nodelist_main
@@ -56,7 +56,8 @@ class ContainerNode(template.Node):
                
                return ''
        
-       def get_container_content(self, page):
+       def get_container_content(self, context):
+               page = context['page']
                if self.references:
                        try:
                                contentreference = page.contentreferences.get(name__exact=self.name, content_type=self.references)
@@ -71,7 +72,7 @@ class ContainerNode(template.Node):
                                                content = mark_safe(template.Template(contentlet.content, name=contentlet.name).render(context))
                                        except template.TemplateSyntaxError, error:
                                                if settings.DEBUG:
-                                                       content = ('[Error parsing contentlet \'%s\': %s]' % self.name, error)
+                                                       content = ('[Error parsing contentlet \'%s\': %s]' % (self.name, error))
                                else:
                                        content = contentlet.content
                        except ObjectDoesNotExist: