2023-08-27 00:23:57 +03:00
|
|
|
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
|
|
|
|
|
2023-08-27 00:23:57 +03:00
|
|
|
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 (
|
2023-08-27 00:23:57 +03:00
|
|
|
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
|
|
|
|
)
|
2023-08-27 00:23:57 +03:00
|
|
|
elif instance.question.inner_tag == "names":
|
2023-08-27 04:15:52 +03:00
|
|
|
instance.deck.name = instance.answer if instance.answer else None
|
2023-08-27 00:23:57 +03:00
|
|
|
instance.deck.save()
|
2023-08-26 14:10:10 +03:00
|
|
|
elif instance.question.inner_tag in ["finance_model"]:
|
2023-08-27 00:23:57 +03:00
|
|
|
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
|
|
|
|
)
|
2023-08-27 04:15:52 +03:00
|
|
|
elif instance.question.inner_tag == "names":
|
|
|
|
instance.deck.name = instance.answer if instance.answer else None
|
|
|
|
instance.deck.save()
|
2023-08-26 14:10:10 +03:00
|
|
|
elif instance.question.inner_tag in ["finance_model"]:
|
2023-08-27 00:23:57 +03:00
|
|
|
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()
|