[2206] | 1 | import string |
---|
[2188] | 2 | from zope.interface import implements |
---|
[2193] | 3 | from zope.component import queryMultiAdapter, getMultiAdapter |
---|
[2188] | 4 | |
---|
[2193] | 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 |
---|
[2188] | 11 | from plone.portlets.interfaces import IPortletDataProvider |
---|
| 12 | |
---|
| 13 | from zope import schema |
---|
| 14 | from zope.formlib import form |
---|
[2193] | 15 | |
---|
| 16 | from Acquisition import aq_inner |
---|
[2188] | 17 | from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile |
---|
| 18 | |
---|
| 19 | from quintagroup.portlet.map import QGMapPortletMessageFactory as _ |
---|
| 20 | |
---|
[2193] | 21 | from logging import getLogger |
---|
| 22 | logger = getLogger("Plone") |
---|
[2188] | 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 | |
---|
[2193] | 37 | collection_path = schema.TextLine(title=_(u"Path to collection"), |
---|
| 38 | description=_(u"A plone physical path to collection of locations"), |
---|
| 39 | required=True) |
---|
[2188] | 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 | |
---|
[2193] | 53 | def __init__(self, collection_path=""): |
---|
| 54 | self.collection_path = str(collection_path) |
---|
[2188] | 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 | |
---|
[2206] | 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 | |
---|
[2188] | 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 | |
---|
[2193] | 77 | _template = ViewPageTemplateFile('qgmapportlet.pt') |
---|
[2188] | 78 | |
---|
[2193] | 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() |
---|
[2206] | 84 | self.portal_url = portal_state.portal_url() |
---|
[2197] | 85 | self.gmapEnView = queryMultiAdapter((self.collection, self.request), |
---|
[2193] | 86 | name='maps_googlemaps_enabled_view') |
---|
[2197] | 87 | self.gmapView = queryMultiAdapter((self.collection, self.request), |
---|
| 88 | name='maps_googlemaps_view') |
---|
[2188] | 89 | |
---|
[2206] | 90 | def render(self): |
---|
| 91 | return self.render_js() + self.render_html() |
---|
| 92 | |
---|
[2197] | 93 | @ram.cache(render_cachekey) |
---|
[2206] | 94 | def render_html(self): |
---|
[2193] | 95 | return xhtml_compress(self._template()) |
---|
[2188] | 96 | |
---|
[2193] | 97 | @property |
---|
| 98 | def available(self): |
---|
[2197] | 99 | return bool(self.gmapEnView and self.gmapView and \ |
---|
| 100 | self.gmapEnView.enabled and \ |
---|
| 101 | self._data()) |
---|
[2188] | 102 | |
---|
[2193] | 103 | @property |
---|
| 104 | def collection(self): |
---|
| 105 | try: |
---|
| 106 | return self.portal.restrictedTraverse(self.data.collection_path) |
---|
| 107 | except: |
---|
| 108 | return None |
---|
[2188] | 109 | |
---|
[2197] | 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 '' |
---|
[2188] | 114 | |
---|
[2197] | 115 | @memoize |
---|
[2206] | 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 |
---|
[2197] | 126 | def _data(self): |
---|
| 127 | if hasattr(self.collection, 'queryCatalog'): |
---|
| 128 | return self.collection.queryCatalog() |
---|
| 129 | return [] |
---|
| 130 | |
---|
| 131 | |
---|
| 132 | |
---|
[2193] | 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.") |
---|
[2188] | 137 | |
---|
[2193] | 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)) |
---|
[2188] | 143 | |
---|
| 144 | class EditForm(base.EditForm): |
---|
| 145 | form_fields = form.Fields(IQGMapPortlet) |
---|
[2193] | 146 | label = _(u"Edit Quintagroup Map Portlet") |
---|
| 147 | description = _(u"This portlet displays Locations from the collection on the Google Map.") |
---|