[Episodes] handle objects passed to update_episode_state
[mygpo.git] / mygpo / episodestates / tasks.py
blob0a3305607c6767be174b14052b83fd6e7ad43c44
1 from celery.utils.log import get_task_logger
3 from mygpo.celery import celery
4 from mygpo.history.models import EpisodeHistoryEntry
5 from mygpo.episodestates.models import EpisodeState
7 logger = get_task_logger(__name__)
10 @celery.task
11 def update_episode_state(historyentry_pk):
12 """ Updates the episode state with the saved EpisodeHistoryEntry """
14 # previously an EpisodeHistoryEntry was passed as parameters directly;
15 # as there can still be tasks like this in the queue, we should still
16 # be able to handle it
17 if isinstance(historyentry_pk, EpisodeHistoryEntry):
18 historyentry = historyentry_pk
19 else:
20 historyentry = EpisodeHistoryEntry.objects.get(pk=historyentry_pk)
22 user = historyentry.user
23 episode = historyentry.episode
25 logger.info('Updating Episode State for {user} / {episode}'.format(
26 user=user, episode=episode))
28 state = EpisodeState.objects.update_or_create(
29 user=user,
30 episode=episode,
31 defaults={
32 'action': historyentry.action,
33 'timestamp': historyentry.timestamp,