Fix encoding issue when manually loading templates for forms

We used to manually load the template files for the edit pages using
turbogears.meta.load_kid_template(). Unfortunately this went through
the one code path where encoding was completely ignored. It ended up
defaulting to sys.getdefaultencoding() which is 'ascii'. So even though
most of the templates are loaded as 'utf-8' the few that really mattered
weren't.

The fix is to call kid.load_template() ourselves and set the encoding of
the class we just loaded to either the setting in the app.cfg file or
to the normal default value of 'utf-8'.

454076
This commit is contained in:
Rob Crittenden 2008-07-22 15:14:23 -04:00
parent cdba310f02
commit bae3a2101f
6 changed files with 40 additions and 7 deletions

View File

@ -17,6 +17,7 @@
import turbogears
from turbogears import validators, widgets
from ipagui.helpers import ipahelper
from ipagui.forms.user import UserFields
@ -101,7 +102,7 @@ class DelegateForm(widgets.Form):
def __init__(self, *args, **kw):
super(DelegateForm,self).__init__(*args, **kw)
(self.template_c, self.template) = widgets.meta.load_kid_template(
(self.template_c, self.template) = ipahelper.load_template(
"ipagui.templates.delegateform")
self.delegate_fields = DelegateFields

View File

@ -18,6 +18,7 @@
import turbogears
from turbogears import validators, widgets
from tg_expanding_form_widget.tg_expanding_form_widget import ExpandingForm
from ipagui.helpers import ipahelper
class GroupFields(object):
cn = widgets.TextField(name="cn", label="Name")
@ -50,7 +51,7 @@ class GroupNewForm(widgets.Form):
def __init__(self, *args, **kw):
super(GroupNewForm,self).__init__(*args, **kw)
(self.template_c, self.template) = widgets.meta.load_kid_template("ipagui.templates.groupnewform")
(self.template_c, self.template) = ipahelper.load_template("ipagui.templates.groupnewform")
self.group_fields = GroupFields
def update_params(self, params):
@ -80,5 +81,5 @@ class GroupEditForm(widgets.Form):
def __init__(self, *args, **kw):
super(GroupEditForm,self).__init__(*args, **kw)
(self.template_c, self.template) = widgets.meta.load_kid_template("ipagui.templates.groupeditform")
(self.template_c, self.template) = ipahelper.load_template("ipagui.templates.groupeditform")
self.group_fields = GroupFields

View File

@ -18,6 +18,7 @@
import turbogears
from turbogears import validators, widgets
from tg_expanding_form_widget.tg_expanding_form_widget import ExpandingForm
from ipagui.helpers import ipahelper
class IPAPolicyFields(object):
# From cn=ipaConfig
@ -78,7 +79,7 @@ class IPAPolicyForm(widgets.Form):
def __init__(self, *args, **kw):
super(IPAPolicyForm,self).__init__(*args, **kw)
(self.template_c, self.template) = widgets.meta.load_kid_template(
(self.template_c, self.template) = ipahelper.load_template(
"ipagui.templates.ipapolicyeditform")
self.ipapolicy_fields = IPAPolicyFields

View File

@ -18,6 +18,7 @@
import turbogears
from turbogears import validators, widgets
from tg_expanding_form_widget.tg_expanding_form_widget import ExpandingForm
from ipagui.helpers import ipahelper
class PrincipalFields(object):
hostname = widgets.TextField(name="hostname", label="Host Name")
@ -50,7 +51,7 @@ class PrincipalNewForm(widgets.Form):
def __init__(self, *args, **kw):
super(PrincipalNewForm,self).__init__(*args, **kw)
(self.template_c, self.template) = widgets.meta.load_kid_template("ipagui.templates.principalnewform")
(self.template_c, self.template) = ipahelper.load_template("ipagui.templates.principalnewform")
self.principal_fields = PrincipalFields
def update_params(self, params):

View File

@ -19,6 +19,7 @@ import turbogears
from turbogears import validators, widgets
from tg_expanding_form_widget.tg_expanding_form_widget import ExpandingForm
from ipagui.helpers.validators import *
from ipagui.helpers import ipahelper
class UserFields(object):
givenname = widgets.TextField(name="givenname", label="First Name")
@ -120,7 +121,8 @@ class UserNewForm(widgets.Form):
def __init__(self, *args, **kw):
super(UserNewForm,self).__init__(*args, **kw)
(self.template_c, self.template) = widgets.meta.load_kid_template("ipagui.templates.usernewform")
(self.template_c, self.template) = ipahelper.load_template("ipagui.templates.usernewform")
self.user_fields = UserFields
def update_params(self, params):
@ -172,7 +174,8 @@ class UserEditForm(widgets.Form):
def __init__(self, *args, **kw):
super(UserEditForm,self).__init__(*args, **kw)
(self.template_c, self.template) = widgets.meta.load_kid_template("ipagui.templates.usereditform")
(self.template_c, self.template) = ipahelper.load_template("ipagui.templates.usereditform")
self.user_fields = UserFields

View File

@ -17,6 +17,10 @@
import re
import logging
import turbogears
import kid
from turbokid import kidsupport
from pkg_resources import resource_filename
def javascript_string_escape(input):
"""Escapes the ' " and \ characters in a string so
@ -60,3 +64,25 @@ def fix_incoming_fields(fields, fieldname, multifieldname):
logging.warn("fix_incoming_fields error: " + str(e))
return fields
def load_template(classname, encoding=None):
"""
Loads the given template. This only handles .kid files.
Returns a tuple (compiled_tmpl, None) to emulate
turbogears.meta.load_kid_template() which ends up not properly handling
encoding.
"""
if not encoding:
encoding = turbogears.config.get('kid.encoding', kidsupport.KidSupport.assume_encoding)
divider = classname.rfind(".")
package, basename = classname[:divider], classname[divider+1:]
file_path = resource_filename(package, basename + ".kid")
tclass = kid.load_template(
file_path,
name = classname,
).Template
tclass.serializer = kid.HTMLSerializer(encoding=encoding)
tclass.assume_encoding=encoding
return (tclass, None)