def invoices_task(request): action = int(request.POST.get("action", -1)) now = default_tz_now() last_month_date = (now - timedelta(days=now.day)) month = last_month_date.month year = last_month_date.year start_date = datetime.combine(date(year=year, month=month, day=1), default_tz_time_min()) end_date = datetime.combine(date(year=year, month=month, day=calendar.monthrange(year, month)[1]), default_tz_time_max()) trx_qs = BillingTransaction.objects.filter(debug=False, invoice_sent=False, status=BillingStatus.CHARGED, charge_date__gte=start_date, charge_date__lte=end_date) if action == InvoiceActions.CREATE_ID: logging.info("Creating invoice ids: %s - %s" % (start_date, end_date)) failed = do_create_invoice_ids(trx_qs) elif action == InvoiceActions.SEND: logging.info("Sending invoices: %s - %s" % (start_date, end_date)) failed = do_send_invoices(trx_qs) else: raise RuntimeError("NOT A VALID INVOICE ACTION") action_name = InvoiceActions.get_name(action) if failed: notify_by_email("Error %s invoices for month %s/%s" % (action_name, month, year), "failed with following passenger ids %s" % failed) else: notify_by_email("Success %s invoices for month %s/%s" % (action_name, month, year)) return HttpResponse("OK")
def generate_passengers_list(): jan_first = datetime.datetime.combine(datetime.date(2012, 1, 1), default_tz_time_min()) rides = SharedRide.objects.filter(depart_time__gte=jan_first) passengers = [] for ride in rides: if ride.debug: continue for o in ride.orders.all(): if o.passenger.create_date > jan_first: passengers.append(o.passenger) passengers = set(passengers) csv = u"" for p in passengers: user = p.user csv += u",".join([user.email, user.get_full_name()]) csv += u"\n" send_mail_as_noreply("*****@*****.**", "passengers list", attachments=[("passengers.csv", csv.encode("utf-8"))])