simplify subscribing/unsubscribing podcasts
authorStefan Koegl <stefan@skoegl.net>
Sat, 18 Sep 2010 16:10:38 +0000 (18 19:10 +0300)
committerStefan Koegl <stefan@skoegl.net>
Sun, 19 Sep 2010 06:06:07 +0000 (19 09:06 +0300)
mygpo/api/advanced/__init__.py
mygpo/api/legacy.py
mygpo/api/models/__init__.py
mygpo/api/simple.py
mygpo/web/views/podcast.py

index 32db661..9ba67fc 100644 (file)
@@ -115,14 +115,14 @@ def update_subscriptions(user, device, add, remove):
     for a in add_sanitized:
         p, p_created = Podcast.objects.get_or_create(url=a)
         try:
-            s = SubscriptionAction.objects.create(podcast=p,device=device,action=SUBSCRIBE_ACTION)
+            p.subscribe(device)
         except IntegrityError, e:
             log('can\'t add subscription %s for user %s: %s' % (a, user, e))
 
     for r in rem_sanitized:
         p, p_created = Podcast.objects.get_or_create(url=r)
         try:
-            s = SubscriptionAction.objects.create(podcast=p,device=device,action=UNSUBSCRIBE_ACTION)
+            p.unsubscribe(device)
         except IntegrityError, e:
             log('can\'t remove subscription %s for user %s: %s' % (r, user, e))
 
index 22a6ac7..9717f08 100644 (file)
@@ -18,7 +18,7 @@
 from django.http import HttpResponse
 from django.contrib.auth.models import User
 from mygpo.api.opml import Importer, Exporter
-from mygpo.api.models import Subscription, Podcast, SubscriptionAction, Device, SUBSCRIBE_ACTION, UNSUBSCRIBE_ACTION
+from mygpo.api.models import Subscription, Podcast, Device
 from datetime import datetime
 from django.utils.datastructures import MultiValueDictKeyError
 from django.db import IntegrityError
@@ -77,7 +77,7 @@ def upload(request):
             continue
 
         try:
-            SubscriptionAction.objects.create(podcast=p,action=SUBSCRIBE_ACTION, timestamp=datetime.now(), device=d)
+            p.subscribe(d)
         except IntegrityError, e:
             log('/upload: error while adding subscription: user: %s, podcast: %s, error: %s' % (user.id, p.id, e))
         except ValueError, ve:
@@ -86,7 +86,7 @@ def upload(request):
     for r in rem:
         p, created = Podcast.objects.get_or_create(url=r)
         try:
-            SubscriptionAction.objects.create(podcast=p, action=UNSUBSCRIBE_ACTION, timestamp=datetime.now(), device=d)
+            p.unsubscribe(d)
         except IntegrityError, e:
             log('/upload: error while removing subscription: user: %s, podcast: %s, error: %s' % (user.id, p.id, e))
 
index 3066a01..3698026 100644 (file)
@@ -23,7 +23,7 @@ from mygpo.api.fields import SeparatedValuesField, JSONField
 import hashlib
 import re
 
-from mygpo.api.constants import EPISODE_ACTION_TYPES, DEVICE_TYPES, SUBSCRIBE_ACTION, SUBSCRIPTION_ACTION_TYPES
+from mygpo.api.constants import EPISODE_ACTION_TYPES, DEVICE_TYPES, UNSUBSCRIBE_ACTION, SUBSCRIBE_ACTION, SUBSCRIPTION_ACTION_TYPES
 from mygpo.log import log
 
 class UserProfile(models.Model):
@@ -58,6 +58,21 @@ class Podcast(models.Model):
     group_member_name = models.CharField(max_length=20, default=None, null=True, blank=False)
     content_types = SeparatedValuesField(null=True, blank=True)
 
+
+    def subscribe(self, device):
+        """
+        Subscribe to the current Podcast on the given Device
+        """
+        SubscriptionAction.objects.create(podcast=self, action=SUBSCRIBE_ACTION, device=device)
+
+
+    def unsubscribe(self, device):
+        """
+        Unsubscribe the current Podcast from the given Device
+        """
+        SubscriptionAction.objects.create(podcast=self, action=UNSUBSCRIBE_ACTION, device=device)
+
+
     def subscriptions(self):
         """
         returns all public subscriptions to this podcast
index d396967..b75c971 100644 (file)
@@ -17,7 +17,7 @@
 
 from mygpo.api.basic_auth import require_valid_user, check_username
 from django.http import HttpResponse, HttpResponseBadRequest
-from mygpo.api.models import Device, SubscriptionAction, Podcast, SUBSCRIBE_ACTION, UNSUBSCRIBE_ACTION, SuggestionEntry
+from mygpo.api.models import Device, Podcast, SuggestionEntry
 from mygpo.api.opml import Exporter, Importer
 from mygpo.api.httpresponse import JsonResponse
 from mygpo.api.sanitizing import sanitize_url
@@ -154,12 +154,12 @@ def set_subscriptions(urls, user, device_uid):
 
     for r in rem:
         p = Podcast.objects.get(url=r)
-        s = SubscriptionAction(podcast=p, device=device, action=UNSUBSCRIBE_ACTION)
+        p.unsubscribe(device)
         s.save()
 
     for n in new:
         p, created = Podcast.objects.get_or_create(url=n)
-        s = SubscriptionAction(podcast=p, action=SUBSCRIBE_ACTION, device=device)
+        p.subscribe(device)
         s.save()
 
     # Only an empty response is a successful response
index 1077e5b..421a631 100644 (file)
@@ -8,7 +8,7 @@ from django.contrib.auth.decorators import login_required
 from django.contrib.sites.models import Site
 from django.utils.translation import ugettext as _
 
-from mygpo.api.models import Podcast, Episode, EpisodeAction, Device, SubscriptionAction, Subscription, SUBSCRIBE_ACTION, UNSUBSCRIBE_ACTION, SyncGroup
+from mygpo.api.models import Podcast, Episode, EpisodeAction, Device, SubscriptionAction, Subscription, SyncGroup
 from mygpo.web.forms import PrivacyForm, SyncForm
 from mygpo.data.models import Listener, PodcastTag
 from mygpo.decorators import manual_gc, allowed_methods
@@ -199,7 +199,7 @@ def subscribe(request, pid):
                 device = target
 
             try:
-                SubscriptionAction.objects.create(podcast=podcast, device=device, action=SUBSCRIBE_ACTION)
+                podcast.subscribe(device)
             except IntegrityError, e:
                 log('error while subscribing to podcast (device %s, podcast %s)' % (device.id, podcast.id))
 
@@ -233,7 +233,7 @@ def unsubscribe(request, pid, device_id):
     podcast = get_object_or_404(Podcast, pk=pid)
     device = Device.objects.get(pk=device_id)
     try:
-        SubscriptionAction.objects.create(podcast=podcast, device=device, action=UNSUBSCRIBE_ACTION, timestamp=datetime.now())
+        podcast.unsubscribe(device)
     except IntegrityError, e:
         log('error while unsubscribing from podcast (device %s, podcast %s)' % (device.id, podcast.id))