Merge pull request #793 from gpodder/remove-advertise
[mygpo.git] / mygpo / podcastlists / models.py
blob979cd09b673bd589e952877be0a54fd82373f8b4
1 from django.db import models
2 from django.urls import reverse
3 from django.conf import settings
4 from django.contrib.contenttypes.models import ContentType
5 from django.contrib.contenttypes.fields import GenericForeignKey
7 from mygpo.core.models import UpdateInfoModel, OrderedModel, UUIDModel
8 from mygpo.podcasts.models import Podcast
9 from mygpo.votes.models import VoteMixin
10 from mygpo.utils import set_ordered_entries
13 class PodcastList(UUIDModel, VoteMixin, UpdateInfoModel):
14 """A user-curated collection of podcasts"""
16 # the user that created (and owns) the list
17 user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
19 # the user-assigned title of the list
20 title = models.CharField(max_length=512)
22 # the slug (unique for the user) that is derived from the title
23 slug = models.SlugField(max_length=128)
25 class Meta:
26 unique_together = [
27 # a slug is unique for a user
28 ("user", "slug")
31 @property
32 def max_order(self):
33 return max([-1] + [e.order for e in self.entries.all()])
35 @property
36 def num_entries(self):
37 return self.entries.count()
39 def add_entry(self, obj):
40 entry, created = PodcastListEntry.objects.get_or_create(
41 podcastlist=self,
42 content_type=ContentType.objects.get_for_model(obj),
43 object_id=obj.id,
44 defaults={"order": self.max_order + 1},
47 def set_entries(self, podcasts):
48 """Updates the list to include the given podcast, removes others"""
50 existing = {e.content_object: e for e in self.entries.all()}
51 set_ordered_entries(
52 self, podcasts, existing, PodcastListEntry, "content_object", "podcastlist"
56 class PodcastListEntry(UpdateInfoModel, OrderedModel):
57 """An entry in a Podcast List"""
59 # the list that the entry belongs to
60 podcastlist = models.ForeignKey(
61 PodcastList, related_name="entries", on_delete=models.CASCADE
64 # the object (Podcast or PodcastGroup) that is in the list
65 content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
66 object_id = models.UUIDField()
67 content_object = GenericForeignKey("content_type", "object_id")
69 class Meta(OrderedModel.Meta):
70 unique_together = [
71 ("podcastlist", "order"),
72 ("podcastlist", "content_type", "object_id"),