Skip to content

Commit

Permalink
Merge pull request #482 from plomino/jean-keycolumn
Browse files Browse the repository at this point in the history
Split key column from sort column; respect `DoNotReindex` when adding column
  • Loading branch information
ebrehault committed Nov 17, 2013
2 parents 2f00c73 + 89fa797 commit 771b829
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 12 deletions.
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

0 comments on commit 771b829

Please sign in to comment.