Skip to content

Commit 3132fce

Browse files
committed
remove undefined symbols in mailer, thanks Hadi
1 parent 17056a5 commit 3132fce

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

py4web/utils/auth_plugins/saml2_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def obj2dict(obj, processed=None):
1616
processed = processed if processed is not None else set()
1717
if obj is None:
1818
return None
19-
if isinstance(obj, (int, long, str, unicode, float, bool)):
19+
if isinstance(obj, (int, str, bytes, float, bool)):
2020
return obj
2121
if id(obj) in processed:
2222
return "<reference>"

py4web/utils/form.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1075,7 +1075,7 @@ def is_image(value):
10751075
"""
10761076
if value:
10771077
(_, extension) = os.path.splitext(value)
1078-
if extension in [".gif", ".png", ".jpg", ".jpeg",".webp", ".bmp"]:
1078+
if extension in [".gif", ".png", ".jpg", ".jpeg", ".webp", ".bmp"]:
10791079
return True
10801080
return False
10811081

py4web/utils/mailer.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import smtplib
1616
from email import message_from_string
1717
from email.encoders import encode_base64
18+
from email.mime.multipart import MIMEMultipart
19+
from email.mime.text import MIMEText
20+
from email.mime.base import MIMEBase
1821

1922
from pydal._compat import *
2023

@@ -47,6 +50,31 @@ class Settings:
4750
pass
4851

4952

53+
def to_bytes(text, encoding="utf8"):
54+
"""Converts a text (str, bytes, file-like) to encoded bytes"""
55+
# if the text is not a string or bytes, maybe it is a file
56+
if not isinstance(text, (str, bytes)):
57+
text = text.read()
58+
# if it is a unicode string encode it
59+
if isinstance(text, str):
60+
text = text.encode("utf-8")
61+
# if it is not a unicode string
62+
elif isinstance(text, bytes) and encoding != "utf-8":
63+
text = text.decode(encoding).encode("utf-8")
64+
return text
65+
66+
67+
def to_unicode(text, encoding="utf8"):
68+
"""Converts a body (str, bytes, file-like) to unicode"""
69+
# if the text is not a string or bytes, maybe it is a file
70+
if not isinstance(text, (str, bytes)):
71+
text = text.read()
72+
# if it is a bytes string encode it
73+
if isinstance(text, bytes):
74+
text = text.decode(encoding)
75+
return text
76+
77+
5078
class Mailer:
5179
"""
5280
Class for configuring and sending emails with alternative text / html
@@ -379,15 +407,7 @@ def encoded_or_raw(text):
379407
# Use multipart/mixed if there is attachments
380408
payload_in = MIMEMultipart("mixed")
381409
elif raw:
382-
# No encoding configuration for raw messages
383-
if not isinstance(body, basestring):
384-
body = body.read()
385-
if isinstance(body, unicodeT):
386-
text = body.encode("utf-8")
387-
elif not encoding == "utf-8":
388-
text = body.decode(encoding).encode("utf-8")
389-
else:
390-
text = body
410+
text = to_bytes(body, encoding)
391411
# No charset passed to avoid transport encoding
392412
# NOTE: some unicode encoded strings will produce
393413
# unreadable mail contents.
@@ -421,19 +441,9 @@ def encoded_or_raw(text):
421441

422442
if (text is not None or html is not None) and (not raw):
423443
if text is not None:
424-
if not isinstance(text, basestring):
425-
text = text.read()
426-
if isinstance(text, unicodeT):
427-
text = text.encode("utf-8")
428-
elif not encoding == "utf-8":
429-
text = text.decode(encoding).encode("utf-8")
444+
text = to_bytes(body, encoding)
430445
if html is not None:
431-
if not isinstance(html, basestring):
432-
html = html.read()
433-
if isinstance(html, unicodeT):
434-
html = html.encode("utf-8")
435-
elif not encoding == "utf-8":
436-
html = html.decode(encoding).encode("utf-8")
446+
html = to_bytes(html, encoding)
437447

438448
# Construct mime part only if needed
439449
if text is not None and html:

py4web/utils/populate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import sys
99
import uuid
1010

11-
from pydal._compat import pickle, unicodeT
11+
from pydal._compat import pickle
1212

1313
IUP = {
1414
"ad": {"minim": 1},
@@ -223,7 +223,7 @@ def populate_generator(table, default=True, compute=False, contents=None, ell=No
223223
continue # if user supplied it, let it be.
224224

225225
field = table[fieldname]
226-
if not isinstance(field.type, (str, unicodeT)):
226+
if not isinstance(field.type, str):
227227
continue
228228
elif field.type == "id":
229229
continue

0 commit comments

Comments
 (0)