def update(self): """Update activated labels. """ labeling = ILabeling(self.context) activate_labels = self.request.form.get('activate_labels', []) labeling.update(activate_labels) self.context.reindexObject(idxs=['labels']) return self._redirect()
def update(self): """Update activated labels. """ labeling = ILabeling(self.context) activate_labels = self.request.form.get('activate_labels', []) labeling.update(activate_labels) self.context.reindexObject(idxs=['labels']) return self._redirect()
def test_update_raises_LookupError_when_label_not_in_jar(self): self.assertEqual(0, len(self.jar.list())) self.jar.add('Question', '', False) labeling = ILabeling(self.document) with self.assertRaises(LookupError) as cm: labeling.update(['something']) self.assertEqual( 'Cannot activate label: the label' ' "something" is not in the label jar. ' 'Following labels ids are available: question', str(cm.exception))
def test_update__enable_labels(self): self.jar.add('Bug', 'red') self.jar.add('Question', 'green') self.jar.add('Feature', 'purple') labeling = ILabeling(self.document) self.assertEqual([], labeling.active_labels()) labeling.update(['bug', 'feature']) self.assertItemsEqual(['Bug', 'Feature'], label_titles(labeling.active_labels()))
def test_available_label(self): self.jar.add('Question', '#00FF00') labeling = ILabeling(self.document) labeling.update(['question']) self.assertEqual( [{'label_id': 'question', 'title': 'Question', 'color': '#00FF00', 'active': True}], list(labeling.available_labels()))
def test_update__enable_labels(self): self.jar.add('Bug', 'red', False) self.jar.add('Question', 'green', True) self.jar.add('Feature', 'purple', True) labeling = ILabeling(self.document) self.assertEqual([], labeling.active_labels()) labeling.update(['bug']) labeling.pers_update('feature', True) self.assertItemsEqual(['Bug', 'Feature'], label_titles(labeling.active_labels()))
def test_active_labels(self): self.jar.add('Question', '') self.jar.add('Bug', '') self.jar.add('Duplicate', '') labeling = ILabeling(self.document) labeling.update(['bug']) self.assertEqual( [{'label_id': 'bug', 'title': 'Bug', 'color': ''}], labeling.active_labels())
def test_update_raises_LookupError_when_label_not_in_jar(self): self.assertEqual(0, len(self.jar.list())) self.jar.add('Question', '') labeling = ILabeling(self.document) with self.assertRaises(LookupError) as cm: labeling.update(['something']) self.assertEqual( 'Cannot activate label: the label' ' "something" is not in the label jar. ' 'Following labels ids are available: question', str(cm.exception))
def test_active_labels_filters_deleted_labels(self): self.jar.add('Question', 'blue') self.jar.add('Bug', 'red') labeling = ILabeling(self.document) labeling.update(['question', 'bug']) self.jar.remove('bug') self.assertEqual( [{'label_id': 'question', 'title': 'Question', 'color': 'blue'}], list(labeling.active_labels()))
def test_active_labels_filters_deleted_labels(self): self.jar.add('Question', 'blue', False) self.jar.add('Bug', 'red', False) labeling = ILabeling(self.document) labeling.update(['question', 'bug']) self.jar.remove('bug') self.assertEqual([{ 'label_id': 'question', 'title': 'Question', 'color': 'blue', 'by_user': False }], list(labeling.active_labels()))
def test_active_labels_is_sorted(self): self.jar.add('Zeta-0', '', False) self.jar.add('zeta-1', '', False) self.jar.add('alpha-0', '', False) self.jar.add('\xc3\x84lpha-1', '', False) self.jar.add('Alpha-2', '', False) labeling = ILabeling(self.document) labeling.update([ 'zeta-0', 'zeta-1', 'alpha-0', 'alpha-1', 'alpha-2', ]) self.assertEqual( ['alpha-0', '\xc3\x84lpha-1', 'Alpha-2', 'Zeta-0', 'zeta-1'], [label.get('title') for label in labeling.active_labels()])
def test_active_labels(self): self.jar.add('Question', '', False) self.jar.add('Bug', '', False) self.jar.add('Duplicate', '', True) labeling = ILabeling(self.document) labeling.update(['bug']) labeling.pers_update('duplicate', True) self.assertListEqual([{ 'label_id': 'bug', 'title': 'Bug', 'color': '', 'by_user': False }, { 'label_id': 'duplicate', 'title': 'Duplicate', 'color': '', 'by_user': True }], labeling.active_labels())
def test_active_labels_is_sorted(self): self.jar.add('Zeta-0', '') self.jar.add('zeta-1', '') self.jar.add('alpha-0', '') self.jar.add('\xc3\x84lpha-1', '') self.jar.add('Alpha-2', '') labeling = ILabeling(self.document) labeling.update([ 'zeta-0', 'zeta-1', 'alpha-0', 'alpha-1', 'alpha-2', ]) self.assertEqual( ['alpha-0', '\xc3\x84lpha-1', 'Alpha-2', 'Zeta-0', 'zeta-1'], [label.get('title') for label in labeling.active_labels()])
def test_available_label(self): self.jar.add('Question', '#00FF00', False) self.jar.add('Read', 'red', True) labeling = ILabeling(self.document) labeling.update(['question']) labeling.pers_update('read', True) self.assertEqual([[{ 'label_id': 'read', 'title': 'Read', 'color': 'red', 'active': True, 'by_user': True }], [{ 'label_id': 'question', 'title': 'Question', 'color': '#00FF00', 'active': True, 'by_user': False }]], list(labeling.available_labels()))
def _apply(self, **data): if ((data.get('removed_values', None) and data['action_choice'] in ('remove', 'replace')) or (data.get('added_values', None)) and data['action_choice'] in ('add', 'replace', 'overwrite')): values = {'p_a': [], 'p_r': [], 'g_a': [], 'g_r': []} for act, lst in (('a', data.get('added_values', [])), ('r', data.get('removed_values', []))): for val in lst: typ = (':' in val) and 'p' or 'g' values['{}_{}'.format(typ, act)].append(val.split(':')[0]) for brain in self.brains: obj = brain.getObject() labeling = ILabeling(obj) p_act, g_act = active_labels(labeling) # manage global labels if self.can_change_labels and (values['g_a'] or values['g_r']): if data['action_choice'] in ('overwrite'): items = set(values['g_a']) else: items = set(g_act) # currently active labels if data['action_choice'] in ('remove', 'replace'): items = items.difference(values['g_r']) if data['action_choice'] in ('add', 'replace'): items = items.union(values['g_a']) labeling.update(items) # manage personal labels if values['p_a'] or values['p_r']: if data['action_choice'] in ('overwrite'): items = set(values['p_a']) labeling.pers_update(self.p_labels.difference(items), False) labeling.pers_update(items, True) else: if data['action_choice'] in ('remove', 'replace'): labeling.pers_update( set(p_act).intersection(values['p_r']), False) if data['action_choice'] in ('add', 'replace'): labeling.pers_update(values['p_a'], True) obj.reindexObject(['labels'])