Run 2to3-3.4
[mygpo.git] / mygpo / podcastlists / models.py
blob8d87a88ec86a15d89b66c1272f948635cd200823
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
13 from mygpo.utils import set_ordered_entries
16 class PodcastList(UUIDModel, VoteMixin, UpdateInfoModel):
17 """ A user-curated collection of podcasts """
19 # the user that created (and owns) the list
20 user = models.ForeignKey(settings.AUTH_USER_MODEL,
21 on_delete=models.CASCADE)
23 # the user-assigned title of the list
24 title = models.CharField(max_length=512)
26 # the slug (unique for the user) that is derived from the title
27 slug = models.SlugField(max_length=128)
29 class Meta:
30 unique_together = [
31 # a slug is unique for a user
32 ('user', 'slug'),
35 @property
36 def max_order(self):
37 return max([-1] + [e.order for e in self.entries.all()])
39 @property
40 def num_entries(self):
41 return self.entries.count()
43 def add_entry(self, obj):
44 entry, created = PodcastListEntry.objects.get_or_create(
45 podcastlist=self,
46 content_type=ContentType.objects.get_for_model(obj),
47 object_id=obj.id,
48 defaults={
49 'order': self.max_order + 1,
53 def get_flattr_thing(self, domain, username):
54 """ Returns a "Thing" which can be flattred by other Flattr users """
55 return FlattrThing(
56 url = reverse('list-show', args=[username, self.slug]),
57 title = self.title,
58 description = 'A collection of podcasts about "%s" by %s user %s' % (self.title, domain, username),
59 category = 'audio',
60 hidden = None,
61 tags = None,
62 language = None,
65 def set_entries(self, podcasts):
66 """ Updates the list to include the given podcast, removes others """
68 existing = {e.content_object: e for e in self.entries.all()}
69 set_ordered_entries(self, podcasts, existing, PodcastListEntry,
70 'content_object', 'podcastlist')
73 class PodcastListEntry(UpdateInfoModel, OrderedModel):
74 """ An entry in a Podcast List """
76 # the list that the entry belongs to
77 podcastlist = models.ForeignKey(PodcastList,
78 related_name='entries',
79 on_delete=models.CASCADE,
82 # the object (Podcast or PodcastGroup) that is in the list
83 content_type = models.ForeignKey(ContentType, on_delete=models.PROTECT)
84 object_id = UUIDField()
85 content_object = generic.GenericForeignKey('content_type', 'object_id')
87 class Meta(OrderedModel.Meta):
88 unique_together = [
89 ('podcastlist', 'order'),
90 ('podcastlist', 'content_type', 'object_id'),