models.py 867 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
from django.utils import timezone
from django.db import models
from django.core.exceptions import ValidationError

from funkwhale_api.music.models import Track


class Listening(models.Model):
    end_date = models.DateTimeField(default=timezone.now, null=True, blank=True)
Eliot Berriot's avatar
Eliot Berriot committed
10
11
12
13
14
15
16
17
    track = models.ForeignKey(
        Track, related_name="listenings", on_delete=models.CASCADE)
    user = models.ForeignKey(
        'users.User',
        related_name="listenings",
        null=True,
        blank=True,
        on_delete=models.CASCADE)
18
19
20
21
22
23
24
25
26
27
    session_key = models.CharField(max_length=100, null=True, blank=True)

    class Meta:
        ordering = ('-end_date',)

    def save(self, **kwargs):
        if not self.user and not self.session_key:
            raise ValidationError('Cannot have both session_key and user empty for listening')

        super().save(**kwargs)