[PodcastLists] migration to Django ORM
[mygpo.git] / mygpo / podcastlists / models.py
blob7836a6cbdbab17473e490949c82121d25f4820eb
1 from django.db import models
2 from django.core.urlresolvers import reverse
3 from django.conf import settings
4 from django.contrib.contenttypes.models import ContentType
5 from django.contrib.contenttypes import generic
7 from uuidfield import UUIDField
9 from mygpo.core.models import UpdateInfoModel, OrderedModel, UUIDModel
10 from mygpo.podcasts.models import Podcast
11 from mygpo.flattr import FlattrThing
12 from mygpo.votes.models import VoteMixin
15 class PodcastList(UUIDModel, VoteMixin):
16 """ A user-curated collection of podcasts """
18 # the user that created (and owns) the list
19 user = models.ForeignKey(settings.AUTH_USER_MODEL,
20 on_delete=models.CASCADE)
22 # the user-assigned title of the list
23 title = models.CharField(max_length=512)
25 # the slug (unique for the user) that is derived from the title
26 slug = models.SlugField(max_length=128)
28 # overwrites UpdateInfoModel.created/modified during the migration; this
29 # can be removed after the migration, to use the shadowed field with
30 # auto_add_now
31 created = models.DateTimeField()
32 modified = models.DateTimeField()
34 class Meta:
35 unique_together = [
36 # a slug is unique for a user
37 ('user', 'slug'),
40 @property
41 def max_order(self):
42 return max([-1] + [e.order for e in self.entries.all()])
44 @property
45 def num_entries(self):
46 return self.entries.count()
48 def add_entry(self, obj):
49 entry, created = PodcastListEntry.objects.get_or_create(
50 podcastlist=podcastlist,
51 content_type=ContentType.objects.get_for_model(obj),
52 object_id=obj.id,
53 defaults={
54 'order': self.max_order + 1,
58 def get_flattr_thing(self, domain, username):
59 """ Returns a "Thing" which can be flattred by other Flattr users """
60 return FlattrThing(
61 url = reverse('list-show', args=[username, self.slug]),
62 title = self.title,
63 description = u'A collection of podcasts about "%s" by %s user %s' % (self.title, domain, username),
64 category = u'audio',
65 hidden = None,
66 tags = None,
67 language = None,
72 class PodcastListEntry(UpdateInfoModel, OrderedModel):
73 """ An entry in a Podcast List """
75 # the list that the entry belongs to
76 podcastlist = models.ForeignKey(PodcastList,
77 related_name='entries',
78 on_delete=models.CASCADE,
81 # the object (Podcast or PodcastGroup) that is in the list
82 content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT)
83 object_id = UUIDField()
84 content_object = generic.GenericForeignKey('content_type', 'object_id')
86 class Meta:
87 unique_together = [
88 ('podcastlist', 'order'),
89 ('podcastlist', 'content_type', 'object_id'),