offer_subscriptions_count.py 792 B

1234567891011121314151617181920212223
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.core.management.base import BaseCommand, CommandError
  4. from django.db.models import Count
  5. from coin.offers.models import Offer, OfferSubscription
  6. BOLD_START = '\033[1m'
  7. BOLD_END = '\033[0m'
  8. class Command(BaseCommand):
  9. help = "Return subscription count for each offer type"
  10. def handle(self, *args, **options):
  11. offers = Offer.objects\
  12. .annotate(num_subscribtions=Count('offersubscription'))\
  13. .order_by('name')
  14. for offer in offers:
  15. self.stdout.write("{offer} offer has {count} subscriber(s)".format(
  16. offer=BOLD_START + offer.name + BOLD_END,
  17. count=BOLD_START + str(offer.num_subscribtions) + BOLD_END))