appy.pod: added param 'stylesMapping' for the 'text' function (which calls 'xhtml' internally); appy.gen: bufgix in the translation system (translation of group-related labels); appy.shared.dav: bugfix while getting the 'content-type' HTTP header key; appy.shared.dav: smart error handling when parsing wrong XML content.

This commit is contained in:
Gaetan Delannay 2011-04-15 11:26:20 +02:00
parent 36237c3ee5
commit 4e848ce0a8
3 changed files with 30 additions and 15 deletions

View file

@ -1210,15 +1210,20 @@ class BaseMixin:
if field: if field:
# Maybe we do not have the field itself, but only its name # Maybe we do not have the field itself, but only its name
if isinstance(field, basestring): if isinstance(field, basestring):
field = self.getAppyType(field, className=className) appyField = self.getAppyType(field, className=className)
# Include field-specific mapping if any. else:
fieldMapping = field.mapping[label] appyField = field
if fieldMapping: if appyField:
if callable(fieldMapping): fieldMapping = appyField.mapping[label]
fieldMapping = field.callMethod(self, fieldMapping) if fieldMapping:
mapping.update(fieldMapping) if callable(fieldMapping):
# Get the label fieldMapping=appyField.callMethod(self,fieldMapping)
label = getattr(field, label+'Id') mapping.update(fieldMapping)
# Get the label
label = getattr(appyField, label+'Id')
else:
# It must be a group.
label = '%s_group_%s_%s' % (self.meta_type, field, label)
# We will get the translation from a Translation object. # We will get the translation from a Translation object.
# In what language must we get the translation? # In what language must we get the translation?
if not language: language = self.getUserLanguage() if not language: language = self.getUserLanguage()

View file

@ -239,11 +239,11 @@ class Renderer:
return Xhtml2OdtConverter(xhtmlContent, encoding, self.stylesManager, return Xhtml2OdtConverter(xhtmlContent, encoding, self.stylesManager,
stylesMapping, ns).run() stylesMapping, ns).run()
def renderText(self, text, encoding='utf-8'): def renderText(self, text, encoding='utf-8', stylesMapping={}):
'''Method that can be used (under the name 'text') into a pod template '''Method that can be used (under the name 'text') into a pod template
for inserting a text containing carriage returns.''' for inserting a text containing carriage returns.'''
text = text.replace('\r\n', '<br/>').replace('\n', '<br/>') text = text.replace('\r\n', '<br/>').replace('\n', '<br/>')
return self.renderXhtml(text, encoding) return self.renderXhtml(text, encoding, stylesMapping)
def evalIfExpression(self, condition, ifTrue, ifFalse): def evalIfExpression(self, condition, ifTrue, ifFalse):
'''This method implements the method 'test' which is proposed in the '''This method implements the method 'test' which is proposed in the

View file

@ -1,5 +1,5 @@
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
import os, re, httplib, sys, stat, urlparse, time, socket import os, re, httplib, sys, stat, urlparse, time, socket, xml.sax
from urllib import quote from urllib import quote
from StringIO import StringIO from StringIO import StringIO
from mimetypes import guess_type from mimetypes import guess_type
@ -87,7 +87,14 @@ class HttpResponse:
if self.duration: duration = ', got in %.4f seconds' % self.duration if self.duration: duration = ', got in %.4f seconds' % self.duration
return '<HttpResponse %s (%s)%s>' % (self.code, self.text, duration) return '<HttpResponse %s (%s)%s>' % (self.code, self.text, duration)
xmlHeaders = ('text/xml', 'application/xml') def extractContentType(self, contentType):
'''Extract the content type from the HTTP header, potentially removing
encoding-related data.'''
i = contentType.find(';')
if i != -1: return contentType[:i]
return contentType
xmlHeaders = ('text/xml', 'application/xml', 'application/soap+xml')
def extractData(self): def extractData(self):
'''This method extracts, from the various parts of the HTTP response, '''This method extracts, from the various parts of the HTTP response,
some useful information. For example, it will find the URI where to some useful information. For example, it will find the URI where to
@ -96,12 +103,15 @@ class HttpResponse:
if self.code == 302: if self.code == 302:
return urlparse.urlparse(self.headers['location'])[2] return urlparse.urlparse(self.headers['location'])[2]
elif self.headers.has_key('content-type'): elif self.headers.has_key('content-type'):
contentType = self.headers['content-type'] contentType = self.extractContentType(self.headers['content-type'])
for xmlHeader in self.xmlHeaders: for xmlHeader in self.xmlHeaders:
if contentType.startswith(xmlHeader): if contentType.startswith(xmlHeader):
# Return an unmarshalled version of the XML content, for # Return an unmarshalled version of the XML content, for
# easy use in Python. # easy use in Python.
return XmlUnmarshaller().parse(self.body) try:
return XmlUnmarshaller().parse(self.body)
except xml.sax.SAXParseException, se:
raise ResourceError('Invalid XML response (%s)'%str(se))
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
urlRex = re.compile(r'http://([^:/]+)(:[0-9]+)?(/.+)?', re.I) urlRex = re.compile(r'http://([^:/]+)(:[0-9]+)?(/.+)?', re.I)