def get_children_pod_template(self): """ @see plone.api.content.get @see imio.helpers.content.get_from_annotation @see imio.helpers.content.del_from_annotation :return: a list containing the children templates. Handles the case when the uid doesn't match any object by deleting the uid from anotation. """ res = set() to_delete = set() annotated = get_from_annotation( ConfigurablePODTemplate.parent_pod_annotation_key, self) if annotated: for uid in annotated: child = api.content.get(UID=uid) if child: res.add(child) else: # child doesn't exist anymore. api.content.get returned None to_delete.add(uid) for uid in to_delete: del_from_annotation( ConfigurablePODTemplate.parent_pod_annotation_key, uid, obj=self) return res
def set_ru_status(obj, userid=None, status='read'): """ Set read status for userid on obj """ if userid is None: userid = current_userid() cur_status = get_ru_status(obj, userid=userid) if cur_status == status: return status if 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
def test_add_and_remove_annotation(self): obj = api.content.create(container=self.portal.folder, id='tt', type='testingtype', enabled='Should be a boolean') fakeUid = obj.UID() api.content.delete(obj) obj = api.content.create(container=self.portal.folder, id='tt', type='testingtype', enabled='Should be a boolean') # test get self.assertIsNone(get_from_annotation('test_annot', obj=obj)) self.assertEqual(get_from_annotation('test_annot', obj=obj, default='empty'), 'empty') # get value with uid that doesn't match an object raises no error. self.assertEqual(get_from_annotation('test_annot', uid=fakeUid, default='empty'), 'empty') # test add add_to_annotation('test_annot', 'tralala', uid=obj.UID()) self.assertEqual(IAnnotations(obj)['test_annot'], ['tralala']) add_to_annotation('test_annot', 'tralala', uid=obj.UID()) self.assertEqual(IAnnotations(obj)['test_annot'], ['tralala']) self.assertEqual(get_from_annotation('test_annot', obj=obj), ['tralala']) add_to_annotation('test_annot', 'trilili', obj=obj) self.assertEqual(IAnnotations(obj)['test_annot'], ['tralala', 'trilili']) # add value with uid that doesn't match an object raises no error. add_to_annotation('test_annot', 'tralala', uid='znfzete tztzfozaebfozaefg') # test remove del_from_annotation('test_annot', 'tralala', uid=obj.UID()) self.assertEqual(IAnnotations(obj)['test_annot'], ['trilili']) del_from_annotation('test_annot', 'trilili', uid=obj.UID()) self.assertEqual(IAnnotations(obj)['test_annot'], []) # remove value with uid that doesn't match an object raises no error. del_from_annotation('test_annot', 'tralala', uid=fakeUid) # remove value that doesn't exist in set del_from_annotation('test_annot', 'trololo', uid=obj.UID()) self.assertEqual(IAnnotations(obj)['test_annot'], []) # remove value from a no-existing key in annotation self.assertIsNone(del_from_annotation('test_not_key_in_annot', 'trololo', uid=obj.UID()))
def del_parent_pod_annotation(self): if hasattr(self, 'pod_template_to_use') and self.pod_template_to_use: del_from_annotation( ConfigurablePODTemplate.parent_pod_annotation_key, self.UID(), uid=self.pod_template_to_use)
def test_add_and_remove_annotation(self): obj = api.content.create(container=self.portal.folder, id='tt', type='testingtype', enabled='Should be a boolean') fakeUid = obj.UID() api.content.delete(obj) obj = api.content.create(container=self.portal.folder, id='tt', type='testingtype', enabled='Should be a boolean') # test get self.assertIsNone(get_from_annotation('test_annot', obj=obj)) self.assertEqual( get_from_annotation('test_annot', obj=obj, default='empty'), 'empty') # get value with uid that doesn't match an object raises no error. self.assertEqual( get_from_annotation('test_annot', uid=fakeUid, default='empty'), 'empty') # test add ret = add_to_annotation('test_annot', 'tralala', uid=obj.UID()) self.assertEqual(ret, 'tralala') self.assertEqual(IAnnotations(obj)['test_annot'], ['tralala']) add_to_annotation('test_annot', 'tralala', uid=obj.UID()) self.assertEqual(IAnnotations(obj)['test_annot'], ['tralala']) self.assertEqual(get_from_annotation('test_annot', obj=obj), ['tralala']) add_to_annotation('test_annot', 'trilili', obj=obj) self.assertEqual( IAnnotations(obj)['test_annot'], ['tralala', 'trilili']) # add value with uid that doesn't match an object raises no error. ret = add_to_annotation('test_annot', 'tralala', uid='znfzete tztzfozaebfozaefg') self.assertIsNone(ret) # test remove ret = del_from_annotation('test_annot', 'tralala', uid=obj.UID()) self.assertEqual(ret, 'tralala') self.assertEqual(IAnnotations(obj)['test_annot'], ['trilili']) del_from_annotation('test_annot', 'trilili', uid=obj.UID()) self.assertEqual(IAnnotations(obj)['test_annot'], []) # remove value with uid that doesn't match an object raises no error. del_from_annotation('test_annot', 'tralala', uid=fakeUid) # remove value that doesn't exist in set del_from_annotation('test_annot', 'trololo', uid=obj.UID()) self.assertEqual(IAnnotations(obj)['test_annot'], []) # remove value from a no-existing key in annotation self.assertIsNone( del_from_annotation('test_not_key_in_annot', 'trololo', uid=obj.UID())) # test simple set ret = set_to_annotation('test_annot', 'trululu', obj=obj) self.assertEqual(ret, 'trululu') self.assertEqual(IAnnotations(obj)['test_annot'], 'trululu') set_to_annotation('test_annot', 'trululuid', uid=obj.UID()) self.assertEqual(IAnnotations(obj)['test_annot'], 'trululuid') ret = set_to_annotation('test_annot', 'trululu', uid='df dgf fdsqqe dsf') self.assertIsNone(ret)
def test_get_ru_status(self): self.assertEqual(get_ru_status(self.tt1), 'unread') add_to_annotation(ANNOT_KEY, TEST_USER_ID, obj=self.tt1) self.assertEqual(get_ru_status(self.tt1), 'read') del_from_annotation(ANNOT_KEY, TEST_USER_ID, obj=self.tt1) self.assertEqual(get_ru_status(self.tt1), 'unread')