views.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import datetime
  4. import json
  5. from django.db.models import Q, Count
  6. from django.views.generic.base import RedirectView
  7. from django.shortcuts import get_object_or_404
  8. from django.core.urlresolvers import reverse
  9. from django.http import JsonResponse, HttpResponseServerError
  10. # from django.views.decorators.cache import cache_control
  11. from coin.offers.models import Offer, OfferSubscription
  12. class ConfigurationRedirectView(RedirectView):
  13. """Redirects to the appropriate view for the configuration backend of the
  14. specified subscription."""
  15. permanent = False
  16. def get_redirect_url(self, *args, **kwargs):
  17. subscription = get_object_or_404(OfferSubscription, pk=self.kwargs['id'],
  18. member=self.request.user)
  19. return reverse(subscription.configuration.url_namespace + ':' + subscription.configuration.backend_name,
  20. args=[subscription.configuration.pk])
  21. # @cache_control(max_age=7200)
  22. def subscription_count_json(request):
  23. output = []
  24. # Get date form url, or set default
  25. date = request.GET.get('date', datetime.date.today())
  26. # Validate date type
  27. if not isinstance(date, datetime.date):
  28. try:
  29. datetime.datetime.strptime(date, '%Y-%m-%d')
  30. except ValueError, TypeError:
  31. return HttpResponseServerError("Incorrect date format, should be YYYY-MM-DD")
  32. # Get current offer subscription
  33. offersubscriptions = list(OfferSubscription.objects.running(date).offer_summary())
  34. for offersub in offersubscriptions:
  35. output.append({
  36. 'reference' : offersub['offer__reference'],
  37. 'name' : offersub['offer__name'],
  38. 'subscriptions_count' : offersub['num_subscriptions']
  39. })
  40. # Return JSON
  41. return JsonResponse(output, safe=False)