Coverage for users\signals.py: 94.7%
28 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-08-30 20:58 +0200
« prev ^ index » next coverage.py v7.15.2, created at 2026-08-30 20:58 +0200
1import os
2from django.db.models.signals import pre_save, post_save, post_delete
3from django.dispatch import receiver
4from .models import Users, Notifications
5from services.push.push_service import PushNotification
8# Delete the old picture when it actualizes
9@receiver(pre_save, sender=Users)
10def delete_old_profile_pic(sender, instance, **kwargs):
11 if not instance.pk: 11 ↛ 12line 11 didn't jump to line 12 because the condition on line 11 was never true
12 return # new user, no profile picture
14 try:
15 old_user = Users.objects.get(pk=instance.pk)
16 except Users.DoesNotExist:
17 return
19 old_file = old_user.profile_picture
20 new_file = instance.profile_picture
22 if old_file and old_file != new_file:
23 if os.path.isfile(old_file.path):
24 os.remove(old_file.path)
27@receiver(post_delete, sender=Users)
28def delete_profile_pic_on_delete(sender, instance, **kwargs):
29 file = instance.profile_picture
31 if file and os.path.isfile(file.path):
32 os.remove(file.path)
35# Envia una notificacion push al usuario cada vez que se crea una Notifications en bbdd
36@receiver(post_save, sender=Notifications)
37def send_push_on_notification_created(sender, instance, created, **kwargs):
38 if not created:
39 return
41 PushNotification.send_to_user(
42 user=instance.id_user,
43 title="BlaBlaUCM",
44 body=instance.content,
45 notification_type="notification",
46 data={"notification_id": str(instance.id)},
47 )