remove unnecessary imports
[mygpo.git] / mygpo / data / management / commands / update-toplist.py
blob8f848158c0171f0815063927dcf97a730875df43
1 from django.core.management.base import BaseCommand
2 from optparse import make_option
3 from mygpo.api.models import Podcast, PodcastGroup, ToplistEntry
5 class Command(BaseCommand):
7 option_list = BaseCommand.option_list + (
8 make_option('--preserve-oldplace', action='store_true', dest='preserve_oldplace', default=False, help="Indicates that the Last (week's) position should not be updated."),
12 def handle(self, *args, **options):
13 old_podcasts = {}
14 old_groups = {}
15 n=1
16 for t in ToplistEntry.objects.all().only('subscriptions', 'podcast', 'podcast_group'):
18 if options.get('preserve_oldplace'):
19 oldplace = t.oldplace
20 else:
21 oldplace = n
23 if t.podcast:
24 old_podcasts[t.podcast.id] = oldplace
25 else:
26 old_groups[t.podcast_group.id] = oldplace
27 n += 1
29 ToplistEntry.objects.all().delete()
31 for p in Podcast.objects.filter(group=None).order_by('id'):
32 subscription_count = p.subscriber_count()
33 ToplistEntry.objects.create(podcast=p, oldplace=old_podcasts.get(p.id, 0), subscriptions=subscription_count)
35 for g in PodcastGroup.objects.all().order_by('id'):
36 total = 0
37 for p in g.podcasts():
38 total += p.subscriber_count()
40 ToplistEntry.objects.create(podcast_group=g, oldplace=old_groups.get(g.id, 0), subscriptions=total)