44 lines
1.2 KiB
Python
44 lines
1.2 KiB
Python
from django.db import models
|
|
from core.models import BaseModel
|
|
|
|
import os
|
|
import uuid
|
|
|
|
|
|
def album_cover_upload_to(instance, filename):
|
|
ext = filename.split(".")[-1]
|
|
return os.path.join("album_covers", f"{uuid.uuid4()}.{ext}")
|
|
|
|
|
|
def artist_cover_upload_to(instance, filename):
|
|
ext = filename.split(".")[-1]
|
|
return os.path.join("artist_covers", f"{uuid.uuid4()}.{ext}")
|
|
|
|
|
|
class Artist(BaseModel):
|
|
name = models.CharField(max_length=200)
|
|
cover_image = models.ImageField(upload_to=artist_cover_upload_to, null=True, blank=True)
|
|
|
|
def __str__(self):
|
|
return f"{self.name}"
|
|
|
|
|
|
class Album(BaseModel):
|
|
artist = models.ForeignKey(Artist, on_delete=models.CASCADE, related_name="albums")
|
|
name = models.CharField(max_length=200)
|
|
cover_image = models.ImageField(
|
|
upload_to=album_cover_upload_to, null=True, blank=True
|
|
)
|
|
|
|
def __str__(self):
|
|
return f"{self.artist} - {self.name}"
|
|
|
|
|
|
class Track(BaseModel):
|
|
title = models.CharField(max_length=200)
|
|
album = models.ForeignKey(Album, on_delete=models.CASCADE, related_name="tracks")
|
|
file = models.FileField(upload_to="music/")
|
|
|
|
def __str__(self):
|
|
return f"{self.album.artist} - {self.title}"
|