[Requirements] update all requirements to latest versions
[mygpo.git] / mygpo / subscriptions / models.py
blob3b4a2150321346b18bf16f23bf0a96a0d6a5b011
1 from __future__ import unicode_literals
3 import collections
5 from django.db import models
6 from django.conf import settings
8 from mygpo.core.models import UpdateInfoModel, DeleteableModel, SettingsModel
9 from mygpo.users.models import Client
10 from mygpo.users.settings import PUBLIC_SUB_PODCAST
11 from mygpo.podcasts.models import Podcast
14 class SubscriptionManager(models.Manager):
15 """ Manages subscriptions """
17 def subscribe(self, device, podcast):
18 # create subscription, add history
19 pass
21 def unsubscribe(self, device, podcast):
22 # delete subscription, add history
23 pass
26 class Subscription(DeleteableModel):
27 """ A subscription to a podcast on a specific client """
29 # the user that subscribed to a podcast
30 user = models.ForeignKey(settings.AUTH_USER_MODEL, db_index=True,
31 on_delete=models.CASCADE)
33 # the client on which the user subscribed to the podcast
34 client = models.ForeignKey(Client, db_index=True,
35 on_delete=models.CASCADE)
37 # the podcast to which the user subscribed to
38 podcast = models.ForeignKey(Podcast, db_index=True,
39 on_delete=models.PROTECT)
41 # the URL that the user subscribed to; a podcast might have multiple URLs,
42 # the we want to return the users the ones they know
43 ref_url = models.URLField(max_length=2048)
45 # the following fields do not use auto_now[_add] for the time of the
46 # migration, in order to store historically accurate data; once the
47 # migration is complete, this model should inherit from UpdateInfoModel
48 created = models.DateTimeField()
49 modified = models.DateTimeField()
51 objects = SubscriptionManager()
53 class Meta:
54 unique_together = [
55 ['user', 'client', 'podcast'],
58 index_together = [
59 ['user', 'client']
62 def __str__(self):
63 return '{user} subscribed to {podcast} on {client}'.format(
64 user=self.user, podcast=self.podcast, client=self.client)
67 class PodcastConfigmanager(models.Manager):
68 """ Manager for PodcastConfig objects """
70 def get_private_podcasts(self, user):
71 """ Returns the podcasts that the user has marked as private """
72 configs = PodcastConfig.objects.filter(user=user)\
73 .select_related('podcast')
74 private = []
76 for cfg in configs:
77 if not cfg.get_wksetting(PUBLIC_SUB_PODCAST):
78 private.append(cfg.podcast)
80 return private
83 class PodcastConfig(SettingsModel, UpdateInfoModel):
84 """ Settings for a podcast, independant of a device / subscription """
86 # the user for which the config is stored
87 user = models.ForeignKey(settings.AUTH_USER_MODEL, db_index=True,
88 on_delete=models.CASCADE)
90 # the podcast for which the config is stored
91 podcast = models.ForeignKey(Podcast, db_index=True,
92 on_delete=models.PROTECT)
94 class Meta:
95 unique_together = [
96 ['user', 'podcast'],
99 objects = PodcastConfigmanager()
102 SubscribedPodcast = collections.namedtuple('SubscribedPodcast',
103 'podcast public ref_url')