lib.itmens/catalog/music/models.py

124 lines
3.8 KiB
Python
Raw Normal View History

2023-02-15 15:45:57 -05:00
from datetime import date
2022-12-16 01:08:10 -05:00
from django.db import models
from django.utils.translation import gettext_lazy as _
2024-04-03 23:10:21 -04:00
from django.utils.translation import pgettext_lazy
2023-08-11 01:43:19 -04:00
from catalog.common import (
BaseSchema,
IdType,
Item,
ItemCategory,
ItemInSchema,
ItemType,
PrimaryLookupIdDescriptor,
jsondata,
)
2024-07-16 02:28:53 -04:00
from catalog.common.models import LIST_OF_ONE_PLUS_STR_SCHEMA
2023-02-15 15:45:57 -05:00
class AlbumInSchema(ItemInSchema):
other_title: list[str]
2023-02-15 15:45:57 -05:00
genre: list[str]
artist: list[str]
company: list[str]
duration: int | None = None
release_date: date | None = None
track_list: str | None = None
class AlbumSchema(AlbumInSchema, BaseSchema):
2023-02-15 16:22:32 -05:00
barcode: str | None = None
2023-02-15 15:45:57 -05:00
pass
class Album(Item):
2023-06-05 02:04:52 -04:00
type = ItemType.Album
2022-12-29 23:57:02 -05:00
url_path = "album"
2022-12-11 23:20:28 +00:00
category = ItemCategory.Music
2022-12-08 15:45:37 +00:00
barcode = PrimaryLookupIdDescriptor(IdType.GTIN)
douban_music = PrimaryLookupIdDescriptor(IdType.DoubanMusic)
spotify_album = PrimaryLookupIdDescriptor(IdType.Spotify_Album)
2022-12-16 01:08:10 -05:00
METADATA_COPY_LIST = [
2024-07-13 00:16:47 -04:00
# "title",
# "other_title",
"localized_title",
2023-01-05 03:06:13 -05:00
"artist",
"company",
"track_list",
2024-07-13 00:16:47 -04:00
# "brief",
"localized_description",
2022-12-29 23:57:02 -05:00
"album_type",
"media",
"disc_count",
"genre",
"release_date",
"duration",
"bandcamp_album_id",
2022-12-16 01:08:10 -05:00
]
2024-01-20 23:40:47 -05:00
release_date = jsondata.DateField(
2024-03-10 20:55:50 -04:00
_("release date"), null=True, blank=True, help_text=_("YYYY-MM-DD")
)
duration = jsondata.IntegerField(
_("length"), null=True, blank=True, help_text=_("milliseconds")
2024-01-20 23:40:47 -05:00
)
2024-11-22 23:44:14 -05:00
artist = jsondata.ArrayField(
2024-03-10 20:55:50 -04:00
verbose_name=_("artist"),
2024-11-22 23:44:14 -05:00
base_field=models.CharField(blank=True, default="", max_length=100),
2024-07-16 02:28:53 -04:00
null=False,
blank=False,
2022-12-29 23:57:02 -05:00
default=list,
2024-07-16 02:28:53 -04:00
schema=LIST_OF_ONE_PLUS_STR_SCHEMA,
2022-12-29 23:57:02 -05:00
)
genre = jsondata.ArrayField(
2024-04-03 23:10:21 -04:00
verbose_name=pgettext_lazy("music", "genre"),
base_field=models.CharField(blank=True, default="", max_length=50),
null=True,
blank=True,
default=list,
)
2022-12-29 23:57:02 -05:00
company = jsondata.ArrayField(
models.CharField(blank=True, default="", max_length=500),
2024-03-10 20:55:50 -04:00
verbose_name=_("publisher"),
2022-12-29 23:57:02 -05:00
null=True,
blank=True,
default=list,
)
2024-07-16 02:28:53 -04:00
track_list = jsondata.TextField(_("tracks"), blank=True)
other_title = jsondata.ArrayField(
2024-03-10 20:55:50 -04:00
verbose_name=_("other title"),
base_field=models.CharField(blank=True, default="", max_length=200),
null=True,
blank=True,
default=list,
)
2024-07-16 02:28:53 -04:00
album_type = jsondata.CharField(_("album type"), blank=True, max_length=500)
media = jsondata.CharField(_("media type"), blank=True, max_length=500)
bandcamp_album_id = jsondata.CharField(blank=True, max_length=500)
2024-03-10 20:55:50 -04:00
disc_count = jsondata.IntegerField(
2024-05-19 16:32:59 -04:00
_("number of discs"), blank=True, default="", max_length=500
2024-03-10 20:55:50 -04:00
)
2023-01-05 03:06:13 -05:00
def get_embed_link(self):
for res in self.external_resources.all():
if res.id_type == IdType.Bandcamp.value and res.metadata.get(
"bandcamp_album_id"
):
return f"https://bandcamp.com/EmbeddedPlayer/album={res.metadata.get('bandcamp_album_id')}/size=large/bgcol=ffffff/linkcol=19A2CA/artwork=small/transparent=true/"
if res.id_type == IdType.Spotify_Album.value:
return res.url.replace("open.spotify.com/", "open.spotify.com/embed/")
if res.id_type == IdType.AppleMusic.value:
return res.url.replace("music.apple.com/", "embed.music.apple.com/us/")
return None
2023-01-05 03:06:13 -05:00
@classmethod
def lookup_id_type_choices(cls):
id_types = [
IdType.GTIN,
IdType.ISRC,
IdType.Spotify_Album,
IdType.Bandcamp,
IdType.DoubanMusic,
]
return [(i.value, i.label) for i in id_types]