[PubSub] migrate to Django ORM
[mygpo.git] / mygpo / pubsub / models.py
blob8442345ef20cddbaab0b79a4952eba0aba5ebfe0
1 from __future__ import unicode_literals
3 from django.db import models
5 from couchdbkit.ext.django.schema import *
7 from mygpo.podcasts.models import Podcast
8 from mygpo.core.models import UpdateInfoModel
11 class SubscriptionError(Exception):
12 pass
14 class Subscription(Document):
15 url = StringProperty(required=True)
16 verify_token = StringProperty(required=True)
17 mode = StringProperty(required=True)
18 verified = BooleanProperty(default=False)
21 def __unicode__(self):
22 if self.verified:
23 verified = u'verified'
24 else:
25 verified = u'unverified'
26 return u'<Subscription for %s: %s>' % (self.url, verified)
28 def __str__(self):
29 return str(unicode(self))
32 class HubSubscription(UpdateInfoModel):
33 """ A client-side PubSubHubbub subscription
35 https://code.google.com/p/pubsubhubbub/ """
37 SUBSCRIBE = 'subscribe'
38 UNSUBSCRIBE = 'unsubscribe'
40 MODE_CHOICES = (
41 (SUBSCRIBE, 'subscribe'),
42 (UNSUBSCRIBE, 'unsubscribe'),
45 # podcast to which the subscription belongs
46 podcast = models.ForeignKey(Podcast, on_delete=models.PROTECT)
48 # the topic of the subscription, ie the URL that was subscribed at the hub
49 topic_url = models.CharField(max_length=2048, unique=True)
51 # the URL of the hub
52 hub_url = models.CharField(max_length=1000)
54 # a token to verify the authenticity of the hub
55 verify_token = models.CharField(max_length=32)
57 # the last mode that was requested, either subscribe or unsubscribe
58 mode = models.CharField(
59 choices=MODE_CHOICES,
60 max_length=max(map(len, [mode for mode, name in MODE_CHOICES])),
61 blank=True,
64 # indicates whether the last mode change has already been verified
65 verified = models.BooleanField(default=False)