def test_Reject(self): self.startWorkflow() task = self.editor_items.pop() task.object_.unattribute('cat22') task.object_.attribute('cat21') task.finish('reject', u"Done") self.assertTrue(len(self.editor_items) == 0) self.assertTrue( 'cat21' not in interfaces.IAttribution(self.item).get()) self.assertTrue('cat12' in interfaces.IAttribution(self.item).get()) self.assertTrue('cat22' in interfaces.IAttribution(self.item).get()) self.assertTrue('cat32' in interfaces.IAttribution(self.item).get())
def storeToWorkItem(self): data, errors = self.extractData() categories = [] for catset in data.values(): for cat in catset: categories.append(cat.__name__) attr = interfaces.IAttribution(self.getContent()) attr.set(categories)
def get(self): attribution = list(interfaces.IAttribution(self.context).get()) vocabulary = self.field.value_type.vocabulary rc = [] for term in vocabulary: if term.value.__name__ in attribution: rc.append(term.value) return rc
def test_PostponeAndAccept(self): self.startWorkflow() self.assertTrue(len(self.editor_items) == 1) task = self.editor_items.pop() task.finish('postpone', u"Not yet done") self.assertTrue(len(self.editor_items) == 1) self.assertTrue(self.editor_items.pop() != task) task = self.editor_items.pop() task.object_.unattribute('cat22') task.object_.attribute('cat21') from zope.wfmc.interfaces import ProcessError task.finish('accept', u"Done") self.assertTrue(len(self.editor_items) == 0) self.assertTrue( 'cat22' not in interfaces.IAttribution(self.item).get()) self.assertTrue('cat12' in interfaces.IAttribution(self.item).get()) self.assertTrue('cat21' in interfaces.IAttribution(self.item).get()) self.assertTrue('cat32' in interfaces.IAttribution(self.item).get())
def test_AttributionUpToDateWhenCategoryMoved(self): """ Test if the attribution is up to date if a category was removed.""" self.root['c'] = catable = Categorizable() attribution = interfaces.IAttribution(catable) attribution.attribute('cat11') from zope.copypastemove.interfaces import IObjectMover mover = IObjectMover(self.categories['set1']['cat11']) mover.moveTo(self.categories['set2']) self.assertTrue(attribution.isAttributed('cat11'))
def removeAttributionSubscriber(category, event): """ Remove attributions if a category is removed.""" intids = zope.component.getUtility(IIntIds, context=category) container = zope.component.getUtility(interfaces.ICategoriesContainer, context=category) query = SearchQuery(AnyOf(ATTRIBUTION_INDEX, (category.__name__, ))) result = query.apply() for intid in result: categorizable = intids.getObject(intid) attribution = interfaces.IAttribution(categorizable) attribution.unattribute(category.__name__)
def test_AttributionUpToDateWhenCategoryRemoved(self): """ Test if the indexer is up to date if a category was removed.""" self.root['c'] = catable = Categorizable() attribution = interfaces.IAttribution(catable) attribution.attribute('cat11') zope.event.notify(ObjectModifiedEvent(catable)) self.assertTrue(attribution.isAttributed('cat11')) set1 = self.categories['set1'] del set1['cat11'] zope.event.notify(ObjectModifiedEvent(catable)) self.assertTrue(not attribution.isAttributed('cat11'))
def test_IndexedUpToDateWhenCategoryRemoved(self): """ Test if an attribution is indexed at all.""" self.root['c'] = catable = Categorizable() attribution = interfaces.IAttribution(catable) attribution.attribute('cat22') del self.categories['set2']['cat22'] from z3c.indexer.query import AnyOf from z3c.indexer.search import SearchQuery query = SearchQuery(AnyOf('attribution-set', ('cat22', ))) result = query.apply() self.assertTrue(len(result) == 0)
def startWorkflow(self): from zope.wfmc.interfaces import IProcessDefinition pd = zope.component.getUtility(IProcessDefinition, name='quotationtool.reclassify') from quotationtool.categorization.reclassify import ReclassificationContext context = ReclassificationContext(self.item) from quotationtool.categorization.attribution import PersistentAttribution import datetime self.work_attr = PersistentAttribution() self.work_attr.set(interfaces.IAttribution(self.item).get()) pd(context).start('bob', datetime.datetime.now(), u"Hello World", None, self.work_attr)
def moveAttributionSubscriber(category, event): """ Keep an attribution up to date if a category is renamed.""" if event.oldName and event.newName: intids = zope.component.getUtility(IIntIds, context=category) container = zope.component.getUtility(interfaces.ICategoriesContainer, context=category) query = SearchQuery(AnyOf(ATTRIBUTION_INDEX, (event.oldName, ))) result = query.apply() for intid in result: categorizable = intids.getObject(intid) attribution = interfaces.IAttribution(categorizable) attribution.unattribute(event.oldName) attribution.attribute(event.newName)
def set(self, value): if self.field.readonly: raise TypeError("Can't set values on read-only fields " "(name=%s, class=%s.%s)" % (self.field.__name__, self.context.__class__.__module__, self.context.__class__.__name__)) attribution = interfaces.IAttribution(self.context) vocabulary = self.field.value_type.vocabulary for term in vocabulary: if term.token in value: attribution.attribute(term.value.__name__) else: if attribution.isAttributed(term.value.__name__): attribution.unattribute(term.value.__name__)
def test_IfIndexed(self): """ Test if an attribution is indexed at all.""" self.root['c'] = catable = Categorizable() attribution = interfaces.IAttribution(catable) attribution.attribute('cat21') zope.event.notify(ObjectModifiedEvent(catable)) from z3c.indexer.query import AnyOf from z3c.indexer.search import SearchQuery query = SearchQuery(AnyOf('attribution-set', ('cat21', ))) result = query.apply() self.assertTrue(len(result) == 1) attribution.unattribute('cat21') zope.event.notify(ObjectModifiedEvent(catable)) query = SearchQuery(AnyOf('attribution-set', ('cat21', ))) result = query.apply() self.assertTrue(len(result) == 0)
def test_IndexedUpToDateWhenCategoryMovedAndRenamed(self): """ Test if an attribution is indexed at all.""" self.root['c'] = catable = Categorizable() attribution = interfaces.IAttribution(catable) attribution.attribute('cat11') zope.event.notify(ObjectModifiedEvent(catable)) from zope.copypastemove.interfaces import IObjectMover mover = IObjectMover(self.categories['set1']['cat11']) mover.moveTo(self.categories['set2'], new_name='moved') zope.event.notify(ObjectModifiedEvent(catable)) from z3c.indexer.query import AnyOf from z3c.indexer.search import SearchQuery query = SearchQuery(AnyOf('attribution-set', ('cat22', ))) result = query.apply() self.assertTrue(len(result) == 0) query = SearchQuery(AnyOf('attribution-set', ('moved', ))) result = query.apply() self.assertTrue(len(result) == 1)
def value(self): attribution = self.attribution_factory() intids = zope.component.getUtility(IIntIds, context=self.context) context_id = intids.getId(self.context) category_sets = zope.component.queryUtility( interfaces.ICategoriesContainer, context=self.context) if category_sets is None: return [] for catalog in zope.component.getAllUtilitiesRegisteredFor( zc.relation.interfaces.ICatalog, context=self.context): for info in catalog.iterValueIndexInfo(): for obj in catalog.findValues(info['name'], {zc.relation.RELATION: context_id}): relattr = interfaces.IAttribution(obj).get() for category_set in category_sets.values(): provided = False for iface in category_set.categorizable_items: if iface.providedBy(self.context): provided = True if info['name'] in category_set.relation_indices and provided: categories = self.attribution_factory(category_set.keys()) relattr = self.attribution_factory(relattr) attr = self.intersection(relattr, categories) attribution = self.union(attribution, attr) return list(attribution)
def setUp(self): super(ReclassificationTests, self).setUp() self.root = placefulSetUp(True) setUpZCML(self) hooks.setSite(self.root) testing.setUpIntIds(self) testing.setUpIndices(self) testing.setUpRelationCatalog(self) interaction = newInteraction() # needed for generation of categories testing.generateCategorizableItemDescriptions(self.root) testing.generateCategoriesContainer(self.root) from quotationtool.workflow import testing as workflowtesting workflowtesting.setUpWorkLists(self.root) workflowtesting.setUpIndices(self) from quotationtool.workflow.interfaces import IWorkList self.editor_items = zope.component.getUtility(IWorkList, name='editor', context=self.root) self.contributor_items = zope.component.getUtility(IWorkList, name='contributor', context=self.root) self.item = self.root['item'] = testing.Categorizable() attr = interfaces.IAttribution(self.item) attr.set(('cat12', 'cat22', 'cat32'))
def getAttribution(self, category_set=interfaces.ALL): attribution = self.attribution_factory() # get integer id of context object intids = zope.component.getUtility(IIntIds, context=self.context) context_id = intids.getId(self.context) # get categories container utility categories_container = zope.component.queryUtility( interfaces.ICategoriesContainer, context=self.context) if categories_container is None: return [] # iterate relation catalogs for catalog in zope.component.getAllUtilitiesRegisteredFor( zc.relation.interfaces.ICatalog, context=self.context): # iterate value indices of each relation catalog for info in catalog.iterValueIndexInfo(): # iterate intrinsically related (can only be zero or one) objects for obj in catalog.findValues(info['name'], {zc.relation.RELATION: context_id}): if category_set != interfaces.ALL: category_sets = (category_set,) else: category_sets = categories_container.values() # iterate category sets and check if the # attribution of the intrinsically related object # is inherited by the context object relattr = interfaces.IAttribution(obj).get() for category_set in category_sets: provided = False for iface in category_set.categorizable_items: if iface.providedBy(self.context): provided = True if info['name'] in category_set.relation_indices and provided: # calculate attribution categories = self.attribution_factory(category_set.keys()) relattr = self.attribution_factory(relattr) attr = self.intersection(relattr, categories) attribution = self.union(attribution, attr) return list(attribution)
def processFinished(self, process, history, object_, finish): if finish == 'accept': annotation = interfaces.IAttribution(self.item) annotation.set(object_.get()) zope.event.notify(ObjectModifiedEvent(self.item))
def value(self): attribution = interfaces.IAttribution(self.context) return attribution.get()
def finishedHook(self): annotation = interfaces.IAttribution(self.item) annotation.set(self.object_.get()) zope.event.notify(ObjectModifiedEvent(self.item))
def evolve(context): """ evolve to categorization based on annotations """ # We moved the imports here because of getInfo which only takes # the __doc__ string in evolve. getInfo should not import # anything. import zope.interface import zope.component from zope.app.zopeappgenerations import getRootFolder from zope.app.generations.utility import findObjectsProviding from zope.app.component import hooks from quotationtool.site.interfaces import IQuotationtoolSite root = getRootFolder(context) site = None for s in findObjectsProviding(root, IQuotationtoolSite): site = s break if site is None: raise Exception('No quotationtool site!') hooks.setSite(site) sm = site.getSiteManager() default = sm['default'] from zope.intid.interfaces import IIntIds intids = zope.component.getUtility(IIntIds, context=site) from persistent.list import PersistentList from z3c.indexer.index import SetIndex from z3c.indexer.interfaces import IIndex from quotationtool.categorization import interfaces from quotationtool.categorization.attribution import ATTRIBUTION_INDEX from quotationtool.categorization.relatedattribution import RELATED_ATTRIBUTION_INDEX sm['default'][ATTRIBUTION_INDEX] = idx = SetIndex() sm.registerUtility(idx, IIndex, name=ATTRIBUTION_INDEX) sm['default'][RELATED_ATTRIBUTION_INDEX] = idx = SetIndex() sm.registerUtility(idx, IIndex, name=RELATED_ATTRIBUTION_INDEX) categories = site['categories'] # create __categories attribute setattr(categories, '_CategoriesContainer__categories', categories._newCategoriesData()) for catset in categories.values(): # create relation_indices attribute setattr(catset, 'relation_indices', []) for name, cat in catset.items(): # register category in categories container categories.addCategory(name, cat) # evolve to annotation based attribution for intid in cat.attribution: obj = intids.getObject(intid) attribution = interfaces.IAttribution(obj).attribute( cat.__name__) # delete integer based attributes of category delattr(cat, '_Attribution__attribution') # delete integer based attributes of category set delattr(catset, '_Attribution__attribution')