1 from django.db import models
2 from django.contrib.contenttypes.models import ContentType
3 from django.contrib.contenttypes import generic
4 from philo.models.base import value_content_type_limiter, register_value_model
5 from philo.utils import fattr
6 from django.template import add_to_builtins as register_templatetags
9 class Collection(models.Model):
10 name = models.CharField(max_length=255)
11 description = models.TextField(blank=True, null=True)
13 @fattr(short_description='Members')
15 return self.members.count()
17 def __unicode__(self):
24 class CollectionMemberManager(models.Manager):
25 use_for_related_fields = True
27 def with_model(self, model):
28 return model._default_manager.filter(pk__in=self.filter(member_content_type=ContentType.objects.get_for_model(model)).values_list('member_object_id', flat=True))
31 class CollectionMember(models.Model):
32 objects = CollectionMemberManager()
33 collection = models.ForeignKey(Collection, related_name='members')
34 index = models.PositiveIntegerField(verbose_name='Index', help_text='This will determine the ordering of the item within the collection. (Optional)', null=True, blank=True)
35 member_content_type = models.ForeignKey(ContentType, limit_choices_to=value_content_type_limiter, verbose_name='Member type')
36 member_object_id = models.PositiveIntegerField(verbose_name='Member ID')
37 member = generic.GenericForeignKey('member_content_type', 'member_object_id')
39 def __unicode__(self):
40 return u'%s - %s' % (self.collection, self.member)
46 register_templatetags('philo.templatetags.collections')
47 register_value_model(Collection)