1 | from zope.interface import implements |
---|
2 | from zope.component import queryMultiAdapter, getMultiAdapter |
---|
3 | |
---|
4 | from plone.memoize import ram |
---|
5 | from plone.memoize.instance import memoize |
---|
6 | from plone.memoize.compress import xhtml_compress |
---|
7 | |
---|
8 | from plone.app.portlets.portlets import base |
---|
9 | from plone.app.portlets.cache import render_cachekey |
---|
10 | from plone.portlets.interfaces import IPortletDataProvider |
---|
11 | |
---|
12 | from zope import schema |
---|
13 | from zope.formlib import form |
---|
14 | |
---|
15 | from Acquisition import aq_inner |
---|
16 | from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
---|
17 | |
---|
18 | from quintagroup.portlet.map import QGMapPortletMessageFactory as _ |
---|
19 | |
---|
20 | from logging import getLogger |
---|
21 | logger = getLogger("Plone") |
---|
22 | |
---|
23 | class IQGMapPortlet(IPortletDataProvider): |
---|
24 | """A portlet |
---|
25 | |
---|
26 | It inherits from IPortletDataProvider because for this portlet, the |
---|
27 | data that is being rendered and the portlet assignment itself are the |
---|
28 | same. |
---|
29 | """ |
---|
30 | |
---|
31 | # TODO: Add any zope.schema fields here to capture portlet configuration |
---|
32 | # information. Alternatively, if there are no settings, leave this as an |
---|
33 | # empty interface - see also notes around the add form and edit form |
---|
34 | # below. |
---|
35 | |
---|
36 | collection_path = schema.TextLine(title=_(u"Path to collection"), |
---|
37 | description=_(u"A plone physical path to collection of locations"), |
---|
38 | required=True) |
---|
39 | |
---|
40 | |
---|
41 | class Assignment(base.Assignment): |
---|
42 | """Portlet assignment. |
---|
43 | |
---|
44 | This is what is actually managed through the portlets UI and associated |
---|
45 | with columns. |
---|
46 | """ |
---|
47 | |
---|
48 | implements(IQGMapPortlet) |
---|
49 | |
---|
50 | # TODO: Set default values for the configurable parameters here |
---|
51 | |
---|
52 | def __init__(self, collection_path=""): |
---|
53 | self.collection_path = str(collection_path) |
---|
54 | |
---|
55 | @property |
---|
56 | def title(self): |
---|
57 | """This property is used to give the title of the portlet in the |
---|
58 | "manage portlets" screen. |
---|
59 | """ |
---|
60 | return "Quintagroup Google Map portlet" |
---|
61 | |
---|
62 | |
---|
63 | class Renderer(base.Renderer): |
---|
64 | """Portlet renderer. |
---|
65 | |
---|
66 | This is registered in configure.zcml. The referenced page template is |
---|
67 | rendered, and the implicit variable 'view' will refer to an instance |
---|
68 | of this class. Other methods can be added and referenced in the template. |
---|
69 | """ |
---|
70 | |
---|
71 | _template = ViewPageTemplateFile('qgmapportlet.pt') |
---|
72 | |
---|
73 | def __init__(self, *args): |
---|
74 | base.Renderer.__init__(self, *args) |
---|
75 | context = aq_inner(self.context) |
---|
76 | portal_state = getMultiAdapter((context, self.request), name=u'plone_portal_state') |
---|
77 | self.portal = portal_state.portal() |
---|
78 | self.gmapview = queryMultiAdapter((self.collection, self.request), |
---|
79 | name='maps_googlemaps_enabled_view') |
---|
80 | #self._data = self.collection |
---|
81 | |
---|
82 | #@ram.cache(render_cachekey) |
---|
83 | def render(self): |
---|
84 | return xhtml_compress(self._template()) |
---|
85 | |
---|
86 | @property |
---|
87 | def available(self): |
---|
88 | return bool(self.gmapview and \ |
---|
89 | self.gmapview.enabled and \ |
---|
90 | self.gmapview.getMarkers()) |
---|
91 | |
---|
92 | @property |
---|
93 | def collection(self): |
---|
94 | try: |
---|
95 | return self.portal.restrictedTraverse(self.data.collection_path) |
---|
96 | except: |
---|
97 | return None |
---|
98 | |
---|
99 | |
---|
100 | class AddForm(base.AddForm): |
---|
101 | form_fields = form.Fields(IQGMapPortlet) |
---|
102 | label = _(u"Add Quintagroup Map Portlet") |
---|
103 | description = _(u"This portlet displays Locations from the collection on the Google Map.") |
---|
104 | |
---|
105 | def create(self, data): |
---|
106 | portal_state = getMultiAdapter((self.context, self.request), |
---|
107 | name=u'plone_portal_state') |
---|
108 | default_path = '/'.join(portal_state.portal().getPhysicalPath()) + '/events' |
---|
109 | return Assignment(collection_path=data.get('collection_path', default_path)) |
---|
110 | |
---|
111 | class EditForm(base.EditForm): |
---|
112 | form_fields = form.Fields(IQGMapPortlet) |
---|
113 | label = _(u"Edit Quintagroup Map Portlet") |
---|
114 | description = _(u"This portlet displays Locations from the collection on the Google Map.") |
---|