From a94d400d03a4c572dd7dd8b47cf30b7a8219552f Mon Sep 17 00:00:00 2001 From: Gaetan Delannay Date: Mon, 29 Apr 2013 21:32:05 +0200 Subject: [PATCH] [pod] Bugfix: error when a pod template does not have a 'mimetype' file inside (https://bugs.launchpad.net/bugs/1173009); bugfix: error when parsing some ods pod templates: some cells do not have attribute 'office:value-type' (see bug https://bugs.launchpad.net/bugs/1173301). [gen] Added a basic integration of gen applications with Google Analytics. --- gen/__init__.py | 2 ++ gen/generator.py | 1 + gen/mixins/ToolMixin.py | 25 +++++++++++++++++++++++++ gen/templates/config.pyt | 1 + gen/ui/template.pt | 3 +++ pod/pod_parser.py | 1 + pod/renderer.py | 9 +++++++-- 7 files changed, 40 insertions(+), 2 deletions(-) diff --git a/gen/__init__.py b/gen/__init__.py index 8e9365c..e0ac30a 100644 --- a/gen/__init__.py +++ b/gen/__init__.py @@ -3013,4 +3013,6 @@ class Config: # When using Ogone, place an instance of appy.gen.ogone.OgoneConfig in # the field below. self.ogone = None + # When using Google analytics, specify here the Analytics ID + self.googleAnalyticsId = None # ------------------------------------------------------------------------------ diff --git a/gen/generator.py b/gen/generator.py index cdeb732..863a8c4 100644 --- a/gen/generator.py +++ b/gen/generator.py @@ -605,6 +605,7 @@ class ZopeGenerator(Generator): repls['sourceLanguage'] = self.config.sourceLanguage repls['enableSessionTimeout'] = self.config.enableSessionTimeout repls['ogone'] = repr(self.config.ogone) + repls['googleAnalyticsId'] = repr(self.config.googleAnalyticsId) repls['activateForgotPassword'] = self.config.activateForgotPassword self.copyFile('config.pyt', repls, destName='config.py') diff --git a/gen/mixins/ToolMixin.py b/gen/mixins/ToolMixin.py index 4a0fd51..a401871 100644 --- a/gen/mixins/ToolMixin.py +++ b/gen/mixins/ToolMixin.py @@ -1222,4 +1222,29 @@ class ToolMixin(BaseMixin): else: objects = method.__get__(tool)(tool) return [(o.uid, o) for o in objects] + + def getGoogleAnalyticsCode(self): + '''If the config defined a Google Analytics ID, this method returns the + Javascript code to be included in every page, allowing Google + Analytics to work.''' + # Disable Google Analytics when we are in debug mode. + if self.isDebug(): return + # Disable Google Analytics if no ID is found in the config. + gaId = self.getProductConfig().googleAnalyticsId + if not gaid: return + # Google Analytics must be enabled: return the chunk of Javascript + # code specified by Google. + code = "var _gaq = _gaq || [];\n" \ + "_gaq.push(['_setAccount', '%s']);\n" \ + "_gaq.push(['_trackPageview']);\n" \ + "(function() {\n" \ + " var ga = document.createElement('script'); " \ + "ga.type = 'text/javascript'; ga.async = true;\n" \ + " ga.src = ('https:' == document.location.protocol ? " \ + "'https://ssl' : 'http://www') + " \ + "'.google-analytics.com/ga.js';\n" \ + " var s = document.getElementsByTagName('script')[0]; " \ + "s.parentNode.insertBefore(ga, s);\n" \ + "})();\n" % gaId + return code # ------------------------------------------------------------------------------ diff --git a/gen/templates/config.pyt b/gen/templates/config.pyt index 6da84f5..e0cb77e 100644 --- a/gen/templates/config.pyt +++ b/gen/templates/config.pyt @@ -50,6 +50,7 @@ sourceLanguage = '' activateForgotPassword = enableSessionTimeout = ogone = +googleAnalyticsId = # When Zope is starting or runs in test mode, there is no request object. We # create here a fake one for storing Appy wrappers. diff --git a/gen/ui/template.pt b/gen/ui/template.pt index 2143089..0724446 100644 --- a/gen/ui/template.pt +++ b/gen/ui/template.pt @@ -34,6 +34,9 @@ + Google Analytics stuff, if enabled + Grey background shown when popups are shown
diff --git a/pod/pod_parser.py b/pod/pod_parser.py index a13e333..a1f5322 100644 --- a/pod/pod_parser.py +++ b/pod/pod_parser.py @@ -248,6 +248,7 @@ class PodParser(OdfParser): e.exprHasStyle = False elif (elem == e.tags['table-cell']) and \ attrs.has_key(e.tags['formula']) and \ + attrs.has_key(e.tags['value-type']) and \ (attrs[e.tags['value-type']] == 'string') and \ attrs[e.tags['formula']].startswith('of:="'): # In an ODS template, any cell containing a formula of type "string" diff --git a/pod/renderer.py b/pod/renderer.py index 55677f4..508c6fe 100644 --- a/pod/renderer.py +++ b/pod/renderer.py @@ -540,8 +540,13 @@ class Renderer: # Linux/Unix, are unable to detect the correct mimetype for a pod result # (it simply recognizes it as a "application/zip" and not a # "application/vnd.oasis.opendocument.text)". - resultZip.write(os.path.join(self.unzipFolder, 'mimetype'), - 'mimetype', zipfile.ZIP_STORED) + mimetypeFile = os.path.join(self.unzipFolder, 'mimetype') + # This file may not exist (presumably, ods files from Google Drive) + if not os.path.exists(mimetypeFile): + f = open(mimetypeFile, 'w') + f.write(mimeTypes[resultExt]) + f.close() + resultZip.write(mimetypeFile, 'mimetype', zipfile.ZIP_STORED) for dir, dirnames, filenames in os.walk(self.unzipFolder): for f in filenames: folderName = dir[len(self.unzipFolder)+1:]