-
Notifications
You must be signed in to change notification settings - Fork 7
/
vocabularies.py
143 lines (119 loc) · 4.39 KB
/
vocabularies.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
# -*- coding: utf-8 -*-
from collective.collectionfilter import _
from collective.collectionfilter.interfaces import IGroupByCriteria
from collective.collectionfilter.interfaces import IGroupByModifier
from collective.collectionfilter.utils import safe_encode
from zope.component import getAdapters
from zope.component import getUtility
from zope.interface import implementer
from zope.interface import provider
from zope.schema.interfaces import IVocabularyFactory
from zope.schema.vocabulary import SimpleTerm
from zope.schema.vocabulary import SimpleVocabulary
import plone.api
import six
# Use this EMPTY_MARKER for your custom indexer to index empty criterions.
EMPTY_MARKER = '__EMPTY__'
TEXT_IDX = "SearchableText"
GEOLOC_IDX = [
'latitude',
'longitude',
]
GROUPBY_BLACKLIST = [
'CreationDate',
'Date',
'Description',
'EffectiveDate',
'ExpirationDate',
'ModificationDate',
'Title',
'UID',
'cmf_uid',
'created',
'effective',
'end',
'expires',
'getIcon',
'getId',
'getObjSize',
'getRemoteUrl',
'id',
'last_comment_date',
'listCreators',
'meta_type',
'modified',
'start',
'sync_uid',
'total_comments',
] + GEOLOC_IDX # latitude/longitude is handled as a range filter ... see query.py # noqa
DEFAULT_FILTER_TYPE = 'single'
LIST_SCALING = ['No Scaling', 'Linear', 'Logarithmic']
@implementer(IGroupByCriteria)
class GroupByCriteria():
"""Global utility for retrieving and manipulating groupby criterias.
1) Populate ``groupby`` catalog metadata.
2) Do not use blacklisted metadata columns.
3) Use IGroupByModifier adapters to modify the datastructure.
"""
_groupby = None
groupby_modify = {}
@property
def groupby(self):
if self._groupby is not None:
# The groupby criteria are used at each IBeforeTraverseEvent - so
# on each request. This has to be fast, so exit early.
return self._groupby
self._groupby = {}
cat = plone.api.portal.get_tool('portal_catalog')
# get catalog metadata schema, but filter out items which cannot be
# used for grouping
metadata = [it for it in cat.schema() if it not in GROUPBY_BLACKLIST]
for it in metadata:
index_modifier = None
idx = cat._catalog.indexes.get(it)
if six.PY2 and getattr(idx, 'meta_type', None) == 'KeywordIndex':
# in Py2 KeywordIndex accepts only utf-8 encoded values.
index_modifier = safe_encode
self._groupby[it] = {
'index': it,
'metadata': it,
'display_modifier': _, # Allow to translate in this package domain per default. # noqa
'index_modifier': index_modifier,
'value_blacklist': None,
'sort_key_function': lambda it: it['title'].lower(), # sort key function. defaults to a lower-cased title. # noqa
}
modifiers = getAdapters((self, ), IGroupByModifier)
for name, modifier in sorted(modifiers, key=lambda it: it[0]):
modifier()
return self._groupby
@groupby.setter
def groupby(self, value):
self._groupby = value
@provider(IVocabularyFactory)
def GroupByCriteriaVocabulary(context):
"""Collection filter group by criteria.
"""
groupby = getUtility(IGroupByCriteria).groupby
items = [SimpleTerm(title=_(it), value=it) for it in groupby.keys()]
return SimpleVocabulary(items)
# TODO: this should depend on the index type, or be validated against it
@provider(IVocabularyFactory)
def FilterTypeVocabulary(context):
items = [
SimpleTerm(title=_('filtertype_single'), value='single'),
SimpleTerm(title=_('filtertype_and'), value='and'),
SimpleTerm(title=_('filtertype_or'), value='or')
]
return SimpleVocabulary(items)
@provider(IVocabularyFactory)
def InputTypeVocabulary(context):
items = [
SimpleTerm(title=_('inputtype_links'), value='links'),
SimpleTerm(title=_('inputtype_checkboxes_radiobuttons'), value='checkboxes_radiobuttons'), # noqa
SimpleTerm(title=_('inputtype_checkboxes_dropdowns'), value='checkboxes_dropdowns') # noqa
]
return SimpleVocabulary(items)
@provider(IVocabularyFactory)
def ListScalingVocabulary(context):
items = [SimpleTerm(title=_(it), value=it) for it in LIST_SCALING]
return SimpleVocabulary(items)