1 from __future__
import unicode_literals
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
21 def unsubscribe(self
, device
, podcast
):
22 # delete subscription, add history
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()
55 ['user', 'client', 'podcast'],
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')
77 if not cfg
.get_wksetting(PUBLIC_SUB_PODCAST
):
78 private
.append(cfg
.podcast
)
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
)
99 objects
= PodcastConfigmanager()
102 SubscribedPodcast
= collections
.namedtuple('SubscribedPodcast',
103 'podcast public ref_url')