Skip to content

Commit

Permalink
Added read_by index. Use ZCatalog > 3.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
sgeulette committed Mar 7, 2018
1 parent b410403 commit c79fa08
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Changelog
------------------
- package modification
[sgeulette]
- added viewlet, view, utils
- added viewlet, view, utils, index
[sgeulette]

1.0b6
Expand Down
2 changes: 0 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ To do

- Propose an optional zcml to automatically mark as read traversed objects
- Add a registry option with a portal types list to choose if modification event must be notified
- Add a separate profile to choose to add read/unread index


Translations
------------
Expand Down
3 changes: 2 additions & 1 deletion buildout.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[buildout]
extends =
https://raw.github.com/collective/buildout.plonetest/master/plone-4.3.x.cfg
# https://raw.github.com/collective/buildout.plonetest/master/plone-5.x.cfg
# https://raw.github.com/collective/buildout.plonetest/master/plone-5.0.x.cfg
# https://raw.github.com/collective/buildout.plonetest/master/plone-5.1.x.cfg
buildout.d/development.cfg
buildout.d/versions-plone-4.3.x.cfg
2 changes: 2 additions & 0 deletions buildout.d/versions-plone-4.3.x.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[versions]
Products.ZCatalog = 3.0.2
2 changes: 2 additions & 0 deletions buildout.d/versions-plone-5.0.x.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[versions]

2 changes: 2 additions & 0 deletions buildout.d/versions-plone-5.1.x.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[versions]

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
'plone.app.contenttypes',
'plone.app.imaging',
'plone.app.robotframework[debug]',
'Products.ZCatalog>3.0,<=3.0.3',
],
},
entry_points="""
Expand Down
16 changes: 16 additions & 0 deletions src/collective/readunread/adapters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# -*- coding: utf-8 -*-

from collective.readunread.utils import ANNOT_KEY
from plone.indexer import indexer
from Products.CMFCore.interfaces import IContentish
from imio.helpers.content import get_from_annotation


@indexer(IContentish)
def read_by_index(obj):
""" Index method escaping acquisition """
userids = list(get_from_annotation(ANNOT_KEY, obj=obj, default=[]))
# we need to put a default value not empty, because an empty list is not searched with 'not' query
if not userids:
userids = [0]
return userids
2 changes: 2 additions & 0 deletions src/collective/readunread/configure.zcml
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,6 @@
factory=".setuphandlers.HiddenProfiles"
name="collective.readunread-hiddenprofiles" />

<adapter name="read_by" factory=".adapters.read_by_index" />

</configure>
7 changes: 7 additions & 0 deletions src/collective/readunread/profiles/base/catalog.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<object name="portal_catalog" meta_type="Plone Catalog Tool">
<index name="read_by" meta_type="KeywordIndex">
<indexed_attr value="read_by"/>
</index>
<column value="read_by"/>
</object>
32 changes: 32 additions & 0 deletions src/collective/readunread/tests/test_adapters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# -*- coding: utf-8 -*-
"""Setup tests for this package."""
from collective.readunread.utils import get_ru_status
from collective.readunread.utils import set_ru_status
from collective.readunread.testing import COLLECTIVE_READUNREAD_INTEGRATION_TESTING # noqa
from plone.app.testing import TEST_USER_ID

import unittest


class TestAdapters(unittest.TestCase):
"""Test collective.readunread adapters.py."""

layer = COLLECTIVE_READUNREAD_INTEGRATION_TESTING

def setUp(self):
"""Custom shared utility setup for tests."""
self.portal = self.layer['portal']
self.tt1 = self.portal['tt1']
self.pc = self.portal.portal_catalog

def test_read_by_index(self):
# test catalog search, also with 'not' query to find 'unread' items
self.assertEqual(get_ru_status(self.tt1), 'unread')
self.assertEqual(len(self.pc(portal_type='testtype', read_by=0)), 1)
self.assertEqual(len(self.pc(portal_type='testtype', read_by=TEST_USER_ID)), 0)
self.assertEqual(len(self.pc(portal_type='testtype', read_by={'not': TEST_USER_ID})), 1)
# change read status for current user id
set_ru_status(self.tt1, status='read')
self.assertEqual(len(self.pc(portal_type='testtype', read_by=0)), 0)
self.assertEqual(len(self.pc(read_by=TEST_USER_ID)), 1)
self.assertEqual(len(self.pc(portal_type='testtype', read_by={'not': TEST_USER_ID})), 0)
1 change: 1 addition & 0 deletions src/collective/readunread/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,5 @@ def set_ru_status(obj, userid=None, status='read'):
add_to_annotation(ANNOT_KEY, userid, obj=obj)
elif status == 'unread':
del_from_annotation(ANNOT_KEY, userid, obj=obj)
obj.reindexObject(idxs=['read_by'])
return status

0 comments on commit c79fa08

Please sign in to comment.