|
@@ -1,12 +1,17 @@
|
|
|
# -*- coding: utf-8 -*-
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
+import datetime
|
|
|
+import json
|
|
|
+
|
|
|
+from django.db.models import Q, Count
|
|
|
from django.views.generic.base import RedirectView
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
from django.core.urlresolvers import reverse
|
|
|
+from django.http import JsonResponse, HttpResponseServerError
|
|
|
+# from django.views.decorators.cache import cache_control
|
|
|
|
|
|
-from coin.offers.models import OfferSubscription
|
|
|
-
|
|
|
+from coin.offers.models import Offer, OfferSubscription
|
|
|
|
|
|
class ConfigurationRedirectView(RedirectView):
|
|
|
"""Redirects to the appropriate view for the configuration backend of the
|
|
@@ -19,3 +24,31 @@ class ConfigurationRedirectView(RedirectView):
|
|
|
member=self.request.user)
|
|
|
return reverse(subscription.configuration.url_namespace + ':' + subscription.configuration.backend_name,
|
|
|
args=[subscription.configuration.pk])
|
|
|
+
|
|
|
+
|
|
|
+# @cache_control(max_age=7200)
|
|
|
+def subscription_count_json(request):
|
|
|
+ output = {}
|
|
|
+
|
|
|
+ # Get date form url, or set default
|
|
|
+ date = request.GET.get('date', datetime.date.today())
|
|
|
+
|
|
|
+ # Validate date type
|
|
|
+ if type(date) is not datetime.date:
|
|
|
+ try:
|
|
|
+ datetime.datetime.strptime(date, '%Y-%m-%d')
|
|
|
+ except ValueError, TypeError:
|
|
|
+ return HttpResponseServerError("Incorrect date format, should be YYYY-MM-DD")
|
|
|
+
|
|
|
+ # Count offer subscription
|
|
|
+ offers = Offer.objects\
|
|
|
+ .filter(Q(offersubscription__subscription_date__lte=date) & (Q(offersubscription__resign_date__gt=date) | Q(offersubscription__resign_date__isnull=True)))\
|
|
|
+ .annotate(num_subscribtions=Count('offersubscription'))\
|
|
|
+ .order_by('name')
|
|
|
+
|
|
|
+ # Print count by offer type
|
|
|
+ for offer in offers:
|
|
|
+ output[offer.name] = offer.num_subscribtions
|
|
|
+
|
|
|
+ return JsonResponse(output)
|
|
|
+
|