forked from collective/collective.collectionfilter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseviews.py
384 lines (328 loc) · 12.5 KB
/
baseviews.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# -*- coding: utf-8 -*-
from Acquisition import aq_inner
from collective.collectionfilter import PLONE_VERSION
from collective.collectionfilter.filteritems import get_filter_items
from collective.collectionfilter.filteritems import ICollectionish
from collective.collectionfilter.interfaces import IGroupByCriteria
from collective.collectionfilter.query import make_query
from collective.collectionfilter.utils import base_query
from collective.collectionfilter.utils import safe_decode
from collective.collectionfilter.utils import safe_encode
from collective.collectionfilter.utils import safe_iterable
from collective.collectionfilter.vocabularies import TEXT_IDX
from plone import api
from plone.api.portal import get_registry_record as getrec
from plone.app.uuid.utils import uuidToCatalogBrain
from plone.app.uuid.utils import uuidToObject
from plone.i18n.normalizer.interfaces import IIDNormalizer
from plone.memoize import instance
from plone.uuid.interfaces import IUUID
from Products.CMFPlone.utils import get_top_request
from Products.CMFPlone.utils import safe_unicode
from six.moves.urllib.parse import urlencode
from zope.component import getUtility
from zope.component import queryUtility
from zope.i18n import translate
from zope.schema.interfaces import IVocabularyFactory
import json
try:
from collective.geolocationbehavior.interfaces import IGeoJSONProperties
from plone.formwidget.geolocation.vocabularies import _
HAS_GEOLOCATION = True
except ImportError:
HAS_GEOLOCATION = False
class BaseView(object):
"""Abstract base filter view class."""
_collection = None
_top_request = None
@property
def settings(self):
return self.data
@property
def available(self):
return True
@property
def is_available(self):
return True
@property
def filter_id(self):
raise NotImplementedError
@property
def title(self):
return getattr(self.settings, "header", getattr(self.collection, "Title", u""))
@property
def filterClassName(self):
name = self.title and queryUtility(IIDNormalizer).normalize(self.title)
return u"filter" + name.capitalize() if name else ""
@property
def reload_url(self):
raise NotImplementedError
@property
def top_request(self):
if not self._top_request:
self._top_request = get_top_request(self.request)
return self._top_request
@property
def collection_uuid(self):
if self.settings.target_collection:
return self.settings.target_collection
return IUUID(self.context)
@property
def collection(self):
if not self._collection:
self._collection = uuidToCatalogBrain(self.collection_uuid)
return aq_inner(self._collection)
@property
def pat_options(self):
return json.dumps(
{
"collectionUUID": self.collection_uuid,
"reloadURL": self.reload_url,
"ajaxLoad": self.ajax_load,
"contentSelector": self.content_selector,
"notifyOnly": getattr(self, "notify_only", False)
}
)
@property
def content_selector(self):
if self.settings.content_selector:
return self.settings.content_selector
collectionish = (
ICollectionish(self.collection.getObject()) if self.collection else None
)
selector = collectionish.content_selector
if collectionish is None or not selector:
return u"#content-core"
else:
return selector
@property
def ajax_load(self):
if PLONE_VERSION < "5.1":
# Due to bug in AJAX load pattern makes it hard to make it work in 5.0.x
return False
values = api.portal.get_registry_record("plone.patternoptions")
if "collectionfilter" in values:
filterOptions = json.loads(values["collectionfilter"])
if "ajaxLoad" in filterOptions:
return filterOptions["ajaxLoad"]
return True
@property
def notify_only(self):
return self.settings.notify_only
class BaseFilterView(BaseView):
@property
def input_type(self):
if self.settings.input_type == "links":
return "link"
elif self.settings.filter_type == "single":
if self.settings.input_type == "checkboxes_radiobuttons":
return "radio"
else:
return "dropdown"
else:
return "checkbox"
# results is called twice inside the template in view/available and view/results. But its expensive so we cache it
# but just the the lifetime of the view
@instance.memoize
def results(self):
results = get_filter_items(
target_collection=self.collection_uuid,
group_by=self.settings.group_by,
filter_type=self.settings.filter_type,
narrow_down=self.settings.narrow_down,
show_count=self.settings.show_count,
view_name=self.settings.view_name,
cache_enabled=self.settings.cache_enabled,
request_params=self.top_request.form or {},
content_selector=self.settings.content_selector,
)
return results
@property
def is_available(self):
if not self.settings.hide_if_empty:
return True
if self.settings.narrow_down:
groupby_criteria = getUtility(IGroupByCriteria).groupby
idx = groupby_criteria[self.settings.group_by]["index"]
request_params = safe_decode(self.top_request.form)
current_idx_value = safe_iterable(request_params.get(idx))
if current_idx_value:
return True
results = self.results()
return not (results is None or len(results) <= 2) # 2 becayse we include "All"
class BaseSearchView(BaseView):
@property
def value(self):
return safe_unicode(self.top_request.get(TEXT_IDX, ""))
@property
def action_url(self):
return self.collection.getURL()
@property
def urlquery(self):
urlquery = {}
urlquery.update(self.top_request.form)
for it in (
TEXT_IDX,
"b_start",
"b_size",
"batch",
"sort_on",
"limit",
"portlethash",
):
# Remove problematic url parameters
if it in urlquery:
del urlquery[it]
# add filter trigger if not already there
if "collectionfilter" not in urlquery:
urlquery["collectionfilter"] = "1"
return ((k, vv) for k, v in urlquery.items() for vv in safe_iterable(v))
@property
def ajax_url(self):
# Recursively transform all to unicode
request_params = safe_decode(self.top_request.form)
urlquery = base_query(request_params, extra_ignores=["SearchableText"])
query_param = urlencode(safe_encode(urlquery), doseq=True)
ajax_url = u"/".join(
[
it
for it in [
self.collection.getURL(),
self.settings.view_name,
"?" + query_param if query_param else None,
]
if it
]
)
return ajax_url
class BaseSortOnView(BaseView):
def results(self):
collection = ICollectionish(self.collection.getObject()).selectContent(
self.settings.content_selector
)
if collection is None:
return
curr_val = self.top_request.get("sort_on", collection.sort_on)
curr_order = self.top_request.get(
"sort_order", "descending" if collection.sort_reversed else "ascending"
)
new_order = "ascending"
if curr_order is not None and curr_order == "ascending":
new_order = "descending"
sortable_indexes = getUtility(
IVocabularyFactory, name="collective.collectionfilter.SortOnIndexes"
)
vocab = sortable_indexes(self.context)
for idx in self.settings.sort_on:
curr = curr_val == idx
yield {
"value": idx,
"title": vocab.getTerm(idx).title,
"new_order": new_order if curr else "ascending",
"curr_order": curr_order if curr else "",
"active": curr,
}
@property
def ajax_url(self):
# Recursively transform all to unicode
request_params = safe_decode(self.top_request.form)
urlquery = base_query(request_params, extra_ignores=["sort_on", "sort_order"])
query_param = urlencode(safe_encode(urlquery), doseq=True)
ajax_url = u"/".join(
[
it
for it in [
self.collection.getURL(),
self.settings.view_name,
"?" + query_param if query_param else None,
]
if it
]
)
return ajax_url
if HAS_GEOLOCATION:
class BaseMapsView(BaseView):
@property
def ajax_url(self):
# Recursively transform all to unicode
request_params = safe_decode(self.top_request.form)
urlquery = base_query(
request_params, extra_ignores=["latitude", "longitude"]
)
query_param = urlencode(safe_encode(urlquery), doseq=True)
ajax_url = u"/".join(
[
it
for it in [
self.collection.getURL(),
self.settings.view_name,
"?" + query_param if query_param else None,
]
if it
]
)
return ajax_url
@property
def locations(self):
custom_query = {} # Additional query to filter the collection
collection = uuidToObject(self.collection_uuid)
if not collection:
return None
# Recursively transform all to unicode
request_params = safe_decode(self.top_request.form or {})
# Get all collection results with additional filter
# defined by urlquery
custom_query = base_query(request_params)
custom_query = make_query(custom_query)
return (
ICollectionish(collection)
.selectContent(self.settings.content_selector)
.results(custom_query, request_params)
)
@property
def data_geojson(self):
"""Return the geo location as GeoJSON string."""
features = []
for it in self.locations:
if not it.longitude or not it.latitude:
# these ``it`` are brains, so anything which got lat/lng
# indexed can be used.
continue
feature = {
"type": "Feature",
"id": it.UID,
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
it.longitude,
it.latitude,
],
},
}
props = IGeoJSONProperties(it.getObject(), None)
if getattr(props, "popup", None):
feature["properties"]["popup"] = props.popup
if getattr(props, "color", None):
feature["properties"]["color"] = props.color
if getattr(props, "extraClasses", None):
feature["properties"]["extraClasses"] = props.extraClasses
features.append(feature)
if not features:
return
geo_json = json.dumps({"type": "FeatureCollection", "features": features})
return geo_json
@property
def map_configuration(self):
config = {
"fullscreencontrol": getrec("geolocation.fullscreen_control"),
"locatecontrol": getrec("geolocation.locate_control"),
"zoomcontrol": getrec("geolocation.zoom_control"),
"minimap": getrec("geolocation.show_minimap"),
"default_map_layer": self.settings.default_map_layer,
"map_layers": [
{"title": translate(_(it), context=self.request), "id": it}
for it in self.settings.map_layers
],
}
return json.dumps(config)