backend/pitch_deck_generator/decks/signals.py

74 lines
2.8 KiB
Python
Raw Normal View History

from io import BytesIO
import requests
from django.core.files import File
2023-08-26 14:10:10 +03:00
from django.db.models.signals import post_save, pre_save
2023-08-26 09:36:47 +03:00
from django.dispatch import receiver
from pitch_deck_generator.decks.models import (
PdfToPPTXStorage,
PitchDeck,
QuestionAnswer,
)
2023-08-26 10:44:23 +03:00
from pitch_deck_generator.decks.tasks import (
ML_HOST,
create_images_mokups,
2023-08-26 10:44:23 +03:00
generate_numeric_values,
2023-08-26 14:10:10 +03:00
qenerate_answer_qr,
2023-08-26 10:44:23 +03:00
run_pitch_deck_calculation,
2023-08-26 14:10:10 +03:00
save_answer_to_deck,
2023-08-26 10:44:23 +03:00
)
2023-08-26 09:36:47 +03:00
@receiver(post_save, sender=PitchDeck)
2023-08-26 14:10:10 +03:00
def pitch_deck_create(sender, instance: PitchDeck, created, **kwargs):
2023-08-26 09:36:47 +03:00
if created:
2023-08-26 14:10:10 +03:00
run_pitch_deck_calculation.apply_async(kwargs={"pk": instance.pk}, delay=1)
2023-08-26 10:44:23 +03:00
@receiver(post_save, sender=QuestionAnswer)
2023-08-26 14:10:10 +03:00
def question_answer_create(sender, instance: QuestionAnswer, created, **kwargs):
2023-08-26 10:44:23 +03:00
if created:
if instance.question.inner_tag == "category":
2023-08-26 14:10:10 +03:00
generate_numeric_values.apply_async(
kwargs={"pk": instance.deck.pk}, countdown=1
)
elif instance.question.inner_tag == "names":
instance.deck.name = instance.answer
instance.deck.save()
2023-08-26 14:10:10 +03:00
elif instance.question.inner_tag in ["finance_model"]:
qenerate_answer_qr.apply_async(kwargs={"pk": instance.pk}, countdown=5)
elif instance.question.inner_tag == "images":
create_images_mokups.apply_async(kwargs={"pk": instance.pk}, countdown=5)
save_answer_to_deck.apply_async(kwargs={"pk": instance.pk}, countdown=10)
2023-08-26 14:10:10 +03:00
@receiver(pre_save, sender=QuestionAnswer)
2023-08-26 17:16:07 +03:00
def question_answer_update(sender, instance: QuestionAnswer, **kwargs):
2023-08-26 14:10:10 +03:00
if instance.id:
if instance.question.inner_tag == "category":
generate_numeric_values.apply_async(
kwargs={"pk": instance.deck.pk}, countdown=1
)
elif instance.question.inner_tag in ["finance_model"]:
qenerate_answer_qr.apply_async(kwargs={"pk": instance.pk}, countdown=5)
elif instance.question.inner_tag == "images":
create_images_mokups.apply_async(kwargs={"pk": instance.pk}, countdown=5)
save_answer_to_deck.apply_async(kwargs={"pk": instance.pk}, countdown=10)
@receiver(post_save, sender=PdfToPPTXStorage)
def pdt_to_pptx_convert(sender, instance: PdfToPPTXStorage, created, **kwargs):
if created:
with open(instance.pdf.path, "rb") as f:
r = requests.post(ML_HOST + "convert-to-pptx", files={"in_file": f}).json()
data = requests.get(ML_HOST + r["file"][1:]).content
instance.pptx.save(
instance.pdf.path.split("/")[-1].replace("pdf", "pptx"),
File(
BytesIO(data),
instance.pdf.path.split("/")[-1].replace("pdf", "pptx"),
),
)
instance.save()