From 4f65cdfa989a8d40ff9ac480ade4037572bb096c Mon Sep 17 00:00:00 2001 From: Serghei MIHAI Date: Mon, 6 Oct 2014 23:56:20 +0200 Subject: [PATCH] fixing user infos retreiving and email body building --- .../management/commands/email_new_invoices.py | 25 +++++++++++++------ .../data/templates/invoice_mail.txt | 2 +- synchro_orleans/settings.py | 8 +++--- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/synchro_orleans/data/management/commands/email_new_invoices.py b/synchro_orleans/data/management/commands/email_new_invoices.py index 338c4e5..94fb660 100644 --- a/synchro_orleans/data/management/commands/email_new_invoices.py +++ b/synchro_orleans/data/management/commands/email_new_invoices.py @@ -4,6 +4,7 @@ import shutil import os import requests import logging +from hashlib import sha256 from django.core.management.base import BaseCommand, CommandError from django.conf import settings @@ -12,7 +13,6 @@ from django.db.models import Q from django.template.loader import get_template from django.template import Context from django.core.mail import EmailMultiAlternatives -from django.contrib.sites.models import Site from synchro_orleans.data.models import Facture, InvoiceNotificationEmail from synchro_orleans import signature @@ -32,6 +32,10 @@ class Command(BaseCommand): email_template = 'invoice_mail.txt' email_from = settings.DEFAULT_FROM_EMAIL + def get_invoice_hash(self, *args): + hash = sha256(''.join(map(lambda s: str(s), args))) + return hash.hexdigest()[:8] + def handle(self, *args, **options): @@ -46,22 +50,27 @@ class Command(BaseCommand): InvoiceNotificationEmail.objects.create(invoice_number=invoice.id) nameid = invoice.famille.liaisonnameidfamille_set.all()[0] - signed_query = signature.sign_query('orig=synchro-orleans') - url = '{url}/{nameid}?{query}'.format(url=settings.AUTHENTIC2_USER_INFO_URL, + signed_query = signature.sign_query('orig=synchro-orleans', settings.EMAILING_APIKEY) + url = '{url}/{nameid}?{query}'.format(url=settings.IDP_USER_INFO_URL, nameid=nameid.name_id, query=signed_query) logger.debug('requesting: %s' % url) response = requests.get(url) - data = response.json() + data = response.json().get('data', None) + if not data: + continue attachment = os.path.join(settings.INVOICES_DIR, 'facture_%s.pdf' % invoice.id) + invoice_hash = self.get_invoice_hash(invoice.id, + invoice.date_generation, + invoice.montant) - context = Context({'invoice': invoice, - 'site': Site.objects.get_current(), - 'invoice_view_url_base': invoice_view_url_base},) + context = {'invoice': invoice, 'invoice_view_url_base': invoice_view_url_base, + 'invoice_hash': invoice_hash} context.update(data) + context = Context(context) text_body = get_template(self.email_template).render(context) - message = EmailMultiAlternatives(self.email_subject, text_body, self.email_from, [result.email]) + message = EmailMultiAlternatives(self.email_subject, text_body, self.email_from, [data['email']]) 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) diff --git a/synchro_orleans/data/templates/invoice_mail.txt b/synchro_orleans/data/templates/invoice_mail.txt index 4c3e874..3583384 100644 --- a/synchro_orleans/data/templates/invoice_mail.txt +++ b/synchro_orleans/data/templates/invoice_mail.txt @@ -2,7 +2,7 @@ Bonjour {{ first_name }} {{ last_name }}, La facture nr. {{ invoice.id }} d'un montant de {{ invoice.amount }} {{ invoice.devise }} a ete emise le {{ invoice.creation_date }}. -Vous pouvez la visualiser a l'adresse {{ invoice_view_url_base }}/{{ invoice.id }}/{{ invoice.hash }} +Vous pouvez la visualiser a l'adresse {{ invoice_view_url_base }}/{{ invoice.id }}/{{ invoice_hash }} Cordialement, diff --git a/synchro_orleans/settings.py b/synchro_orleans/settings.py index 2fae744..2bf70bd 100644 --- a/synchro_orleans/settings.py +++ b/synchro_orleans/settings.py @@ -163,13 +163,13 @@ INVOICES_DIR = os.environ.get('INVOICES_DIR', os.path.dirname(__file__)) INVOICES_LOCATION_PATTERN = os.path.join(INVOICES_DIR, 'facture_{invoice_id}.pdf') INVOICES_NOTIFICATION_TIMEOUT = os.environ.get('INVOICES_NOTIFICATION_TIMEOUT', 10) # in days -AUTHENTIC2_URL = os.environ.get('AUTHENTIC2_URL', '') -PORTAIL_CITOYEN_URL = os.environ.get('PORTAIl_CITOYEN_URL', '') +IDP_URL = os.environ.get('IDP_URL', '') +PORTAIL_CITOYEN_URL = os.environ.get('PORTAIL_CITOYEN_URL', '') EMAILING_APIKEY = os.environ.get('EMAILING_APIKEY', '12345') -INVOICE_VIEW_URL_BASE = PORTAIL_CITOYEN_URL + '/facture/simple/tipi/' +INVOICE_VIEW_URL_BASE = PORTAIL_CITOYEN_URL + '/facture/simple/tipi' -AUTHENTIC2_USER_INFO_URL = '%s/userinfo' % AUTHENTIC2_URL +IDP_USER_INFO_URL = '%s/userinfo' % IDP_URL try: from local_settings import *