Added UserVoice API
authorThierry Florac <thierry.florac@onf.fr>
Wed, 02 Jul 2014 10:30:28 +0200
changeset 52 3d75d7c04593
parent 51 dbed1ff1e070
child 53 bc3aa1f34e2d
Added UserVoice API
src/ztfy/myams/configuration.py
src/ztfy/myams/interfaces/configuration.py
src/ztfy/myams/templates/layout.pt
--- a/src/ztfy/myams/configuration.py	Wed Jul 02 10:30:01 2014 +0200
+++ b/src/ztfy/myams/configuration.py	Wed Jul 02 10:30:28 2014 +0200
@@ -12,20 +12,21 @@
 
 # import local interfaces
 from zope.annotation.interfaces import IAnnotations
-from zope.location.location import locate
 from ztfy.myams.interfaces import IMyAMSApplication
-from ztfy.myams.interfaces.configuration import IMyAMSStaticConfiguration, \
-    IMyAMSConfigurationTarget, IMyAMSConfiguration
+from ztfy.myams.interfaces.configuration import IMyAMSStaticConfiguration, IMyAMSConfigurationTarget, IMyAMSConfiguration
 
 # import Zope3 packages
 from zope.component import adapter, queryUtility
 from zope.container.contained import Contained
 from zope.interface import implementer, implements
+from zope.location.location import locate
 from zope.schema.fieldproperty import FieldProperty
 from ztfy.extfile.blob import BlobImage, BlobFile
 from ztfy.file.property import FileProperty
 
 # import local packages
+from ztfy.mail.adapter import getPrincipalAddress
+from ztfy.utils.request import queryRequest
 from ztfy.utils.traversing import getParent
 
 
@@ -36,6 +37,7 @@
 
     application_package = 'ztfy.myams'
     application_name = u'MyAMS'
+    version_location = u'menus'
     include_top_links = True
     include_site_search = True
     include_mobile_search = True
@@ -68,6 +70,7 @@
     custom_css = FileProperty(IMyAMSConfiguration['custom_css'], klass=BlobFile)
     custom_js = FileProperty(IMyAMSConfiguration['custom_js'], klass=BlobFile)
     google_analytics_key = FieldProperty(IMyAMSConfiguration['google_analytics_key'])
+    uservoice_api_key = FieldProperty(IMyAMSConfiguration['uservoice_api_key'])
 
     @property
     def static_configuration(self):
@@ -75,6 +78,16 @@
         if application is not None:
             return queryUtility(IMyAMSStaticConfiguration, name=application.configuration_name)
 
+    @property
+    def user_email(self):
+        request = queryRequest()
+        if request is None:
+            return None
+        address = getPrincipalAddress(request.principal)
+        if not address:
+            return None
+        return address[0][1]
+
 
 MYAMS_CONFIGURATION_KEY = 'ztfy.myams.configuration'
 
--- a/src/ztfy/myams/interfaces/configuration.py	Wed Jul 02 10:30:01 2014 +0200
+++ b/src/ztfy/myams/interfaces/configuration.py	Wed Jul 02 10:30:28 2014 +0200
@@ -12,7 +12,7 @@
 
 # import Zope3 packages
 from zope.interface import Interface, Attribute
-from zope.schema import TextLine, Bool
+from zope.schema import TextLine, Bool, Choice
 
 # import local packages
 from ztfy.file.schema import ImageField, FileField
@@ -33,6 +33,11 @@
 
     version = Attribute(_("Application version"))
 
+    version_location = Choice(title=_("Version location"),
+                              required=False,
+                              values=(u'menus', u'shortcuts'),
+                              default=u'menus')
+
     include_top_links = Bool(title=_("Include top links?"),
                              default=True,
                              required=True)
@@ -117,8 +122,14 @@
     google_analytics_key = TextLine(title=_("Google Analytics key"),
                                     required=False)
 
+    uservoice_api_key = TextLine(title=_("UserVoice API key"),
+                                 description=_("This is the name of UserVoice javascript file"),
+                                 required=False)
+
     static_configuration = Attribute(_("Application static configuration utility"))
 
+    user_email = Attribute(_("Current user email address"))
+
 
 class IMyAMSConfigurationTarget(Interface):
     """MyAMS configuration marker interface"""
--- a/src/ztfy/myams/templates/layout.pt	Wed Jul 02 10:30:01 2014 +0200
+++ b/src/ztfy/myams/templates/layout.pt	Wed Jul 02 10:30:28 2014 +0200
@@ -90,7 +90,12 @@
 							<span class="login-name" tal:content="request/principal/title">FLORAC Thierry</span>
 							<i class="fa fa-angle-down" tal:condition="display_shortcuts"></i>
 						</a>
-						<div id="shortcut" tal:condition="display_shortcuts" tal:content="structure shortcuts"></div>
+						<div id="shortcut" tal:condition="display_shortcuts">
+							<tal:var content="structure shortcuts" />
+							<div tal:condition="python:static.version_location == 'shortcuts'" class="version">
+								<tal:var content="static/application_name" /> - version <tal:var content="static/version" />
+							</div>
+						</div>
 					</tal:var>
 				</span>
 
@@ -125,7 +130,7 @@
 		</tal:if>
 
 		<!-- Application version -->
-		<div class="version">
+		<div tal:condition="python:static.version_location == 'menus'" class="version">
 			<tal:var content="static/application_name" /> - version <tal:var content="static/version" />
 		</div>
 	</aside>
@@ -179,5 +184,40 @@
 		''' % {'key': config.google_analytics_key}"></script>
 		<!-- end Google Analytics -->
 	</tal:if>
+
+	<tal:if condition="config/uservoice_api_key">
+		<!-- UserVoice -->
+		<script type="text/javascript" tal:content="python:'''
+
+			UserVoice = window.UserVoice || [];
+			(function() {
+				var uv=document.createElement('script');
+				uv.type='text/javascript';
+				uv.async=true;
+				uv.src='//widget.uservoice.com/%(key)s.js';
+				var s=document.getElementsByTagName('script')[0];
+				s.parentNode.insertBefore(uv,s);
+			})();
+
+			UserVoice.push(['set', {
+				accent_color: '#448dd6',
+				trigger_color: 'white',
+				trigger_background_color: '#e23a39'
+			}]);
+
+			UserVoice.push(['identify', {
+				email: '%(mail)s',
+				name:  '%(name)s'
+			}]);
+
+			UserVoice.push(['addTrigger', { mode: 'contact', trigger_position: 'top-left' }]);
+
+			UserVoice.push(['autoprompt', {}]);
+		''' % {'key': config.uservoice_api_key,
+			   'name': request.principal.title,
+			   'mail': config.user_email}"></script>
+		<!-- end UserVoice -->
+	</tal:if>
+
 </body>
 </html>