listing and fake emailing options added
This commit is contained in:
parent
4eb912e8e0
commit
7a9e1186f6
|
@ -1,5 +1,6 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from optparse import make_option
|
||||
from datetime import datetime, timedelta
|
||||
import json
|
||||
import shutil
|
||||
|
@ -35,22 +36,53 @@ class Command(BaseCommand):
|
|||
email_from = settings.DEFAULT_FROM_EMAIL
|
||||
secret = settings.INVOICE_HASHING_SECRET
|
||||
|
||||
option_list = BaseCommand.option_list + (
|
||||
make_option('--list',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help='List outgoing emails in CSV format'),
|
||||
make_option('--fake',
|
||||
action='store_true',
|
||||
default=False,
|
||||
help='Fake emailing'),
|
||||
make_option('--id',
|
||||
action='store_true',
|
||||
default=None,
|
||||
help='Invoice\'s id to be emailed'),
|
||||
make_option('--to',
|
||||
action='store_true',
|
||||
default=None,
|
||||
help='Fake emailing destination'),
|
||||
)
|
||||
|
||||
|
||||
def get_invoice_hash(self, *args):
|
||||
hash = hmac.HMAC(self.secret, digestmod=sha256, msg=''.join(map(lambda s: str(s), args)))
|
||||
return hash.hexdigest()[:8]
|
||||
|
||||
|
||||
def handle(self, *args, **options):
|
||||
if options['list']:
|
||||
print "destination;nr. facture;montant;montant regle"
|
||||
|
||||
for invoice in Facture.objects.filter(Q(paye=False) | Q(date_reglement__isnull=True),
|
||||
active=True, date_limite_paie__gte=datetime.now(),
|
||||
famille__liaisonnameidfamille__isnull=False):
|
||||
queryset = Facture.objects.filter(Q(paye=False) | Q(date_reglement__isnull=True),
|
||||
active=True, date_limite_paie__gte=datetime.now(),
|
||||
famille__liaisonnameidfamille__isnull=False)
|
||||
if options['fake']:
|
||||
if not options['id']:
|
||||
raise CommandError('An invoice id should be provided')
|
||||
if not options['to']:
|
||||
raise CommandError('A destination email should be provided')
|
||||
queryset = queryset.filter(pk=options['id'])
|
||||
|
||||
for invoice in queryset:
|
||||
try:
|
||||
notification = InvoiceNotificationEmail.objects.filter(invoice_number=invoice.id).latest()
|
||||
if notification.date_sent > make_aware(datetime.now(), get_current_timezone()) - timedelta(days=notification_timeout):
|
||||
continue
|
||||
except InvoiceNotificationEmail.DoesNotExist:
|
||||
InvoiceNotificationEmail.objects.create(invoice_number=invoice.id)
|
||||
if not options['list'] or not options['fake']:
|
||||
InvoiceNotificationEmail.objects.create(invoice_number=invoice.id)
|
||||
|
||||
nameid = invoice.famille.liaisonnameidfamille_set.all()[0]
|
||||
signed_query = signature.sign_query('orig=synchro-orleans', settings.EMAILING_APIKEY)
|
||||
|
@ -71,7 +103,6 @@ class Command(BaseCommand):
|
|||
context = {'invoice': invoice, 'invoice_view_url_base': invoice_view_url_base,
|
||||
'invoice_hash': invoice_hash}
|
||||
context.update(data)
|
||||
context = Context(context)
|
||||
|
||||
if invoice.prelevement_automatique:
|
||||
email_template = 'autobilling_invoice_mail.txt'
|
||||
|
@ -82,8 +113,17 @@ class Command(BaseCommand):
|
|||
else:
|
||||
email_template = 'invoice_mail.txt'
|
||||
|
||||
if options['list']:
|
||||
print "{email};{invoice.id};{invoice.montant};{invoice.montant_regle}".format(**context)
|
||||
continue
|
||||
|
||||
context = Context(context)
|
||||
text_body = get_template(email_template).render(context)
|
||||
message = EmailMultiAlternatives(self.email_subject, text_body, self.email_from, [data['email']])
|
||||
if options['fake']:
|
||||
to = [options['to']]
|
||||
else:
|
||||
to = [data['email']]
|
||||
message = EmailMultiAlternatives(self.email_subject, text_body, self.email_from, to)
|
||||
message.attach_file(os.path.join(settings.INVOICES_DIR, 'facture_%s.pdf'% invoice.id))
|
||||
message.send()
|
||||
logger.debug('email for invoice nr. %s sent' % invoice.id)
|
||||
|
|
Reference in New Issue