class CollectionAdmin(admin.ModelAdmin):
inlines = [CollectionMemberInline]
+ list_display = ('name', 'description', 'get_count')
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):
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']
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
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):
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
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)
--- /dev/null
+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
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
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)
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: