diff --git a/impression/couts.py b/impression/couts.py index e4ea3d1b..547c7765 100755 --- a/impression/couts.py +++ b/impression/couts.py @@ -2,9 +2,10 @@ # -*- coding: iso-8859-15 -*- # Utilisé par /var/www/impression/analyse.py -# /usr/lib/cups/backend/devis et /usr/lib/cups/backend/laserjet +# et /usr/scripts/impression/crans_backend.py # Écrit initial par Benoit # modifié par Brice DUBOST et Benoit +# Licence : GNU General Public Licence, version 2 import locale locale.setlocale(locale.LC_ALL, 'fr_FR') @@ -31,7 +32,9 @@ class cout: erreur="" recto_v="None" media="" - + str_cout="" + From="Imprimante <%s>" % impression.From_imprimante + def __init__(self, fichierps, media="", mode_couleur="Couleur", recto_v="None", taille="A4", nb_copie=1): """ * fichierps est le fichier PostScript (ou pdf) à analyser * media vaut transparent le cas écheant (dans ce cas taille=A4) @@ -45,6 +48,17 @@ class cout: # Pour la couleur, je n'ai pas trouve de methode valables tout le temps # Le plus simple reste donc de voir s'il y a 0% de couleur dans le ps. + # Vérification du format de fichier. + try: + en_tete=open(fichierps).read(4) + except: + self.erreur="Le fichier ne peut etre ouvert" + return + # On vérifie que ce que l'on nous a envoyé est bien un PS ou un PDF + if en_tete!='%!PS' and en_tete!='%PDF': + self.erreur="Format de fichier non supporte" + return + self.nb_copie=nb_copie # on compte le nb de copies et on enlève les balises pour ne pas recalculer nb_copie_ps=0 @@ -107,17 +121,6 @@ class cout: else: c_taille = impression.c_a4 - # Vérification du format de fichier. - try: - en_tete=open(fichier).read(4) - except: - self.erreur="Le fichier ne peut etre ouvert" - return - # On vérifie que ce que l'on nous a envoyé est bien un PS ou un PDF - if en_tete!='%!PS' and en_tete!='%PDF': - self.erreur="Format de fichier non supporte" - return - (status,rep) = commands.getstatusoutput("nice -n 10 gs -sDEVICE=%s -r100 -dBATCH -dNOPAUSE -dSAFER -dPARANOIDSAFER -dGraphicsAlphaBits=4 -dTextAlphaBits=4 -dMaxBitmap=50000000 -sOutputFile='%s' -q '%s'" % (device, fichier+"%d.png", fichier) ) if (status != 0): self.erreur="ERREUR : Ghostscript : Fichier Postscript invalide.\n" @@ -168,6 +171,11 @@ class cout: self.c_total = c_taille*self.pages+impression.c_tambour_noir*self.faces+cout_noir*self.total_noir self.c_total = int(self.nb_copie*self.c_total+impression.fact+0.5) # arrondi et facture self.c_total_euros=float(self.c_total)/100 + if (self.c_total < 100): + self.str_cout = "%s centimes d'euros" % (self.c_total) + else : + self.str_cout = "%s euros" % (self.c_total_euros) + os.system("rm -f '%s'*.png" %(fichier)) else: self.erreur=u"ERREUR : Fichier Postscript invalide. Aucun png n'a été créé\n" @@ -176,120 +184,13 @@ class cout: def remplis_template(self,template): """Cette fonction rempli le template avec les bonnes valeurs""" - if (self.c_total < 100): - str_cout = "%s centimes d'euros" % (self.c_total) - else : - str_cout = "%s euros" % (self.c_total/100.) - if self.taille=="NON_VALIDE": - taille=u"Le format de papier que vous avez demandé n'est pas valide" + if self.erreur=="Taille invalide": + taille="Le format de papier que vous avez demandé n'est pas valide" else: if self.media=="transparent": taille="transparent A4" else: taille=self.taille - try: - return template % { 'prix' : str_cout } - except: - return template % { 'noir' : self.total_noir, 'couleur' : self.total_couleur, 'faces' : self.faces, 'pages' : self.pages, 'copies' : self.nb_copie, 'taille' : taille, 'prix' : str_cout} - - def html_cout(self): - """Renvoie le cout formaté en html""" - # Format du PS (parfois different de celui choisi) - # Utile pour deboguage, sera supprime apres - template = """Résultats : - -
- Prix total : %(prix)s -

\n""" - - if not self.erreur: - return self.remplis_template(template) - else: - return self.erreur - - - def send_mail(self, Dest , sujet, template): - """Cette fonction envoie le mail template correctement rempli - provenant de l'imprimante avec comme serveur - SMTP localhost - Arguments : - Dest : destinataire - sujet : sujet du mail - template : le corps du mail - Pour le remplissage du template voir source pour le moment""" - - if not self.erreur: - mail=self.remplis_template(template) - else: - sujet="Erreur " + sujet - # Provisoire, il faudra ameliore cela.. - mail=self.erreur + template #"\n\n-- \nL'imprimante\n" - - #l'envoi - self.send_email("Imprimante <%(From)s>" % { 'From' : impression.From_imprimante}, Dest , sujet, mail) - - - 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() - + return template % { 'noir' : self.total_noir, 'couleur' : self.total_couleur, 'faces' : self.faces, 'pages' : self.pages, 'copies' : self.nb_copie, 'taille' : taille, 'prix' : self.str_cout} diff --git a/impression/crans_backend.py b/impression/crans_backend.py index 55a037b2..727dbdf6 100755 --- a/impression/crans_backend.py +++ b/impression/crans_backend.py @@ -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()