1
0
mirror of https://github.com/Damillora/Altessimo synced 2024-11-22 05:57:31 +00:00
Altessimo/artists/models.py

42 lines
1.4 KiB
Python
Raw Normal View History

2020-12-15 21:32:20 +00:00
from django.db import models
from django.apps import apps
2020-12-16 17:36:29 +00:00
from django.utils.text import slugify
2020-12-15 21:32:20 +00:00
# Create your models here.
2020-12-16 07:29:08 +00:00
class ArtistManager(models.Manager):
def create_or_new(self, romanized_name):
romanized_name = romanized_name.strip()
qs = self.get_queryset().filter(romanized_name__iexact=romanized_name)
if qs.exists():
return qs.first(), False
return Artist.objects.create(romanized_name=romanized_name), True
def comma_to_qs(self, artists_str):
final_ids = []
for artist in artists_str.split(','):
2020-12-16 08:56:29 +00:00
obj, created = self.create_or_new(artist.strip())
2020-12-16 07:29:08 +00:00
final_ids.append(obj.id)
qs = self.get_queryset().filter(id__in=final_ids).distinct()
return qs
2020-12-15 21:32:20 +00:00
class Artist(models.Model):
2020-12-16 07:29:08 +00:00
name = models.CharField(max_length=255,blank=True)
2020-12-16 17:36:29 +00:00
romanized_name = models.CharField(max_length=255)
slug = models.SlugField(max_length=255,blank=True)
2020-12-15 21:32:20 +00:00
aliases = models.ManyToManyField("self",blank=True)
category = models.ManyToManyField("categories.Category",blank=True)
2020-12-16 17:36:29 +00:00
about_artist = models.TextField(blank=True)
2020-12-15 21:32:20 +00:00
about_music = models.TextField(blank=True)
2020-12-16 07:29:08 +00:00
objects = ArtistManager()
2020-12-16 08:51:48 +00:00
class Meta:
ordering = [ 'romanized_name','name' ]
2020-12-15 21:32:20 +00:00
def __str__(self):
return self.romanized_name
2020-12-16 17:36:29 +00:00
def save(self, *args, **kwargs):
self.slug = slugify(self.romanized_name)
super().save(*args,**kwargs)