From d3a2b85a10a150988ea41006e4e1b205ca83882a Mon Sep 17 00:00:00 2001 From: Gaetan Delannay Date: Wed, 9 May 2012 09:45:15 +0200 Subject: [PATCH] appy.gen: bugfixes. --- gen/__init__.py | 4 ++-- gen/mixins/__init__.py | 3 ++- shared/utils.py | 40 ++++++++++++++++++++++++++-------------- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/gen/__init__.py b/gen/__init__.py index e6fb0f2..a9441f8 100644 --- a/gen/__init__.py +++ b/gen/__init__.py @@ -12,7 +12,7 @@ import appy.pod from appy.pod.renderer import Renderer from appy.shared.data import countries from appy.shared.utils import Traceback, getOsTempFolder, formatNumber, \ - cleanXhtml, FileWrapper, sequenceTypes + XhtmlCleaner, FileWrapper, sequenceTypes # Default Appy permissions ----------------------------------------------------- r, w, d = ('read', 'write', 'delete') @@ -1239,7 +1239,7 @@ class String(Type): # (ie for image size when images are resized). So in this case we # can't remove style-related information. keepStyles = self.allowImageUpload or self.richText - value = cleanXhtml(value, keepStyles=keepStyles) + value = XhtmlCleaner.clean(value, keepStyles=keepStyles) Type.store(self, obj, value) def getFormattedValue(self, obj, value): diff --git a/gen/mixins/__init__.py b/gen/mixins/__init__.py index a77ace7..c932738 100644 --- a/gen/mixins/__init__.py +++ b/gen/mixins/__init__.py @@ -340,7 +340,8 @@ class BaseMixin: obj = self.getTool().getObject(rq['objectUid']) else: obj = self - return obj.getMethod('on'+action)() + if rq.get('appy', None) == '1': obj = obj.appy() + return getattr(obj, 'on'+action)() def rememberPreviousData(self): '''This method is called before updating an object and remembers, for diff --git a/shared/utils.py b/shared/utils.py index d5331a1..914e4d7 100644 --- a/shared/utils.py +++ b/shared/utils.py @@ -193,7 +193,7 @@ def executeCommand(cmd): return res # ------------------------------------------------------------------------------ -unwantedChars = ('\\', '/', ':', '*', '?', '"', '<', '>', '|', ' ', '\t') +unwantedChars = ('\\', '/', ':', '*', '?', '"', '<', '>', '|', ' ', '\t', "'") alphaRex = re.compile('[a-zA-Z]') alphanumRex = re.compile('[a-zA-Z0-9]') def normalizeString(s, usage='fileName'): @@ -264,21 +264,33 @@ def formatNumber(n, sep=',', precision=2, tsep=' '): return res # ------------------------------------------------------------------------------ -xhtmlClassAttr = re.compile('class\s*=\s*".*?"') -xhtmlStyleAttr = re.compile('style\s*=\s*".*?"') -xhtmlComment = re.compile('', re.S) +class XhtmlCleaner: + # Regular expressions used for cleaning. + classAttr = re.compile('class\s*=\s*".*?"') + comment = re.compile('', re.S) -def cleanXhtml(s, keepStyles=False): - '''Returns a version of XHTML string p_s where: - * attributes "class" and "style" have been removed (only if p_keepStyles - is False); - * XHTML comments have been removed. + '''This class has 2 objectives: + + 1. The main objective is to format XHTML p_s to be storable in the ZODB + according to Appy rules. + a. Every

or

  • must be on a single line (ending with a carriage + return); else, appy.shared.diff will not be able to compute XHTML + diffs; + b. Optimize size: HTML comments are removed. + + 2. If p_keepStyles (or m_clean) is False, some style-related information + will be removed, in order to get a standardized content that can be + dumped in an elegant and systematic manner into a POD template. ''' - if not keepStyles: - s = xhtmlClassAttr.sub('', s) - s = xhtmlStyleAttr.sub('', s) - s = xhtmlComment.sub('', s) - return s + @classmethod + def clean(klass, s, keepStyles=False): + '''Returns the cleaned variant of p_s.''' + if not keepStyles: + # Format p_s according to objective 2. + s = klass.classAttr.sub('', s) + # Format p_s according to objective 1. + s = klass.comment.sub('', s) + return s # ------------------------------------------------------------------------------ def lower(s):