1 | import string |
---|
2 | from zope.interface import implements |
---|
3 | from zope.component import queryMultiAdapter, getMultiAdapter |
---|
4 | |
---|
5 | from plone.memoize import ram |
---|
6 | from plone.memoize.instance import memoize |
---|
7 | from plone.memoize.compress import xhtml_compress |
---|
8 | |
---|
9 | from plone.app.portlets.portlets import base |
---|
10 | from plone.app.portlets.cache import render_cachekey |
---|
11 | from plone.portlets.interfaces import IPortletDataProvider |
---|
12 | |
---|
13 | from zope import schema |
---|
14 | from zope.formlib import form |
---|
15 | |
---|
16 | from Acquisition import aq_inner |
---|
17 | from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
---|
18 | |
---|
19 | from quintagroup.portlet.map import QGMapPortletMessageFactory as _ |
---|
20 | |
---|
21 | from logging import getLogger |
---|
22 | logger = getLogger("Plone") |
---|
23 | |
---|
24 | class IQGMapPortlet(IPortletDataProvider): |
---|
25 | """A portlet |
---|
26 | |
---|
27 | It inherits from IPortletDataProvider because for this portlet, the |
---|
28 | data that is being rendered and the portlet assignment itself are the |
---|
29 | same. |
---|
30 | """ |
---|
31 | |
---|
32 | # TODO: Add any zope.schema fields here to capture portlet configuration |
---|
33 | # information. Alternatively, if there are no settings, leave this as an |
---|
34 | # empty interface - see also notes around the add form and edit form |
---|
35 | # below. |
---|
36 | |
---|
37 | collection_path = schema.TextLine(title=_(u"Path to collection"), |
---|
38 | description=_(u"A plone physical path to collection of locations"), |
---|
39 | required=True) |
---|
40 | |
---|
41 | |
---|
42 | class Assignment(base.Assignment): |
---|
43 | """Portlet assignment. |
---|
44 | |
---|
45 | This is what is actually managed through the portlets UI and associated |
---|
46 | with columns. |
---|
47 | """ |
---|
48 | |
---|
49 | implements(IQGMapPortlet) |
---|
50 | |
---|
51 | # TODO: Set default values for the configurable parameters here |
---|
52 | |
---|
53 | def __init__(self, collection_path=""): |
---|
54 | self.collection_path = str(collection_path) |
---|
55 | |
---|
56 | @property |
---|
57 | def title(self): |
---|
58 | """This property is used to give the title of the portlet in the |
---|
59 | "manage portlets" screen. |
---|
60 | """ |
---|
61 | return "Quintagroup Google Map portlet" |
---|
62 | |
---|
63 | |
---|
64 | JS_TEMPLATE = string.Template(""" |
---|
65 | <script src="${portal_url}/maps-config.js" type="text/javascript"></script> |
---|
66 | <script src="${portal_url}/maps-googlemaps.js" type="text/javascript"></script> |
---|
67 | """) |
---|
68 | |
---|
69 | class Renderer(base.Renderer): |
---|
70 | """Portlet renderer. |
---|
71 | |
---|
72 | This is registered in configure.zcml. The referenced page template is |
---|
73 | rendered, and the implicit variable 'view' will refer to an instance |
---|
74 | of this class. Other methods can be added and referenced in the template. |
---|
75 | """ |
---|
76 | |
---|
77 | _template = ViewPageTemplateFile('qgmapportlet.pt') |
---|
78 | |
---|
79 | def __init__(self, *args): |
---|
80 | base.Renderer.__init__(self, *args) |
---|
81 | context = aq_inner(self.context) |
---|
82 | portal_state = getMultiAdapter((context, self.request), name=u'plone_portal_state') |
---|
83 | self.portal = portal_state.portal() |
---|
84 | self.portal_url = portal_state.portal_url() |
---|
85 | self.gmapEnView = queryMultiAdapter((self.collection, self.request), |
---|
86 | name='maps_googlemaps_enabled_view') |
---|
87 | self.gmapView = queryMultiAdapter((self.collection, self.request), |
---|
88 | name='maps_googlemaps_view') |
---|
89 | |
---|
90 | def render(self): |
---|
91 | return self.render_js() + self.render_html() |
---|
92 | |
---|
93 | @ram.cache(render_cachekey) |
---|
94 | def render_html(self): |
---|
95 | return xhtml_compress(self._template()) |
---|
96 | |
---|
97 | @property |
---|
98 | def available(self): |
---|
99 | return bool(self.gmapEnView and self.gmapView and \ |
---|
100 | self.gmapEnView.enabled and \ |
---|
101 | self._data()) |
---|
102 | |
---|
103 | @property |
---|
104 | def collection(self): |
---|
105 | try: |
---|
106 | return self.portal.restrictedTraverse(self.data.collection_path) |
---|
107 | except: |
---|
108 | return None |
---|
109 | |
---|
110 | @property |
---|
111 | def footer_url(self): |
---|
112 | collection_url = self.collection and self.collection.absolute_url() |
---|
113 | return collection_url and collection_url + '/maps_map' or '' |
---|
114 | |
---|
115 | @memoize |
---|
116 | def render_js(self): |
---|
117 | # JS block included only if it's not already present in html-header block: |
---|
118 | # for check use maps_googlemaps_enabled_view view |
---|
119 | contxtEnView = queryMultiAdapter((self.context, self.request), |
---|
120 | name='maps_googlemaps_enabled_view') |
---|
121 | if not (contxtEnView and contxtEnView.enabled): |
---|
122 | return JS_TEMPLATE.substitute({'portal_url':self.portal_url}) |
---|
123 | return "" |
---|
124 | |
---|
125 | @memoize |
---|
126 | def _data(self): |
---|
127 | if hasattr(self.collection, 'queryCatalog'): |
---|
128 | return self.collection.queryCatalog() |
---|
129 | return [] |
---|
130 | |
---|
131 | |
---|
132 | |
---|
133 | class AddForm(base.AddForm): |
---|
134 | form_fields = form.Fields(IQGMapPortlet) |
---|
135 | label = _(u"Add Quintagroup Map Portlet") |
---|
136 | description = _(u"This portlet displays Locations from the collection on the Google Map.") |
---|
137 | |
---|
138 | def create(self, data): |
---|
139 | portal_state = getMultiAdapter((self.context, self.request), |
---|
140 | name=u'plone_portal_state') |
---|
141 | default_path = '/'.join(portal_state.portal().getPhysicalPath()) + '/events' |
---|
142 | return Assignment(collection_path=data.get('collection_path', default_path)) |
---|
143 | |
---|
144 | class EditForm(base.EditForm): |
---|
145 | form_fields = form.Fields(IQGMapPortlet) |
---|
146 | label = _(u"Edit Quintagroup Map Portlet") |
---|
147 | description = _(u"This portlet displays Locations from the collection on the Google Map.") |
---|