From 51cb76593c02cb7165d2923f04a5a64a991b5868 Mon Sep 17 00:00:00 2001 From: Stefan Koegl Date: Sat, 18 Sep 2010 19:10:38 +0300 Subject: [PATCH] simplify subscribing/unsubscribing podcasts --- mygpo/api/advanced/__init__.py | 4 ++-- mygpo/api/legacy.py | 6 +++--- mygpo/api/models/__init__.py | 17 ++++++++++++++++- mygpo/api/simple.py | 6 +++--- mygpo/web/views/podcast.py | 6 +++--- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/mygpo/api/advanced/__init__.py b/mygpo/api/advanced/__init__.py index 32db6615..9ba67fc4 100644 --- a/mygpo/api/advanced/__init__.py +++ b/mygpo/api/advanced/__init__.py @@ -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)) diff --git a/mygpo/api/legacy.py b/mygpo/api/legacy.py index 22a6ac7c..9717f084 100644 --- a/mygpo/api/legacy.py +++ b/mygpo/api/legacy.py @@ -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)) diff --git a/mygpo/api/models/__init__.py b/mygpo/api/models/__init__.py index 3066a01d..36980260 100644 --- a/mygpo/api/models/__init__.py +++ b/mygpo/api/models/__init__.py @@ -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 diff --git a/mygpo/api/simple.py b/mygpo/api/simple.py index d396967d..b75c9711 100644 --- a/mygpo/api/simple.py +++ b/mygpo/api/simple.py @@ -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 diff --git a/mygpo/web/views/podcast.py b/mygpo/web/views/podcast.py index 1077e5bb..421a631f 100644 --- a/mygpo/web/views/podcast.py +++ b/mygpo/web/views/podcast.py @@ -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)) -- 2.11.4.GIT