1 from django
.db
import models
2 from django
.conf
import settings
4 from mygpo
.podcasts
.models
import Episode
5 from mygpo
.history
.models
import EpisodeHistoryEntry
8 class EpisodeState(models
.Model
):
9 """ The latest status of an episode for a user """
11 user
= models
.ForeignKey(settings
.AUTH_USER_MODEL
,
12 on_delete
=models
.CASCADE
)
14 episode
= models
.ForeignKey(Episode
, on_delete
=models
.CASCADE
)
17 action
= models
.CharField(
18 max_length
=max(list(map(len, [action
for action
, name
19 in EpisodeHistoryEntry
.EPISODE_ACTIONS
]))),
20 choices
=EpisodeHistoryEntry
.EPISODE_ACTIONS
,
23 # the timestamp at which the event happened (provided by the client)
24 timestamp
= models
.DateTimeField()
32 def dict_for_user(cls
, user
, episodes
=None):
33 """ The state of the users episode as a {episode: state} dict """
34 states
= cls
.objects
.filter(user
=user
)
36 if episodes
is not None:
37 states
= states
.filter(episode__in
=episodes
)
39 return dict(states
.values_list('episode', 'action'))