Factoristaion et plus de cohrence dans l'organisation.

darcs-hash:20050702174438-061a7-51258144e4d7216b77fce05e3d0c9851cf618494.gz
This commit is contained in:
rozel 2005-07-02 19:44:38 +02:00
parent 34f30a7420
commit bd825ec95a
2 changed files with 86 additions and 127 deletions

View file

@ -96,7 +96,7 @@ def utilisateur(user, rw):
sys.exit(0) # On conclue l'impression sans stopper l'imprimante
adherent = res[0]
sys.stderr.write("DEBUG: Adherent %s recupere.\n" % adherent.Nom())
sys.stderr.write("INFO: Adherent %s recupere.\n" % adherent.Nom())
return adherent
@ -129,10 +129,68 @@ def calcul_prix(nom_fic, arguments):
sys.exit(1) # On arrete l'imprimante
if prix.erreur == "Taille invalide":
sys.stderr.write("DEBUG: Erreur de taille de papier (%s) \n" % prix.taille)
sys.stderr.write("ERROR: Erreur de taille de papier (%s) \n" % prix.taille)
elif prix.erreur:
sys.stderr.write("DEBUG: Erreur du calcul du prix : %s \n" % prix.erreur)
sys.stderr.write("ERROR: Erreur du calcul du prix : %s \n" % prix.erreur)
else:
sys.stderr.write("DEBUG: Prix calcule : %s euros (taille %s, recto_verso %s).\n" %(prix.c_total_euros, prix.taille, prix.recto_v) )
sys.stderr.write("INFO: Prix calcule : %s euros (%s, %s).\n" % (prix.c_total_euros, prix.taille, prix.recto_v) )
return prix
def send_email(self, sender, recipient, subject, body):
"""Send an email.
All arguments should be Unicode strings (plain ASCII works as well).
Only the real name part of sender and recipient addresses may contain
non-ASCII characters.
The email will be properly MIME encoded and delivered though SMTP to
localhost port 25. This is easy to change if you want something different.
The charset of the email will be the first one out of US-ASCII, ISO-8859-1
and UTF-8 that can represent all the characters occurring in the email.
"""
from smtplib import SMTP
from email.MIMEText import MIMEText
from email.Header import Header
from email.Utils import parseaddr, formataddr
# Header class is smart enough to try US-ASCII, then the charset we
# provide, then fall back to UTF-8.
header_charset = 'ISO-8859-1'
# We must choose the body charset manually
for body_charset in 'US-ASCII', 'ISO-8859-1', 'UTF-8':
try:
body.encode(body_charset)
except UnicodeError:
pass
else:
break
# Split real name (which is optional) and email address parts
sender_name, sender_addr = parseaddr(sender)
recipient_name, recipient_addr = parseaddr(recipient)
# We must always pass Unicode strings to Header, otherwise it will
# use RFC 2047 encoding even on plain ASCII strings.
sender_name = str(Header(unicode(sender_name), header_charset))
recipient_name = str(Header(unicode(recipient_name), header_charset))
# Make sure email addresses do not contain non-ASCII characters
sender_addr = sender_addr.encode('ascii')
recipient_addr = recipient_addr.encode('ascii')
# Create the message ('plain' stands for Content-Type: text/plain)
msg = MIMEText(body.encode(body_charset), 'plain', body_charset)
msg['From'] = formataddr((sender_name, sender_addr))
msg['To'] = formataddr((recipient_name, recipient_addr))
msg['Subject'] = Header(unicode(subject), header_charset)
# Send the message via SMTP to localhost:25
smtp = SMTP("localhost")
smtp.sendmail(sender, recipient, msg.as_string())
smtp.quit()