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

37 lines
1.3 KiB
Python
Raw Normal View History

2018-01-19 16:26:14 +00:00
from django.conf import settings
from django.db.models import F
2018-01-19 16:26:14 +00:00
from django.db.models.signals import post_save
from django.dispatch import receiver
from .models import RadioProfile, SongRequest
2018-01-19 16:26:14 +00:00
@receiver(post_save, sender=settings.AUTH_USER_MODEL)
def create_profile(sender, instance, created, **kwargs):
"""
Create a profile object after a new user is created and link them.
"""
if created:
profile, new = RadioProfile.objects.get_or_create(user=instance)
@receiver(post_save, sender=SongRequest)
def update_song_plays(sender, instance, created, update_fields, **kwargs):
"""
Set when a song can be requested again once it's queued. Once played,
update the song data with the latest play time and the number of times
played.
"""
if not created and update_fields:
song = instance.song
2020-02-15 18:34:12 +00:00
user = instance.profile.user
if 'played_at' in update_fields:
song.last_played = instance.played_at
song.num_played = F('num_played') + 1
song.save()
if 'queued_at' in update_fields:
2020-02-15 18:34:12 +00:00
if song.is_song and (not user.is_staff or user.is_dj):
queued = instance.queued_at
song.next_play = song.get_date_when_requestable(queued)
song.save()