Use Django's new UUIDField
[mygpo.git] / mygpo / episodestates / models.py
blob039f581f365a8893e977a00f4676ddb3d3d6bcf1
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)
16 # the latest action
17 action = models.CharField(
18 max_length=max(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()
26 class Meta:
27 unique_together = [
28 ('user', 'episode')
31 @classmethod
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'))