Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add a daily management command for subscribing / unsubscribing / rene…

…wing
  • Loading branch information...
commit 40750ec1dfb5daa44ccaffd8b52fe45347ed7792 1 parent 37efc95
@brutasse brutasse authored
View
42 aggregator/management/commands/update_subscriptions.py
@@ -0,0 +1,42 @@
+import logging
+
+from datetime import timedelta
+
+from django.conf import settings
+from django.core.management.base import NoArgsCommand
+from django.utils import timezone
+from django_push.subscriber.models import Subscription
+
+from ...models import Feed, APPROVED_FEED
+
+
+logger = logging.getLogger(__name__)
+
+
+class Command(NoArgsCommand):
+ def handle_noargs(self, **kwargs):
+ feed_urls = set(Feed.objects.filter(
+ is_defunct=False, approval_status=APPROVED_FEED
+ ).values_list('feed_url', flat=True))
+
+ subscribed_urls = set(Subscription.objects.values_list('topic',
+ flat=True))
+
+ missing_feeds = feed_urls - subscribed_urls
+ extra_feeds = subscribed_urls - feed_urls
+
+ for url in missing_feeds:
+ logger.info(u'Subscribing to {0}'.format(url))
+ Subscription.objects.subscribe(url, settings.PUSH_HUB)
+
+ for subscription in Subscription.objects.filter(topic__in=extra_feeds):
+ logger.info(u'Unsubscribing from {0} ({1})'.format(
+ subscription.pk, subscription.topic))
+ subscription.unsubscribe()
+
+ limit = timezone.now() + timedelta(days=2)
+ for subscription in Subscription.objects.exclude(
+ topic__in=extra_feeds).filter(lease_expiration__lte=limit):
+ logger.info(u'Renewing subscription for {0} ({1})'.format(
+ subscription.topic, subscription.pk))
+ subscription.subscribe()
View
2  deploy-requirements.txt
@@ -5,7 +5,7 @@ Django == 1.5.1
# django-contact-form doesn't have a release compatible with Django 1.2.
http://bitbucket.org/ubernostrum/django-contact-form/get/tip.bz2
django-haystack == 1.2.7
-django-push == 0.5
+django-push == 0.6
django-registration==1.0
django-secure == 1.0
docutils == 0.10

0 comments on commit 40750ec

Please sign in to comment.
Something went wrong with that request. Please try again.