rename/move filters, validate_doc_update
[mygpo.git] / mygpo / directory / management / commands / update-episode-toplist.py
blob7ac9649e7ba01a1cbab712312a5d37873e0f7748
1 from couchdbkit.exceptions import ResourceNotFound
3 from mygpo.core.models import Episode
4 from mygpo.users.models import EpisodeUserState
5 from mygpo.decorators import repeat_on_conflict
6 from mygpo.maintenance.management.changescmd import ChangesCommand
8 class Command(ChangesCommand):
10 def __init__(self):
11 super(Command, self).__init__('episode-toplist-status',
12 'Episode-Toplist-Update')
15 def handle_obj(self, seq, doc, actions):
16 state = EpisodeUserState.wrap(doc)
18 try:
19 episode = Episode.get(state.episode)
21 except ResourceNotFound:
22 episode = None
24 if episode:
25 listeners = episode.listener_count()
26 updated = self.update(episode=episode, listeners=listeners)
27 actions['updated'] += updated
29 else:
30 actions['missing'] += 1
33 @repeat_on_conflict(['episode'])
34 def update(self, episode, listeners):
35 if episode.listeners == listeners:
36 return False
38 episode.listeners = listeners
39 episode.save()
40 return True
43 def get_query_params(self):
44 return dict(include_docs=True, filter='episode_states/has_play_events')
47 def get_db(self):
48 return EpisodeUserState.get_db()