Bump babel from 2.9.1 to 2.10.3
[mygpo.git] / mygpo / episodestates / models.py
blobe3d0478fc4e6641f84465abe1aa3967060f14d16
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)
15 # the latest action
16 action = models.CharField(
17 max_length=max(
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()
26 class Meta:
27 unique_together = [("user", "episode")]
29 @classmethod
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"))