backend/pitch_deck_generator/tickets/signals.py

43 lines
1.3 KiB
Python
Raw Normal View History

2023-08-26 01:17:47 +03:00
from django.core.cache import cache
2023-08-26 09:53:03 +03:00
from django.db.models.signals import post_delete, post_save, pre_save
2023-08-26 01:17:47 +03:00
from django.dispatch import receiver
from pitch_deck_generator.tickets.models import Ticket
@receiver(post_save, sender=Ticket)
def ticket_to_cache(sender, instance: Ticket, created, **kwargs):
if created:
cache.set_many(
{
f"{instance.id}": True,
f"{instance.id}-current": 0,
f"{instance.id}-name": instance.name,
f"{instance.id}-max": instance.max,
f"{instance.id}-next": instance.next,
},
3600,
)
@receiver(pre_save, sender=Ticket)
def update_ticket(sender, instance: Ticket, **kwargs):
if instance.id is not None:
cache.set_many(
{
f"{instance.id}": True,
f"{instance.id}-name": instance.name,
f"{instance.id}-max": instance.max,
f"{instance.id}-next": instance.next,
},
3600,
)
@receiver(post_delete, sender=Ticket)
def delete_ticket(sender, instance: Ticket, **kwargs):
uuid = instance.id
cache.delete_many(
[f"{uuid}", f"{uuid}-max", f"{uuid}-current", f"{uuid}-name", f"{uuid}-next"]
)