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
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"
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=';')
# 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":
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
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
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
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:
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,
+ )