update cmd "update-episode-toplist" to userdata db
[mygpo.git] / mygpo / directory / management / commands / update-episode-toplist.py
blobe8f344ed6363ff351670e818d54acd4e9cdc973f
1 from mygpo.users.models import EpisodeUserState
2 from mygpo.maintenance.management.changescmd import ChangesCommand
3 from mygpo.db.couchdb import get_userdata_database
4 from mygpo.db.couchdb.episode import episode_by_id, set_episode_listeners
5 from mygpo.db.couchdb.episode_state import episode_listener_count
8 class Command(ChangesCommand):
10 def __init__(self):
11 super(Command, self).__init__('episode-toplist-status',
12 'Episode-Toplist-Update')
14 def handle_obj(self, seq, doc, actions):
15 state = EpisodeUserState.wrap(doc)
17 episode = episode_by_id(state.episode)
19 if not episode:
20 actions['missing'] += 1
21 return
23 listeners = episode_listener_count(episode)
24 updated = set_episode_listeners(episode, listeners)
25 actions['updated'] += int(updated)
28 def get_query_params(self):
29 return dict(include_docs=True, filter='episode_states/has_play_events')
31 def get_db(self):
32 return get_userdata_database()