replace Http404 with more appropriate methods
authorStefan Koegl <stefan@skoegl.net>
Thu, 16 Sep 2010 17:25:06 +0000 (16 20:25 +0300)
committerStefan Koegl <stefan@skoegl.net>
Thu, 16 Sep 2010 17:25:06 +0000 (16 20:25 +0300)
mygpo/api/advanced/__init__.py
mygpo/api/advanced/auth.py
mygpo/api/advanced/directory.py
mygpo/api/advanced/episode.py
mygpo/decorators.py
mygpo/web/views/device.py
mygpo/web/views/episode.py
mygpo/web/views/podcast.py
mygpo/web/views/public.py

index 902399d..58285e8 100644 (file)
@@ -16,7 +16,7 @@
 #
 
 from mygpo.api.basic_auth import require_valid_user, check_username
-from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden, Http404
+from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
 from mygpo.api.models import Device, Podcast, SubscriptionAction, Episode, EpisodeAction, SUBSCRIBE_ACTION, UNSUBSCRIBE_ACTION, EPISODE_ACTION_TYPES, DEVICE_TYPES, Subscription
 from mygpo.api.models.users import EpisodeFavorite
 from mygpo.api.httpresponse import JsonResponse
@@ -59,10 +59,7 @@ def subscriptions(request, username, device_uid):
     now_ = int(mktime(now.timetuple()))
 
     if request.method == 'GET':
-        try:
-            d = Device.objects.get(user=request.user, uid=device_uid, deleted=False)
-        except Device.DoesNotExist:
-            raise Http404('device %s does not exist' % device_uid)
+        d = get_object_or_404(Device, user=request.user, uid=device_uid, deleted=False)
 
         try:
             since_ = request.GET['since']
@@ -185,11 +182,8 @@ def episodes(request, username, version=1):
 
         since = datetime.fromtimestamp(float(since_)) if since_ else None
 
-        try:
-            podcast = Podcast.objects.get(url=podcast_url) if podcast_url else None
-            device  = Device.objects.get(user=request.user,uid=device_uid, deleted=False) if device_uid else None
-        except:
-            raise Http404
+        podcast = get_object_or_404(Podcast, url=podcast_url) if podcast_url else None
+        device  = get_object_or_404(Device, user=request.user,uid=device_uid, deleted=False) if device_uid else None
 
         return JsonResponse(get_episode_changes(request.user, podcast, device, since, now, aggregated, version))
 
index 1571133..ba3ab16 100644 (file)
@@ -18,7 +18,7 @@
 from django.contrib.auth.models import User
 from mygpo.api.basic_auth import require_valid_user, check_username
 from django.contrib import auth
-from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden, Http404
+from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
 from mygpo.api.httpresponse import JsonResponse
 from mygpo.web.models import SecurityToken
 from django.shortcuts import get_object_or_404
index ffe1b04..47a5c85 100644 (file)
@@ -16,7 +16,7 @@
 #
 
 from mygpo.api.basic_auth import require_valid_user, check_username
-from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden, Http404
+from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
 from mygpo.api.httpresponse import JsonResponse
 from mygpo.exceptions import ParameterMissing
 from django.shortcuts import get_object_or_404
index 9e03b32..32ab82b 100644 (file)
@@ -16,7 +16,7 @@
 #
 
 from mygpo.api.basic_auth import require_valid_user, check_username
-from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden, Http404
+from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
 from mygpo.api.httpresponse import JsonResponse
 from mygpo.exceptions import ParameterMissing
 from django.shortcuts import get_object_or_404
index d981880..3c9530e 100644 (file)
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #
 
-from django.shortcuts import render_to_response
+from django.shortcuts import render_to_response, get_object_or_404
 from django.template import RequestContext
 from mygpo.web.models import SecurityToken
 from django.contrib.auth.models import User
-from django.http import Http404, HttpResponseForbidden, HttpResponseNotAllowed
+from django.http import HttpResponseForbidden, HttpResponseNotAllowed
 import random
 import string
 import gc
@@ -44,11 +44,7 @@ def requires_token(object, action, denied_template=None):
     def decorator(fn):
         def tmp(request, username, *args, **kwargs):
 
-            try:
-                user = User.objects.get(username=username)
-            except User.DoesNotExist:
-                return lambda: Http404
-
+            user = get_object_or_404(User, username=username)
             token, c = SecurityToken.objects.get_or_create(user=user, object=object, action=action,
                         defaults = {'token': "".join(random.sample(string.letters+string.digits, 32))})
 
index 774e59d..58501ab 100644 (file)
@@ -16,7 +16,7 @@
 #
 
 from django.shortcuts import render_to_response
-from django.http import HttpResponseRedirect, HttpResponse, HttpResponseBadRequest, Http404, HttpResponseForbidden
+from django.http import HttpResponseRedirect, HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
 from django.contrib.auth.models import User
 from django.template import RequestContext
 from mygpo.api.models import Podcast, Episode, Device, EpisodeAction, SubscriptionAction, ToplistEntry, EpisodeToplistEntry, Subscription, SuggestionEntry, SyncGroup, SUBSCRIBE_ACTION, UNSUBSCRIBE_ACTION, SubscriptionMeta
index 4e5aa33..3158ea0 100644 (file)
@@ -16,7 +16,7 @@
 #
 
 from django.shortcuts import render_to_response
-from django.http import HttpResponseRedirect, HttpResponse, HttpResponseBadRequest, Http404, HttpResponseForbidden
+from django.http import HttpResponseRedirect, HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
 from django.template import RequestContext
 from mygpo.api.models import Podcast, Episode, Device, EpisodeAction, Subscription
 from mygpo.api.models.episodes import Chapter
index d310fb5..47f65b3 100644 (file)
@@ -1,6 +1,6 @@
 from datetime import date, timedelta, datetime
 
-from django.http import HttpResponseBadRequest, HttpResponseRedirect
+from django.http import HttpResponseBadRequest, HttpResponseRedirect, Http404
 from django.db import IntegrityError
 from django.shortcuts import get_object_or_404, render_to_response
 from django.template import RequestContext
@@ -228,9 +228,9 @@ def subscribe(request, pid):
 @login_required
 def unsubscribe(request, pid, device_id):
 
-    return_to = request.GET.get('return_to')
+    return_to = request.GET.get('return_to', None)
 
-    if return_to == None:
+    if not return_to:
         raise Http404('Wrong URL')
 
     podcast = get_object_or_404(Podcast, pk=pid)
@@ -246,9 +246,9 @@ def unsubscribe(request, pid, device_id):
 @manual_gc
 @login_required
 def subscribe_url(request):
-    url = request.GET.get('url')
+    url = request.GET.get('url', None)
 
-    if url == None:
+    if not url:
         raise Http404('http://my.gpodder.org/subscribe?url=http://www.example.com/podcast.xml')
 
     url = sanitize_url(url)
index ab31a07..970462b 100644 (file)
@@ -16,7 +16,7 @@
 #
 
 from django.shortcuts import render_to_response
-from django.http import HttpResponseRedirect, HttpResponse, HttpResponseBadRequest, Http404, HttpResponseForbidden
+from django.http import HttpResponseRedirect, HttpResponse, HttpResponseBadRequest, HttpResponseForbidden
 from django.template import RequestContext
 from mygpo.api.models import Podcast, Episode, Subscription
 from mygpo.api import backend