From cc7d267494679bda29f08fc7bfa9c3db9810ca72 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Thu, 12 Jan 2023 16:05:47 -0500 Subject: [PATCH] Update repeated attributes for scrobblable models --- vrobbler/apps/music/models.py | 10 +++------- vrobbler/apps/podcasts/models.py | 8 +++----- vrobbler/apps/scrobbles/mixins.py | 17 +++++++++++++++++ vrobbler/apps/scrobbles/models.py | 11 ----------- vrobbler/apps/videos/models.py | 8 ++------ 5 files changed, 25 insertions(+), 29 deletions(-) create mode 100644 vrobbler/apps/scrobbles/mixins.py diff --git a/vrobbler/apps/music/models.py b/vrobbler/apps/music/models.py index 814a9aa..2678a03 100644 --- a/vrobbler/apps/music/models.py +++ b/vrobbler/apps/music/models.py @@ -1,13 +1,14 @@ import logging from typing import Dict, Optional from uuid import uuid4 -import musicbrainzngs +import musicbrainzngs from django.apps.config import cached_property from django.core.files.base import ContentFile from django.db import models from django.utils.translation import gettext_lazy as _ from django_extensions.db.models import TimeStampedModel +from scrobbles.mixins import ScrobblableMixin logger = logging.getLogger(__name__) BNULL = {"blank": True, "null": True} @@ -82,20 +83,15 @@ class Album(TimeStampedModel): return f"https://musicbrainz.org/release/{self.musicbrainz_id}" -class Track(TimeStampedModel): +class Track(ScrobblableMixin): class Opinion(models.IntegerChoices): DOWN = -1, 'Thumbs down' NEUTRAL = 0, 'No opinion' UP = 1, 'Thumbs up' - uuid = models.UUIDField(default=uuid4, editable=False, **BNULL) - title = models.CharField(max_length=255, **BNULL) artist = models.ForeignKey(Artist, on_delete=models.DO_NOTHING) album = models.ForeignKey(Album, on_delete=models.DO_NOTHING, **BNULL) musicbrainz_id = models.CharField(max_length=255, unique=True, **BNULL) - run_time = models.CharField(max_length=8, **BNULL) - run_time_ticks = models.PositiveBigIntegerField(**BNULL) - # thumbs = models.IntegerField(default=Opinion.NEUTRAL, choices=Opinion.choices) def __str__(self): return f"{self.title} by {self.artist}" diff --git a/vrobbler/apps/podcasts/models.py b/vrobbler/apps/podcasts/models.py index 5b1e0c9..915654d 100644 --- a/vrobbler/apps/podcasts/models.py +++ b/vrobbler/apps/podcasts/models.py @@ -6,6 +6,8 @@ from django.db import models from django.utils.translation import gettext_lazy as _ from django_extensions.db.models import TimeStampedModel +from vrobbler.apps.scrobbles.mixins import ScrobblableMixin + logger = logging.getLogger(__name__) BNULL = {"blank": True, "null": True} @@ -31,14 +33,10 @@ class Podcast(TimeStampedModel): return f"{self.name}" -class Episode(TimeStampedModel): - title = models.CharField(max_length=255) - uuid = models.UUIDField(default=uuid4, editable=False, **BNULL) +class Episode(ScrobblableMixin): podcast = models.ForeignKey(Podcast, on_delete=models.DO_NOTHING) number = models.IntegerField(**BNULL) pub_date = models.DateField(**BNULL) - run_time = models.CharField(max_length=8, **BNULL) - run_time_ticks = models.PositiveBigIntegerField(**BNULL) mopidy_uri = models.CharField(max_length=255, **BNULL) def __str__(self): diff --git a/vrobbler/apps/scrobbles/mixins.py b/vrobbler/apps/scrobbles/mixins.py new file mode 100644 index 0000000..e0c5a38 --- /dev/null +++ b/vrobbler/apps/scrobbles/mixins.py @@ -0,0 +1,17 @@ +from uuid import uuid4 + +from django.db import models +from django_extensions.db.models import TimeStampedModel + +BNULL = {"blank": True, "null": True} + + +class ScrobblableMixin(TimeStampedModel): + uuid = models.UUIDField(default=uuid4, editable=False, **BNULL) + title = models.CharField(max_length=255, **BNULL) + run_time = models.CharField(max_length=8, **BNULL) + run_time_ticks = models.PositiveBigIntegerField(**BNULL) + # thumbs = models.IntegerField(default=Opinion.NEUTRAL, choices=Opinion.choices) + + class Meta: + abstract = True diff --git a/vrobbler/apps/scrobbles/models.py b/vrobbler/apps/scrobbles/models.py index 4ac1c4a..6d5a884 100644 --- a/vrobbler/apps/scrobbles/models.py +++ b/vrobbler/apps/scrobbles/models.py @@ -1,7 +1,6 @@ import logging from datetime import timedelta from typing import Optional -from uuid import uuid4 from django.conf import settings from django.contrib.auth import get_user_model @@ -22,16 +21,6 @@ VIDEO_WAIT_PERIOD = getattr(settings, 'VIDEO_WAIT_PERIOD_DAYS') TRACK_WAIT_PERIOD = getattr(settings, 'MUSIC_WAIT_PERIOD_MINUTES') -class ScrobblableMixin(TimeStampedModel): - uuid = models.UUIDField(default=uuid4, editable=False, **BNULL) - title = models.CharField(max_length=255, **BNULL) - run_time = models.CharField(max_length=8, **BNULL) - run_time_ticks = models.PositiveBigIntegerField(**BNULL) - - class Meta: - abstract = True - - class Scrobble(TimeStampedModel): video = models.ForeignKey(Video, on_delete=models.DO_NOTHING, **BNULL) track = models.ForeignKey(Track, on_delete=models.DO_NOTHING, **BNULL) diff --git a/vrobbler/apps/videos/models.py b/vrobbler/apps/videos/models.py index bd8467f..5ad10e9 100644 --- a/vrobbler/apps/videos/models.py +++ b/vrobbler/apps/videos/models.py @@ -7,6 +7,7 @@ from django.urls import reverse from django.utils.translation import gettext_lazy as _ from django_extensions.db.models import TimeStampedModel from scrobbles.utils import convert_to_seconds +from scrobbles.mixins import ScrobblableMixin logger = logging.getLogger(__name__) BNULL = {"blank": True, "null": True} @@ -30,15 +31,12 @@ class Series(TimeStampedModel): verbose_name_plural = "series" -class Video(TimeStampedModel): +class Video(ScrobblableMixin): class VideoType(models.TextChoices): UNKNOWN = 'U', _('Unknown') TV_EPISODE = 'E', _('TV Episode') MOVIE = 'M', _('Movie') - # General fields - uuid = models.UUIDField(default=uuid4, editable=False, **BNULL) - title = models.CharField(max_length=255, **BNULL) video_type = models.CharField( max_length=1, choices=VideoType.choices, @@ -46,8 +44,6 @@ class Video(TimeStampedModel): ) overview = models.TextField(**BNULL) tagline = models.TextField(**BNULL) - run_time = models.CharField(max_length=8, **BNULL) - run_time_ticks = models.PositiveBigIntegerField(**BNULL) year = models.IntegerField() # TV show specific fields