Commits

Peter Sanchez committed f1e0be2

Updated views to match newer model changes

Comments (0)

Files changed (1)

djpubsubhubbub/views.py

 from django.views.decorators.csrf import csrf_exempt
 from django.utils.datastructures import MultiValueDictKeyError
 
-from djpubsubhubbub.models import Subscription
+from djpubsubhubbub.models import Subscription, DEFAULT_LEASE_SECONDS
 from djpubsubhubbub.signals import verified, updated
 
 
 @csrf_exempt
 def callback(request, pk):
-    def subscription_update(subscription, is_subscribed=False):
-        subscription.verified = True
-        subscription.is_subscribed = is_subscribed
-        subscription.set_expiration(int(lease_seconds))
-        subscription.save()
-        verified.send(sender=subscription)
-
     if request.method == 'GET':
         try:
             mode = request.GET['hub.mode']
             topic = request.GET['hub.topic']
             challenge = request.GET['hub.challenge']
-            lease_seconds = request.GET.get('hub.lease_seconds', 300)
+            lease_seconds = request.GET.get(
+                'hub.lease_seconds',
+                DEFAULT_LEASE_SECONDS,
+            )
             verify_token = request.GET.get('hub.verify_token', '')
         except MultiValueDictKeyError:
             # Raise 404 instead of 500 error
             if not verify_token.startswith('subscribe'):
                 raise Http404
 
-            subscription_update(subscription, True)
+            subscription.verified = True
+            subscription.is_subscribed = True
+            subscription.set_expiration(int(lease_seconds))
+            verified.send(sender=subscription)
         elif mode == 'unsubscribe':
             if not verify_token.startswith('unsubscribe'):
                 raise Http404
 
-            subscription_update(subscription)
+            subscription.verified = False
+            subscription.is_subscribed = False
+            subscription.set_expiration(int(lease_seconds))
 
         return HttpResponse(challenge, content_type='text/plain')
     elif request.method == 'POST':