models.py 1020 Bytes
Newer Older
1
2
3
4
5
6
7
8
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):
9
10
    creation_date = models.DateTimeField(
        default=timezone.now, null=True, blank=True)
Eliot Berriot's avatar
Eliot Berriot committed
11
12
13
14
15
16
17
18
    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)
19
20
21
    session_key = models.CharField(max_length=100, null=True, blank=True)

    class Meta:
22
        ordering = ('-creation_date',)
23
24
25
26
27
28

    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)
29
30
31
32
33


    def get_activity_url(self):
        return '{}/listenings/tracks/{}'.format(
            self.user.get_activity_url(), self.pk)