When a song request is fulfilled, update song data.

This commit is contained in:
Josh Washburne 2018-03-27 15:59:53 -04:00
parent f9d24de34c
commit f8c49e18e5
2 changed files with 17 additions and 2 deletions

View file

@ -5,4 +5,4 @@ class ProfilesConfig(AppConfig):
name = 'profiles' name = 'profiles'
def ready(self): def ready(self):
from .signals import create_profile from .signals import create_profile, update_song_plays

View file

@ -1,8 +1,9 @@
from django.conf import settings from django.conf import settings
from django.db.models import F
from django.db.models.signals import post_save from django.db.models.signals import post_save
from django.dispatch import receiver from django.dispatch import receiver
from .models import RadioProfile from .models import RadioProfile, SongRequest
@receiver(post_save, sender=settings.AUTH_USER_MODEL) @receiver(post_save, sender=settings.AUTH_USER_MODEL)
@ -12,3 +13,17 @@ def create_profile(sender, instance, created, **kwargs):
""" """
if created: if created:
profile, new = RadioProfile.objects.get_or_create(user=instance) profile, new = RadioProfile.objects.get_or_create(user=instance)
@receiver(post_save, sender=SongRequest)
def update_song_plays(sender, instance, created, update_fields, **kwargs):
"""
Update the song data with the latest play time and the number of times
played.
"""
if not created and update_fields:
if 'played_at' in update_fields:
song = instance.song
song.last_played = instance.played_at
song.num_played = F('num_played') + 1
song.save()