From bf321a24ae0101a80b7637f80e4030cf3abe3c85 Mon Sep 17 00:00:00 2001 From: Yoan Le Clanche Date: Tue, 2 Mar 2021 16:08:36 +0100 Subject: [PATCH] Only export validated seminars and add mail report --- .../commands/teleforma-export-avis.py | 77 ++++++++++++++++--- 1 file changed, 65 insertions(+), 12 deletions(-) diff --git a/teleforma/management/commands/teleforma-export-avis.py b/teleforma/management/commands/teleforma-export-avis.py index c301d1b7..87d7dae9 100644 --- a/teleforma/management/commands/teleforma-export-avis.py +++ b/teleforma/management/commands/teleforma-export-avis.py @@ -1,4 +1,7 @@ from django.core.management.base import BaseCommand +from django.core.mail import send_mail +from django.conf import settings + from teleforma.models import Testimonial, Conference from pbcart.models import Cart @@ -8,6 +11,8 @@ import csv import StringIO from ftplib import FTP +from teleforma.context_processors import seminar_validated +from teleforma.views.pro import get_seminar_delta class Command(BaseCommand): help = "Send orders to avis-verifies ftp" @@ -19,10 +24,15 @@ class Command(BaseCommand): FTP_PATH = "/orders/" + def log(self, message): + logging.info(message) + self.logs += message + "\n" + + def handle(self, *args, **options): + self.logs = "" days = int(args[0]) logging.info('Generate csv file...') - logging.info('Generate csv file done') output = StringIO.StringIO() writer = csv.writer(output, delimiter=';') @@ -42,8 +52,18 @@ class Command(BaseCommand): # get testimonials created within X days delay = datetime.now() - timedelta(days=days) testimonials = Testimonial.objects.filter(date_added__gte=delay) + + added = [] + testimonial_not_ok = [] + cart_not_found = [] + no_product_code = [] + for testimonial in testimonials: user = testimonial.user + seminar = testimonial.seminar + if seminar_validated(user, seminar) and (get_seminar_delta(user, seminar) >= 0): + testimonial_not_ok.append(user) + continue # find relevant cart cart = None if testimonial.seminar.course.code == "demo": @@ -52,16 +72,18 @@ class Command(BaseCommand): if some_cart.has_item(testimonial.seminar): cart = some_cart if not cart: - logging.warning("Can't find cart for testimonial %d" % testimonial.id) + cart_not_found.append(user) + self.log("Can't find cart for testimonial %d and user %s" % (testimonial.id, str(user))) continue if not testimonial.seminar.product_code: - logging.warning("No product code for seminar %d" % testimonial.seminar.id) + no_product_code.append(user) + self.log("No product code for seminar %d" % testimonial.seminar.id) continue cart_date = cart.date_payment if not cart_date: cart_date = cart.last_updated - print("add") + added.append(user) writer.writerow([ cart.order_id, # order reference user.email, # customer email @@ -75,7 +97,8 @@ class Command(BaseCommand): for conference in Conference.objects.filter(date_end__gte=delay, date_end__lt=datetime.now()): if not conference.product_code: - logging.warning("No product code for conference %d" % conference.id) + self.log("No product code for conference %d" % conference.id) + no_product_code.append(user) continue for auditor in conference.auditor.all(): cart = None @@ -85,12 +108,14 @@ class Command(BaseCommand): cart = some_cart if not cart: - logging.warning("Can't find cart for conference %d" % conference.id) + self.log("Can't find cart for conference %d and user %s" % (conference.id, str(user))) + cart_not_found.append(user) continue cart_date = cart.date_payment if not cart_date: cart_date = cart.last_updated - print("add conf") + + added.append(user) writer.writerow([ cart.order_id, # order reference user.email, # customer email @@ -101,10 +126,32 @@ class Command(BaseCommand): conference.title.encode('utf-8') # name of the product ]) # import pdb;pdb.set_trace() - print(output.getvalue()) + + self.log("\n") + self.log("Added :") + self.log("\n".join([str(user) for user in added])) + self.log("\n") + + # if testimonial_not_ok: + # self.log("Testimonial not validated :") + # self.log("\n".join([str(user) for user in testimonial_not_ok])) + # self.log("\n") + + if cart_not_found: + self.log("Cart not found :") + self.log("\n".join([str(user) for user in cart_not_found])) + self.log("\n") + + if no_product_code: + self.log("No product code :") + self.log("\n".join([str(user) for user in no_product_code])) + self.log("\n") + + self.log("Generated file :") + self.log(output.getvalue()) output.seek(0) - logging.info('Sending csv to ftp...') + self.log('Sending csv to ftp...') ftp = FTP(self.FTP_HOST) try: @@ -115,8 +162,14 @@ class Command(BaseCommand): ftp.quit() except: ftp.close() - logging.error('Error while uploading to FTP') + self.log('Error while uploading to FTP') raise - logging.info('Sending csv to ftp done') - + self.log('Sending csv to ftp done') + send_mail( + "Rapport export d'avis", + self.logs, + settings.DEFAULT_FROM_EMAIL, + settings.REPORT_TO_EMAIL, + fail_silently=False, + ) -- 2.39.5