Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split key column from sort column; respect DoNotReindex when adding column #482

Merged
merged 1 commit into from
Nov 17, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions Products/CMFPlomino/PlominoView.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,23 @@
description="Column used to sort the view",
format='select',
label_msgid=_('CMFPlomino_label_SortColumn', default="Sort column"),
description_msgid=_('CMFPlomino_help_SortColumn', default="Column used to sort the view"),
description_msgid=_('CMFPlomino_help_SortColumn', default="Column used to sort the view, and by default for key lookup"),
i18n_domain='CMFPlomino',
),
vocabulary="SortColumn_vocabulary",
vocabulary="Column_vocabulary",
schemata="Sorting",
),
StringField(
name='KeyColumn',
widget=SelectionWidget(
label="Key column",
description="Column used for key lookup",
format='select',
label_msgid=_('CMFPlomino_label_KeyColumn', default="Key column"),
description_msgid=_('CMFPlomino_help_KeyColumn', default="Column used for key lookup, if different from sort column"),
i18n_domain='CMFPlomino',
),
vocabulary="Column_vocabulary",
schemata="Sorting",
),
BooleanField(
Expand Down Expand Up @@ -424,9 +437,11 @@ def at_post_create_script(self):
""" Post create
"""
db = self.getParentDatabase()
refresh = not db.DoNotReindex
db.getIndex().createSelectionIndex(
'PlominoViewFormula_'+self.getViewName())
if not db.DoNotReindex:
'PlominoViewFormula_'+self.getViewName(),
refresh=refresh)
if refresh:
self.getParentDatabase().getIndex().refresh()

security.declarePublic('declareColumn')
Expand Down Expand Up @@ -680,21 +695,30 @@ def getPosition(self):

security.declarePublic('getDocumentsByKey')
def getDocumentsByKey(self, key, getObject=True):
""" Get documents where the sorted column value matches the given key.
""" Get documents where key or sorted column matches the given key
"""
index = self.getParentDatabase().getIndex()
sortindex = self.getSortColumn()
if not sortindex:
keycolumn = self.getKeyColumn()
sortcolumn = self.getSortColumn()

if not (keycolumn or sortcolumn):
return []

sortindex = self.getIndexKey(sortindex)
query = {'PlominoViewFormula_%s' % self.getViewName(): True}
sortkey = None
if keycolumn:
query[self.getIndexKey(keycolumn)] = key
elif sortcolumn:
sortkey = self.getIndexKey(sortcolumn)
query[sortkey] = key

results = index.dbsearch(
{'PlominoViewFormula_%s' % self.getViewName(): True,
sortindex: key},
sortindex,
query,
sortkey,
self.getReverseSorting())

if getObject:
# TODO: keep lazy
return [d.getObject() for d in results]
else:
return results
Expand Down Expand Up @@ -777,7 +801,7 @@ def getIndexKey(self, columnName):
key = ''
return key

def SortColumn_vocabulary(self):
def Column_vocabulary(self):
return [''] + [c.id for c in self.getColumns()]

registerType(PlominoView, PROJECTNAME)
Expand Down
9 changes: 9 additions & 0 deletions Products/CMFPlomino/tests/plomino.txt
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,15 @@ field::
>>> 'field2' in db.getIndex().indexes()
True

If we don't want the sort column to double as the key column, we can set
another column as key column::

>>> db.view1.setKeyColumn('col2')
>>> len(db.view1.getDocumentsByKey('hello'))
0
>>> len(db.view1.getDocumentsByKey('My favorite song is Ho Chi Minh City calling by The Clash'))
1

Import/export design
---------------------

Expand Down