From 6837da09173c0704221e1403a263ed2a1b0a4029 Mon Sep 17 00:00:00 2001 From: Massimo Di Pierro Date: Fri, 20 Jan 2012 11:36:42 -0600 Subject: [PATCH] issue 626 fixes unicode and truncattion, thanks mweissen --- VERSION | 2 +- gluon/html.py | 6 ++++++ gluon/sqlhtml.py | 22 +++++++++------------- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/VERSION b/VERSION index 34d96c41..cf5a969d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 1.99.4 (2012-01-20 11:23:12) stable +Version 1.99.4 (2012-01-20 11:36:35) stable diff --git a/gluon/html.py b/gluon/html.py index 83c485cd..ee98d17b 100644 --- a/gluon/html.py +++ b/gluon/html.py @@ -124,6 +124,12 @@ def xmlescape(data, quote = True): return data +def truncate_string(text, length, dots='...'): + text = text.decode('utf-8') + if len(text)>length: + text = text[:length-len(dots)].encode('utf-8')+dots + return text + def URL( a=None, c=None, diff --git a/gluon/sqlhtml.py b/gluon/sqlhtml.py index 9acda6f3..1fa2a48c 100644 --- a/gluon/sqlhtml.py +++ b/gluon/sqlhtml.py @@ -18,7 +18,7 @@ from http import HTTP from html import XML, SPAN, TAG, A, DIV, CAT, UL, LI, TEXTAREA, BR, IMG, SCRIPT from html import FORM, INPUT, LABEL, OPTION, SELECT, MENU from html import TABLE, THEAD, TBODY, TR, TD, TH -from html import URL +from html import URL, truncate_string from dal import DAL, Table, Row, CALLABLETYPES, smart_query from storage import Storage from utils import md5_hash @@ -1800,11 +1800,11 @@ class SQLFORM(FORM): value = A('File', _href='%s/%s' % (upload, value)) else: - value = '' - elif isinstance(value,str) and len(value)>maxlength: - value=value[:maxlength]+'...' + value = '' + elif isinstance(value,str): + value = truncate_string(value,maxlength) else: - value=field.formatter(value) + value = field.formatter(value) tr.append(TD(value)) row_buttons = TD(_class='row_buttons') if links and links_in_grid: @@ -2195,16 +2195,12 @@ class SQLTABLE(TABLE): r = '' elif field.type in ['string','text']: r = str(field.formatter(r)) - ur = unicode(r, 'utf8') if headers!={}: #new implement dict if isinstance(headers[colname],dict): - if isinstance(headers[colname]['truncate'], int) \ - and len(ur)>headers[colname]['truncate']: - r = ur[:headers[colname]['truncate'] - 3] - r = r.encode('utf8') + '...' - elif not truncate is None and len(ur) > truncate: - r = ur[:truncate - 3].encode('utf8') + '...' - + if isinstance(headers[colname]['truncate'], int): + r = truncate_string(r, headers[colname]['truncate']) + elif not truncate is None: + r = truncate_string(r, truncate) attrcol = dict()#new implement dict if headers!={}: if isinstance(headers[colname],dict):