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
):
16 for t
in ToplistEntry
.objects
.all().only('subscriptions', 'podcast', 'podcast_group'):
18 if options
.get('preserve_oldplace'):
24 old_podcasts
[t
.podcast
.id] = oldplace
26 old_groups
[t
.podcast_group
.id] = oldplace
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'):
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
)