Updates to gilbert. Reorganizing imports and removing support for python < 2.5.4.
[philo.git] / models / collections.py
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
7
8
9 class Collection(models.Model):
10         name = models.CharField(max_length=255)
11         description = models.TextField(blank=True, null=True)
12         
13         @fattr(short_description='Members')
14         def get_count(self):
15                 return self.members.count()
16         
17         def __unicode__(self):
18                 return self.name
19         
20         class Meta:
21                 app_label = 'philo'
22
23
24 class CollectionMemberManager(models.Manager):
25         use_for_related_fields = True
26
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))
29
30
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')
38         
39         def __unicode__(self):
40                 return u'%s - %s' % (self.collection, self.member)
41         
42         class Meta:
43                 app_label = 'philo'
44
45
46 register_templatetags('philo.templatetags.collections')
47 register_value_model(Collection)