From 18e45ab9945070816f5aadc033b83c524f1616d2 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Sat, 11 May 2013 22:03:05 -0500 Subject: [PATCH] Issue 1489:some improvement in Mail send class, thanks Alan --- VERSION | 2 +- gluon/dal.py | 24 +++++++----------------- gluon/tools.py | 35 +++++++++++++++++++++++++++-------- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/VERSION b/VERSION index aa8ae9b6..6537a0e8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.4.6-stable+timestamp.2013.05.10.08.32.00 +Version 2.4.6-stable+timestamp.2013.05.11.22.01.55 diff --git a/gluon/dal.py b/gluon/dal.py index 276ba907..6354769b 100644 --- a/gluon/dal.py +++ b/gluon/dal.py @@ -472,6 +472,11 @@ def IDENTITY(x): return x def varquote_aux(name,quotestr='%s'): return name if REGEX_W.match(name) else quotestr % name +def quote_keyword(a,keyword='timestamp'): + regex = re.compile('\.keyword(?=\w)') + a = regex.sub('."%s"' % keyword,a) + return a + if 'google' in DRIVERS: is_jdbc = False @@ -1764,6 +1769,8 @@ class BaseAdapter(ConnectionPool): def log_execute(self, *a, **b): if not self.connection: return None command = a[0] + if hasattr(self,'filter_sql_command'): + command = self.filter_sql_command(command) if self.db._debug: LOGGER.debug('SQL: %s' % command) self.db._lastsql = command @@ -3377,24 +3384,7 @@ class VerticaAdapter(MSSQLAdapter): self.execute('SELECT LAST_INSERT_ID();') return long(self.cursor.fetchone()[0]) - - regex_timestamp = re.compile('\.timestamp') - - @staticmethod - def quote_timestamp(a): - tokens = a.split('.timestamp') - c, a = 0, '' - for token in tokens: - if a=='': - a=token - else: - if c%2==0: a+='."timestamp"'+token - else: a+='.timestamp'+token - c+=token.count("'") - return a - def execute(self, a): - a = self.quote_timestamp(a) return self.log_execute(a) class SybaseAdapter(MSSQLAdapter): diff --git a/gluon/tools.py b/gluon/tools.py index 9121d0a7..5fe547a1 100644 --- a/gluon/tools.py +++ b/gluon/tools.py @@ -371,15 +371,16 @@ class Mail(object): if not isinstance(sender, str): raise Exception('Sender address not specified') - if not raw: + if not raw and attachments: + # Use multipart/mixed if there is attachments payload_in = MIMEMultipart.MIMEMultipart('mixed') - else: + elif raw: # no encoding configuration for raw messages if not isinstance(message, basestring): message = message.read() if isinstance(message, unicode): text = message.encode('utf-8') - elif not encoding=='utf-8': + elif not encoding == 'utf-8': text = message.decode(encoding).encode('utf-8') else: text = message @@ -411,25 +412,43 @@ class Mail(object): html = None if (not text is None or not html is None) and (not raw): - attachment = MIMEMultipart.MIMEMultipart('alternative') + if not text is None: if not isinstance(text, basestring): text = text.read() if isinstance(text, unicode): text = text.encode('utf-8') - elif not encoding=='utf-8': + elif not encoding == 'utf-8': text = text.decode(encoding).encode('utf-8') - attachment.attach(MIMEText.MIMEText(text, _charset='utf-8')) if not html is None: if not isinstance(html, basestring): html = html.read() if isinstance(html, unicode): html = html.encode('utf-8') - elif not encoding=='utf-8': + elif not encoding == 'utf-8': html = html.decode(encoding).encode('utf-8') + + # Construct mime part only if needed + if text and html: + # We have text and html we need multipart/alternative + attachment = MIMEMultipart.MIMEMultipart('alternative') + attachment.attach(MIMEText.MIMEText(text, _charset='utf-8')) attachment.attach( MIMEText.MIMEText(html, 'html', _charset='utf-8')) - payload_in.attach(attachment) + elif text: + attachment = MIMEText.MIMEText(text, _charset='utf-8') + elif html: + attachment = \ + MIMEText.MIMEText(html, 'html', _charset='utf-8') + + if attachments: + # If there is attachments put text and html into + # multipart/mixed + payload_in.attach(attachment) + else: + # No attachments no multipart/mixed + payload_in = attachment + if (attachments is None) or raw: pass elif isinstance(attachments, (list, tuple)):