Slight changes to penfield NewsletterIssueAdmin & added newsletter value models
[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
7
8 class Collection(models.Model):
9         name = models.CharField(max_length=255)
10         description = models.TextField(blank=True, null=True)
11         
12         @fattr(short_description='Members')
13         def get_count(self):
14                 return self.members.count()
15         
16         def __unicode__(self):
17                 return self.name
18         
19         class Meta:
20                 app_label = 'philo'
21
22
23 class CollectionMemberManager(models.Manager):
24         use_for_related_fields = True
25
26         def with_model(self, model):
27                 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))
28
29
30 class CollectionMember(models.Model):
31         objects = CollectionMemberManager()
32         collection = models.ForeignKey(Collection, related_name='members')
33         index = models.PositiveIntegerField(verbose_name='Index', help_text='This will determine the ordering of the item within the collection. (Optional)', null=True, blank=True)
34         member_content_type = models.ForeignKey(ContentType, limit_choices_to=value_content_type_limiter, verbose_name='Member type')
35         member_object_id = models.PositiveIntegerField(verbose_name='Member ID')
36         member = generic.GenericForeignKey('member_content_type', 'member_object_id')
37         
38         def __unicode__(self):
39                 return u'%s - %s' % (self.collection, self.member)
40         
41         class Meta:
42                 app_label = 'philo'
43
44
45 register_value_model(Collection)