From 83bc616db700406c8254ae036ef5f41796e80a79 Mon Sep 17 00:00:00 2001 From: Filipp Lepalaan Date: Sun, 6 Jun 2021 14:34:24 +0300 Subject: Python 3 fixes --- servo/lib/ucsv.py | 2 +- servo/management/commands/backup.py | 2 +- servo/management/commands/cron.py | 2 +- servo/models/common.py | 4 ++-- servo/models/note.py | 19 ++++++++++++++++--- servo/views/product.py | 10 +++++----- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/servo/lib/ucsv.py b/servo/lib/ucsv.py index accf7d1..8213b74 100644 --- a/servo/lib/ucsv.py +++ b/servo/lib/ucsv.py @@ -18,7 +18,7 @@ class UnicodeCsvReader(object): # read and split the csv row into fields row = self.csv_reader.next() # now decode - return [unicode(cell, self.encoding) for cell in row] + return [str(cell, self.encoding) for cell in row] @property def line_num(self): diff --git a/servo/management/commands/backup.py b/servo/management/commands/backup.py index 28129e6..253c4fa 100644 --- a/servo/management/commands/backup.py +++ b/servo/management/commands/backup.py @@ -17,7 +17,7 @@ def write(path, header, cursor): writer.writerow(header) for row in cursor.fetchall(): - row = [unicode(s).encode('utf-8') for s in row] + row = [str(s).encode('utf-8') for s in row] writer.writerow(row) diff --git a/servo/management/commands/cron.py b/servo/management/commands/cron.py index eaed8bd..225a991 100755 --- a/servo/management/commands/cron.py +++ b/servo/management/commands/cron.py @@ -38,7 +38,7 @@ def send_table(sender, recipient, subject, table, send_empty=False): settings.EMAIL_HOST_USER = config.get('smtp_user') settings.EMAIL_HOST_PASSWORD = config.get('smtp_password') - send_mail(subject, unicode(table), sender, [recipient], fail_silently=False) + send_mail(subject, str(table), sender, [recipient], fail_silently=False) class Command(BaseCommand): diff --git a/servo/models/common.py b/servo/models/common.py index 1d3e17b..b1b756f 100644 --- a/servo/models/common.py +++ b/servo/models/common.py @@ -49,7 +49,7 @@ class CsvTable(object): """Pad row to self.colwdith""" r = [] for c in row: - r.append(unicode(c).ljust(self.colwidth)) + r.append(str(c).ljust(self.colwidth)) return r @@ -70,7 +70,7 @@ class CsvTable(object): return self.table def __str__(self): - return unicode(self).encode('utf-8') + return str(self).encode('utf-8') diff --git a/servo/models/note.py b/servo/models/note.py index e7ab2dd..5650c4c 100644 --- a/servo/models/note.py +++ b/servo/models/note.py @@ -291,6 +291,11 @@ class Note(MPTTModel): Creates a new Note from an email message """ sender = decode_header(msg['From']) + + if type(sender[0][0]) == str: + enc = sender[0][1] or 'utf-8' + sender = [[sender[0][0].encode(enc), enc]] + detected = chardet.detect(sender[0][0]).get('encoding') sender = [i[0].decode(i[1] or detected) for i in sender] sender = ' '.join(sender) @@ -300,20 +305,28 @@ class Note(MPTTModel): note.is_read = False note.is_reported = False note.recipient = msg['To'] + note.type = cls.T_CUSTOMER_NOTE subject = decode_header(msg['Subject'])[0] + + if type(subject[0][0]) == str: + enc = subject[1] or 'utf-8' + subject = [subject[0].encode(enc), enc] + detected = chardet.detect(subject[0]).get('encoding') - note.subject = subject[0].decode(subject[1] or detected) + encoding = subject[1] or detected + note.subject = subject[0].decode(encoding) note.find_parent(note.subject) for part in msg.walk(): + t, s = part.get_content_type().split('/', 1) charset = part.get_content_charset() or "latin1" if t == "text": payload = part.get_payload(decode=True) - note.body = unicode(payload, str(charset), "ignore") + note.body = payload.decode(charset) if s == "html": h = html2text.HTML2Text() h.ignore_images = True @@ -321,7 +334,7 @@ class Note(MPTTModel): else: note.save() if part.get_filename(): - filename = unicode(part.get_filename()) + filename = part.get_filename() payload = part.get_payload() content = base64.b64decode(payload) content = ContentFile(content, filename) diff --git a/servo/views/product.py b/servo/views/product.py index d2c6750..e95dbc3 100644 --- a/servo/views/product.py +++ b/servo/views/product.py @@ -159,7 +159,7 @@ def get_inventory_report(request): inventory, codemap = {}, {} for k in cursor.fetchall(): - product_id = unicode(k[0]) + product_id = str(k[0]) codemap[product_id] = k[1] # map product IDs to product codes inv_slot = {k[2]: k[3]} @@ -175,7 +175,7 @@ def get_inventory_report(request): amount = p.get(i, '0') # fill empty inventory slots with zeros inventory_cols.append(amount) - code = unicode(codemap[k]) + code = str(codemap[k]) row = [k, code] + inventory_cols response.write("\t".join(row) + "\n") @@ -200,9 +200,9 @@ def download_products(request, group="all"): # @FIXME: Add total stocked amount to product # currently the last column is a placeholder for stock counts in inventory uploads for p in products: - row = [unicode(i) for i in (p.pk, p.code, p.title, - p.price_purchase_stock, - p.price_sales_stock, 0)] + row = [str(i) for i in (p.pk, p.code, p.title, + p.price_purchase_stock, + p.price_sales_stock, 0)] data += "\t".join(row) + "\n" return send_csv(data, filename) -- cgit v1.2.3