[Subscriptions] speed up get_subscribe_targets()
[mygpo.git] / mygpo / subscriptions / models.py
blob6b283054729f7ef88a1771d2ab298fee0fcf04a8
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
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'],
60 ['podcast', 'user'],
63 def __str__(self):
64 return '{user} subscribed to {podcast} on {client}'.format(
65 user=self.user, podcast=self.podcast, client=self.client)
68 SubscribedPodcast = collections.namedtuple('SubscribedPodcast',
69 'podcast public ref_url')