From 582520ddffc40749b05bdb3eb72ff4abf68a6756 Mon Sep 17 00:00:00 2001 From: Shgck Date: Mon, 1 Feb 2016 18:42:01 +0100 Subject: [PATCH] Use a signal to update Artist slug --- label/models.py | 23 ++++++++++------------- label/tools.py | 1 + 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/label/models.py b/label/models.py index 379b10a..338e56f 100644 --- a/label/models.py +++ b/label/models.py @@ -1,14 +1,11 @@ from django.db import models +from django.db.models.signals import pre_save +from django.dispatch.dispatcher import receiver from django.template.defaultfilters import slugify class Artist(models.Model): - """ An Artist of the label. - - Attributes: - name: project / band name - image: image file associated with this project - """ + """ An Artist of the label. """ name = models.CharField(max_length = 64) slug = models.SlugField() image = models.ImageField(upload_to = "artists") @@ -20,15 +17,15 @@ class Artist(models.Model): def __str__(self): return self.name - def save(self, *args, **kwargs): - self.slug = slugify(self.name) - super().save(*args, **kwargs) - class Meta(object): ordering = ("name",) +@receiver(pre_save, sender = Artist) +def _artist_pre_save(sender, instance, **kwargs): + instance.slug = slugify(instance.name) -rel_types_hint = "full-length (0), EP (1), Split (2), Démo (3)" + +REL_TYPES_HINT = "full-length (0), EP (1), Split (2), Démo (3)" class Release(models.Model): """ A release (album, EP, ...) of the label. @@ -47,7 +44,7 @@ class Release(models.Model): ident = models.IntegerField() title = models.CharField(max_length = 256) contributors = models.ManyToManyField(Artist) - release_type = models.IntegerField(default = 0, help_text = rel_types_hint) + release_type = models.IntegerField(default = 0, help_text = REL_TYPES_HINT) cover = models.ImageField(upload_to = "releases") year = models.IntegerField() embed = models.TextField(blank = True) @@ -57,4 +54,4 @@ class Release(models.Model): return self.title class Meta(object): - ordering = ("ident", "title") \ No newline at end of file + ordering = ("ident", "title") diff --git a/label/tools.py b/label/tools.py index 41298cc..40a2aef 100644 --- a/label/tools.py +++ b/label/tools.py @@ -1,3 +1,4 @@ + def get_catalog_tag_from_id(ident): return "IDRE{:0>3d}".format(ident)