|
| 1 | +import logging |
| 2 | + |
| 3 | +from datetime import timedelta |
| 4 | + |
| 5 | +from django.conf import settings |
| 6 | +from django.core.management.base import NoArgsCommand |
| 7 | +from django.utils import timezone |
| 8 | +from django_push.subscriber.models import Subscription |
| 9 | + |
| 10 | +from ...models import Feed, APPROVED_FEED |
| 11 | + |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class Command(NoArgsCommand): |
| 17 | + def handle_noargs(self, **kwargs): |
| 18 | + feed_urls = set(Feed.objects.filter( |
| 19 | + is_defunct=False, approval_status=APPROVED_FEED |
| 20 | + ).values_list('feed_url', flat=True)) |
| 21 | + |
| 22 | + subscribed_urls = set(Subscription.objects.values_list('topic', |
| 23 | + flat=True)) |
| 24 | + |
| 25 | + missing_feeds = feed_urls - subscribed_urls |
| 26 | + extra_feeds = subscribed_urls - feed_urls |
| 27 | + |
| 28 | + for url in missing_feeds: |
| 29 | + logger.info(u'Subscribing to {0}'.format(url)) |
| 30 | + Subscription.objects.subscribe(url, settings.PUSH_HUB) |
| 31 | + |
| 32 | + for subscription in Subscription.objects.filter(topic__in=extra_feeds): |
| 33 | + logger.info(u'Unsubscribing from {0} ({1})'.format( |
| 34 | + subscription.pk, subscription.topic)) |
| 35 | + subscription.unsubscribe() |
| 36 | + |
| 37 | + limit = timezone.now() + timedelta(days=2) |
| 38 | + for subscription in Subscription.objects.exclude( |
| 39 | + topic__in=extra_feeds).filter(lease_expiration__lte=limit): |
| 40 | + logger.info(u'Renewing subscription for {0} ({1})'.format( |
| 41 | + subscription.topic, subscription.pk)) |
| 42 | + subscription.subscribe() |
0 commit comments