[Migration] handle Episode.listeners = None in episode toplist
[mygpo.git] / mygpo / podcastlists / models.py
blobb4e3731bc0b403eaf471da8e469305b6f592247c
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, UpdateInfoModel):
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 class Meta:
29 unique_together = [
30 # a slug is unique for a user
31 ('user', 'slug'),
34 @property
35 def max_order(self):
36 return max([-1] + [e.order for e in self.entries.all()])
38 @property
39 def num_entries(self):
40 return self.entries.count()
42 def add_entry(self, obj):
43 entry, created = PodcastListEntry.objects.get_or_create(
44 podcastlist=podcastlist,
45 content_type=ContentType.objects.get_for_model(obj),
46 object_id=obj.id,
47 defaults={
48 'order': self.max_order + 1,
52 def get_flattr_thing(self, domain, username):
53 """ Returns a "Thing" which can be flattred by other Flattr users """
54 return FlattrThing(
55 url = reverse('list-show', args=[username, self.slug]),
56 title = self.title,
57 description = u'A collection of podcasts about "%s" by %s user %s' % (self.title, domain, username),
58 category = u'audio',
59 hidden = None,
60 tags = None,
61 language = None,
66 class PodcastListEntry(UpdateInfoModel, OrderedModel):
67 """ An entry in a Podcast List """
69 # the list that the entry belongs to
70 podcastlist = models.ForeignKey(PodcastList,
71 related_name='entries',
72 on_delete=models.CASCADE,
75 # the object (Podcast or PodcastGroup) that is in the list
76 content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT)
77 object_id = UUIDField()
78 content_object = generic.GenericForeignKey('content_type', 'object_id')
80 class Meta:
81 unique_together = [
82 ('podcastlist', 'order'),
83 ('podcastlist', 'content_type', 'object_id'),