Skip to content

Commit

Permalink
Merge pull request #14 from collective/MOD-846_awake_object_vocabular…
Browse files Browse the repository at this point in the history
…y_column

Added `AwakeObjectVocabularyColumn` and `AwakeObjectAbbrColumn`
  • Loading branch information
sgeulette committed Aug 29, 2023
2 parents bc0159d + 79abf0a commit 3d64299
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 221 deletions.
9 changes: 7 additions & 2 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ Changelog
2.23 (unreleased)
-----------------

- Nothing changed yet.

- Added `AwakeObjectVocabularyColumn` and `AwakeObjectAbbrColumn`, having same
behavior as `VocabularyColumn` and `AbbrColumn` but `attrName` is get on awaken
object instead brain metadata, this avoids adding a `portal_catalog` metadata.
[gbastien]
- In `AwakeObject` columns, use `imio.helpers.content.base_getattr` instead
`getattr` to avoid problems with acquisiton.
[gbastien]

2.22 (2023-08-24)
-----------------
Expand Down
210 changes: 0 additions & 210 deletions bootstrap.py

This file was deleted.

33 changes: 24 additions & 9 deletions src/collective/eeafaceted/z3ctable/columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from collective.excelexport.exportables.dexterityfields import get_exportable_for_fieldname
from datetime import date
from DateTime import DateTime
from imio.helpers.content import base_getattr
from imio.helpers.content import get_user_fullname
from plone import api
from Products.CMFPlone.utils import base_hasattr
Expand Down Expand Up @@ -209,12 +210,7 @@ class AwakeObjectGetAttrColumn(BaseColumn):
sort_index = -1

def renderCell(self, item):
obj = self._getObject(item)
try:
result = getattr(obj, self.attrName)
return safe_unicode(result)
except AttributeError:
return u'-'
return safe_unicode(base_getattr(self._getObject(item), self.attrName)) or u'-'


class AwakeObjectMethodColumn(BaseColumn):
Expand All @@ -227,9 +223,8 @@ class AwakeObjectMethodColumn(BaseColumn):
def renderCell(self, item):
obj = self._getObject(item)
try:
result = getattr(obj, self.attrName)(**self.params)
return safe_unicode(result)
except AttributeError:
return safe_unicode(base_getattr(obj, self.attrName)(**self.params)) or u'-'
except TypeError:
return u'-'


Expand Down Expand Up @@ -466,6 +461,26 @@ def renderCell(self, item):
return res


class AwakeObjectVocabularyColumn(VocabularyColumn):
"""Column that will wake the object then getattr attrName on it."""

# column not sortable by default, give a catalog index if necessary
sort_index = -1

def getValue(self, item):
return safe_unicode(base_getattr(self._getObject(item), self.attrName))


class AwakeObjectAbbrColumn(AbbrColumn):
"""Column that will wake the object then getattr attrName on it."""

# column not sortable by default, give a catalog index if necessary
sort_index = -1

def getValue(self, item):
return safe_unicode(base_getattr(self._getObject(item), self.attrName))


class ColorColumn(I18nColumn):
"""A column that is aimed to display a background color
and a help message on hover."""
Expand Down
31 changes: 31 additions & 0 deletions src/collective/eeafaceted/z3ctable/tests/test_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

from collective.eeafaceted.z3ctable.columns import AbbrColumn
from collective.eeafaceted.z3ctable.columns import ActionsColumn
from collective.eeafaceted.z3ctable.columns import AwakeObjectAbbrColumn
from collective.eeafaceted.z3ctable.columns import AwakeObjectGetAttrColumn
from collective.eeafaceted.z3ctable.columns import AwakeObjectMethodColumn
from collective.eeafaceted.z3ctable.columns import AwakeObjectVocabularyColumn
from collective.eeafaceted.z3ctable.columns import BaseColumn
from collective.eeafaceted.z3ctable.columns import BooleanColumn
from collective.eeafaceted.z3ctable.columns import BrowserViewCallColumn
Expand Down Expand Up @@ -391,6 +393,35 @@ def test_AbbrColumn(self):
u"<abbr title='unexisting_key'>unexisting_key, </abbr>"
u"<abbr title='Full existing value 2'>Existing v\xe9lue 2</abbr>")

def test_AwakeObjectVocabularyColumn(self):
"""VocabularyColumn where we awake object to get attrName attribute."""
self.eea_folder.setTitle(u'existing_key1')
table = self.faceted_z3ctable_view
column = AwakeObjectVocabularyColumn(self.portal, self.portal.REQUEST, table)
column.attrName = 'title'
brain = self.portal.portal_catalog(UID=self.eea_folder.UID())[0]
column.vocabulary = "collective.eeafaceted.z3ctable.testingvocabulary"
column.full_vocabulary = "collective.eeafaceted.z3ctable.testingfullvocabulary"
self.assertEqual(column.renderCell(brain), u"Existing v\xe9lue 1")
# will raise if attrName does not exist
column.attrName = "unknown"
self.assertEqual(column.renderCell(brain), u'-')

def test_AwakeObjectAbbrColumn(self):
"""AbbrColumn where we awake object to get attrName attribute."""
self.eea_folder.setTitle(u'existing_key1')
table = self.faceted_z3ctable_view
column = AwakeObjectAbbrColumn(self.portal, self.portal.REQUEST, table)
column.attrName = 'title'
brain = self.portal.portal_catalog(UID=self.eea_folder.UID())[0]
column.vocabulary = "collective.eeafaceted.z3ctable.testingvocabulary"
column.full_vocabulary = "collective.eeafaceted.z3ctable.testingfullvocabulary"
self.assertEqual(column.renderCell(brain),
u"<abbr title='Full existing value 1'>Existing v\xe9lue 1</abbr>")
# will raise if attrName does not exist
column.attrName = "unknown"
self.assertEqual(column.renderCell(brain), u'-')

def test_MemberIdColumn(self):
"""This column will display the fullname of the given metadata."""
# set a valid fullname for default user
Expand Down

0 comments on commit 3d64299

Please sign in to comment.