1 from collections
import Counter
3 from django
.core
.management
.base
import BaseCommand
5 from mygpo
.utils
import progress
6 from mygpo
.core
.models
import Podcast
7 from mygpo
.decorators
import repeat_on_conflict
8 from mygpo
.db
.couchdb
.podcast
import podcast_count
, podcast_by_id
, \
9 all_podcasts
, subscriberdata_for_podcast
12 class Command(BaseCommand
):
14 Moves SubscriberData from a Podcast to its PodcastSubscriberData document.
16 new SubscriberData is added (for example by update-toplist) to the Podcast
17 so that it always has the most current data available. To avoid too big
18 Podcast documents, this command moves old data to a separate document,
19 leaving the latest two entries in both the Podcast and its
20 PodcastSubscriberData document.
23 def handle(self
, *args
, **options
):
25 total
= podcast_count()
26 podcasts
= all_podcasts()
29 for n
, podcast
in enumerate(podcasts
):
31 psubscriber
= subscriberdata_for_podcast(podcast
.get_id())
33 res
= self
.update_subscriber_data(podcast
, data
=psubscriber
)
34 self
.update_podcast(podcast
=podcast
)
36 action
= 'updated' if res
else 'skipped'
39 status_str
= ', '.join('%s: %d' % x
for x
in actions
.items())
40 progress(n
+1, total
, status_str
)
43 @repeat_on_conflict(['data'])
44 def update_subscriber_data(self
, podcast
, data
):
45 l1
= len(data
.subscribers
)
47 subscribers
= set(data
.subscribers
+ podcast
.subscribers
)
48 data
.subscribers
= sorted(subscribers
, key
=lambda x
: x
.timestamp
)
50 if len(data
.subscribers
) != l1
:
55 @repeat_on_conflict(['podcast'], reload_f
=lambda p
: podcast_by_id(p
.get_id()))
56 def update_podcast(self
, podcast
):
57 if len(podcast
.subscribers
) > 2:
58 podcast
.subscribers
= podcast
.subscribers
[-2:]