from django.db.models.signals import post_save from django.dispatch import receiver from django.core.mail import send_mail, EmailMultiAlternatives from django.template.loader import get_template, render_to_string from .models import WithDraw, InvoiceHistory, Transaction from projects.models import Stage @receiver(post_save, sender=WithDraw) def send_for_accountant(sender, instance, created, **kwargs): if created: ctx_dict = { 'username': instance.user.username, } subject, from_email, to = 'Заявка на распечатку', 'mukhtar@mukhtar', 'muhtarzubanchi05@gmail.com' text_content = render_to_string('send_for_accountant.txt', ctx_dict) html_content = get_template('send_for_accountant.html').render(ctx_dict) msg = EmailMultiAlternatives(subject, text_content, from_email, [to]) msg.send() @receiver(post_save, sender=Transaction) def add_invoice_history(sender, instance, created, **kwargs): if 'add' in instance.type and instance.complete: inv_history = InvoiceHistory() inv_history.comment = 'Пополнение счета' inv_history.sum = instance.sum inv_history.user = instance.customer inv_history.save() @receiver(post_save, sender=Transaction) def reserve_stages(sender, instance, created, **kwargs): if 'reservation' in instance.type and instance.complete: inv_history = InvoiceHistory() inv_history.comment = 'Резервирование средств за этапы' inv_history.sum = instance.sum inv_history.user = instance.customer inv_history.save() stages_ids_raw = instance.stages_id if stages_ids_raw: stages_ids = [s for s in stages_ids_raw.split(';') if s] for pk in stages_ids: stage = Stage.objects.get(pk=pk) stage.is_paid = True stage.save()