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
, on_delete
=models
.CASCADE
)
13 episode
= models
.ForeignKey(Episode
, on_delete
=models
.CASCADE
)
16 action
= models
.CharField(
18 map(len, [action
for action
, name
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()
27 unique_together
= [("user", "episode")]
30 def dict_for_user(cls
, user
, episodes
=None):
31 """The state of the users episode as a {episode: state} dict"""
32 states
= cls
.objects
.filter(user
=user
)
34 if episodes
is not None:
35 states
= states
.filter(episode__in
=episodes
)
37 return dict(states
.values_list("episode", "action"))