2022-10-08 19:31:01 +03:00
|
|
|
from django.core.files import File
|
|
|
|
from django.db.models.signals import pre_save, post_delete, post_save
|
|
|
|
from django.dispatch import receiver
|
|
|
|
|
|
|
|
from utils.file import crop_image
|
|
|
|
from utils.generators import generate_charset
|
|
|
|
from .models import EventAttendance, Event
|
|
|
|
|
|
|
|
|
2022-10-09 00:42:30 +03:00
|
|
|
@receiver(post_save, sender=EventAttendance)
|
|
|
|
def create_attendance(sender, instance, created, **kwargs):
|
|
|
|
if created:
|
2022-10-08 19:31:01 +03:00
|
|
|
token = generate_charset(25)
|
2022-10-09 00:42:30 +03:00
|
|
|
while EventAttendance.objects.filter(token=token).exists():
|
|
|
|
token = generate_charset(25)
|
|
|
|
instance.token = token
|
2022-10-08 19:31:01 +03:00
|
|
|
|
2022-10-09 00:42:30 +03:00
|
|
|
instance.event.planning += 1
|
|
|
|
instance.event.save(update_fields=["planning", "token"])
|
2022-10-08 19:31:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_save, sender=Event)
|
|
|
|
def process_event(sender, instance, created, **kwargs):
|
|
|
|
if created:
|
|
|
|
slug = generate_charset(5)
|
|
|
|
while Event.objects.filter(slug=slug).exists():
|
|
|
|
slug = generate_charset(5)
|
|
|
|
instance.slug = slug
|
|
|
|
instance.save(update_fields=["slug"])
|
|
|
|
|
|
|
|
if instance.image and kwargs["update_fields"] is None:
|
|
|
|
instance.image_cropped = File(
|
|
|
|
crop_image(instance.image.path, cut_to=(250, 250)),
|
|
|
|
name=instance.image.path.split(".")[0].split("/")[-1] + ".png",
|
|
|
|
)
|
|
|
|
instance.save(update_fields=["image_cropped"])
|
|
|
|
|
|
|
|
|
|
|
|
@receiver(post_delete, sender=EventAttendance)
|
|
|
|
def delete_attendance(sender, instance, **kwargs):
|
|
|
|
instance.event.planning -= 1
|
2022-10-09 00:42:30 +03:00
|
|
|
instance.event.save(update_fields=["planning"])
|