spradio-server-django/savepointradio/radio/signals.py

22 lines
703 B
Python
Raw Normal View History

2018-01-05 20:18:12 +00:00
from django.db.models.signals import pre_save
from django.dispatch import receiver
from core.utils import naturalize
from .models import Album, Artist, Game, Song
@receiver(pre_save, sender=Album)
@receiver(pre_save, sender=Artist)
@receiver(pre_save, sender=Game)
@receiver(pre_save, sender=Song)
def update_sorted_fields(sender, instance, **kwargs):
2018-01-08 14:39:55 +00:00
"""
Whenever the name or title of a radio model object is created/modified, we
want to make sure we update the sorted field with the naturalized data for
sorting.
"""
2018-01-05 20:18:12 +00:00
if sender == Artist:
2018-01-08 14:39:28 +00:00
instance.sorted_full_name = naturalize(instance.full_name)
2018-01-05 20:18:12 +00:00
else:
2018-01-08 14:39:28 +00:00
instance.sorted_title = naturalize(instance.title)