class Labeling(object): implements(ILabeling) adapts(ILabelSupport) def __init__(self, context): self.context = context self.jar = ILabelJar(self.context) def update(self, label_ids): available_labels = self.jar.storage.keys() for label_id in label_ids: if label_id not in available_labels: raise LookupError( 'Cannot activate label: ' 'the label "{0}" is not in the label jar. ' 'Following labels ids are available: {1}'.format( label_id, ', '.join(available_labels))) # Do not replace self.storage, since it is a PersistentList! self.storage[:] = label_ids def active_labels(self): labels = [] for label_id in self.storage: try: labels.append(self.jar.get(label_id)) except KeyError: pass return sorted(labels, key=lambda cls: make_sortable(cls['title'])) def available_labels(self): for label in self.jar.list(): label['active'] = (label.get('label_id') in self.storage) yield label @property def storage(self): if getattr(self, '_storage', None) is None: annotation = IAnnotations(self.context) if ANNOTATION_KEY not in annotation: annotation[ANNOTATION_KEY] = PersistentList() self._storage = annotation[ANNOTATION_KEY] return self._storage
def __call__(self, context): terms = [] try: adapted = ILabelJar(context) except: # noqa return SimpleVocabulary(terms) user = api.user.get_current() for label in adapted.list(): if label['by_user']: terms.append( SimpleVocabulary.createTerm( '%s:%s' % (user.id, label['label_id']), '%s_%s' % (user.id, label['label_id']), safe_unicode(label['title']))) else: terms.append( SimpleVocabulary.createTerm(label['label_id'], label['label_id'], safe_unicode(label['title']))) return SimpleVocabulary(terms)
def get_labels_vocabulary(self): terms, p_labels, g_labels = [], [], [] context = self.get_labeljar_context() try: adapted = ILabelJar(context) except: return SimpleVocabulary(terms), [], [] self.can_change_labels = is_permitted(self.brains, perm='ftw.labels: Change Labels') for label in adapted.list(): if label['by_user']: p_labels.append(label['label_id']) terms.append( SimpleVocabulary.createTerm( '%s:' % label['label_id'], label['label_id'], u'{} (*)'.format(safe_unicode(label['title'])))) else: g_labels.append(label['label_id']) if self.can_change_labels: terms.append( SimpleVocabulary.createTerm( label['label_id'], label['label_id'], safe_unicode(label['title']))) return SimpleVocabulary(terms), set(p_labels), g_labels
def update_site(self): # add documentation message if 'doc' not in self.portal['messages-config']: add_message( 'doc', 'Documentation', u'<p>Vous pouvez consulter la <a href="http://www.imio.be/' u'support/documentation/topic/cp_app_ged" target="_blank">documentation en ligne de la ' u'dernière version</a>, ainsi que d\'autres documentations liées.</p>', msg_type='significant', can_hide=True, req_roles=['Authenticated'], activate=True) if 'doc2-0' in self.portal['messages-config']: api.content.delete(obj=self.portal['messages-config']['doc2-0']) # update front-page frontpage = self.portal['front-page'] if frontpage.Title() == 'Gestion du courrier 2.0': frontpage.setTitle(_("front_page_title")) frontpage.setDescription(_("front_page_descr")) frontpage.setText(_("front_page_text"), mimetype='text/html') # update portal title self.portal.title = 'Gestion du courrier' # for collective.externaleditor if 'MailingLoopTemplate' not in self.registry[ 'externaleditor.externaleditor_enabled_types']: self.registry['externaleditor.externaleditor_enabled_types'] = [ 'PODTemplate', 'ConfigurablePODTemplate', 'DashboardPODTemplate', 'SubTemplate', 'StyleTemplate', 'dmsommainfile', 'MailingLoopTemplate' ] # documentgenerator api.portal.set_registry_record( 'collective.documentgenerator.browser.controlpanel.' 'IDocumentGeneratorControlPanelSchema.raiseOnError_for_non_managers', True) # ftw.labels if not ILabelRoot.providedBy(self.imf): labels = { self.imf: [('Lu', 'green', True), ('Suivi', 'yellow', True)], self.omf: [], self.portal['tasks']: [] } for folder in labels: if not ILabelRoot.providedBy(folder): alsoProvides(folder, ILabelRoot) adapted = ILabelJar(folder) existing = [dic['title'] for dic in adapted.list()] for title, color, by_user in labels[folder]: if title not in existing: adapted.add(title, color, by_user) self.portal.manage_permission('ftw.labels: Manage Labels Jar', ('Manager', 'Site Administrator'), acquire=0) self.portal.manage_permission('ftw.labels: Change Labels', ('Manager', 'Site Administrator'), acquire=0) self.portal.manage_permission( 'ftw.labels: Change Personal Labels', ('Manager', 'Site Administrator', 'Member'), acquire=0) self.runProfileSteps('imio.dms.mail', steps=['imiodmsmail-mark-copy-im-as-read'], profile='singles') # INextPrevNotNavigable alsoProvides(self.portal['tasks'], INextPrevNotNavigable) # registry api.portal.set_registry_record( name= 'Products.CMFPlone.interfaces.syndication.ISiteSyndicationSettings.' 'search_rss_enabled', value=False) # activing versioning self.portal.portal_diff.setDiffForPortalType( 'task', {'any': "Compound Diff for Dexterity types"}) self.portal.portal_diff.setDiffForPortalType( 'dmsommainfile', {'any': "Compound Diff for Dexterity types"}) # change permission self.portal.manage_permission('imio.dms.mail: Write userid field', (), acquire=0) pf = self.portal.contacts['personnel-folder'] pf.manage_permission('imio.dms.mail: Write userid field', ('Manager', 'Site Administrator'), acquire=0) # ckeditor skin self.portal.portal_properties.ckeditor_properties.skin = 'moono-lisa' # update mailcontent options self.registry[ 'collective.dms.mailcontent.browser.settings.IDmsMailConfig.outgoingmail_edit_irn'] = u'hide' self.registry[ 'collective.dms.mailcontent.browser.settings.IDmsMailConfig.outgoingmail_increment_number'] = True # hide faceted actions paob = self.portal.portal_actions.object_buttons for act in ('faceted.sync', 'faceted.disable', 'faceted.enable', 'faceted.search.disable', 'faceted.search.enable', 'faceted.actions.disable', 'faceted.actions.enable'): if act in paob: paob[act].visible = False
class Labeling(object): implements(ILabeling) adapts(ILabelSupport) def __init__(self, context): self.context = context self.jar = ILabelJar(self.context) def update(self, label_ids): jar_keys = self.jar.storage.keys() # removes deselected labels for label_id in self.storage.keys( ): # use keys to avoid RuntimeError: dictionary changed size during iteration if label_id not in jar_keys: continue # do we remove key ?? label = self.jar.get(label_id) if label_id not in label_ids: if not label['by_user']: self.storage.pop(label_id) # adds selected labels for label_id in label_ids: if label_id not in jar_keys: raise LookupError( 'Cannot activate label: ' 'the label "{0}" is not in the label jar. ' 'Following labels ids are available: {1}'.format( label_id, ', '.join(jar_keys))) if label_id not in self.storage: self.storage[label_id] = PersistentList() def pers_update(self, label_ids, activate): user_id = self.user_id() if not user_id: return False if activate: for label_id in label_ids: if label_id not in self.storage: self.storage[label_id] = PersistentList() if user_id not in self.storage[label_id]: self.storage[label_id].append(user_id) else: for label_id in label_ids: if label_id not in self.storage: continue if user_id in self.storage[label_id]: self.storage[label_id].remove(user_id) if not self.storage[label_id]: self.storage.pop(label_id) return True def active_labels(self): # selected labels labels = [] for label_id in self.storage: try: label = self.jar.get(label_id) if label['by_user']: if self.user_id() in self.storage[label_id]: labels.append(label) else: labels.append(label) except KeyError: pass return sorted(labels, key=lambda cls: make_sortable(cls['title'])) def available_labels(self): # possible labels, marking active ones labels = [[], []] for label in self.jar.list(): if label['by_user']: label['active'] = (label['label_id'] in self.storage and self.user_id() in self.storage[label['label_id']]) labels[0].append(label) else: label['active'] = (label.get('label_id') in self.storage) labels[1].append(label) labels[0].sort(key=lambda cls: make_sortable(cls['title'])) labels[1].sort(key=lambda cls: make_sortable(cls['title'])) return labels def user_id(default=None): """ Return current userid """ cur_user = api.user.get_current() if not cur_user: return default return cur_user.getId() @property def storage(self): if getattr(self, '_storage', None) is None: annotation = IAnnotations(self.context) if ANNOTATION_KEY not in annotation: annotation[ANNOTATION_KEY] = PersistentMapping() self._storage = annotation[ANNOTATION_KEY] return self._storage
class TestLabeling(MockTestCase): layer = ADAPTERS_ZCML_LAYER def setUp(self): super(TestLabeling, self).setUp() Labeling.user_id = lambda x: TEST_USER_ID # needed to avoid plone.api.portal.get error self.root = self.providing_stub([ILabelRoot, IAttributeAnnotatable]) self.document = self.providing_stub( [ILabelSupport, IAttributeAnnotatable]) self.set_parent(self.document, self.root) self.replay() self.jar = ILabelJar(self.root) def test_adapter(self): self.assertTrue( queryAdapter(self.document, ILabeling), 'The labeling adapter is not registered for ILabeling') def test_available_labels(self): self.jar.add('Question', '#00FF00', False) self.jar.add('Read', 'red', True) labeling = ILabeling(self.document) self.assertEqual([[{ 'label_id': 'read', 'title': 'Read', 'color': 'red', 'active': False, 'by_user': True }], [{ 'label_id': 'question', 'title': 'Question', 'color': '#00FF00', 'active': False, 'by_user': False }]], list(labeling.available_labels())) def test_available_labels_empty(self): labeling = ILabeling(self.document) self.assertEqual([[], []], labeling.available_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 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_update__disable_labels(self): self.jar.add('Bug', 'red', False) self.jar.add('Question', 'green', False) self.jar.add('Feature', 'purple', True) labeling = ILabeling(self.document) labeling.update(['bug', 'question']) labeling.pers_update('feature', True) self.assertItemsEqual(['Bug', 'Feature', 'Question'], label_titles(labeling.active_labels())) labeling.update(['bug']) labeling.pers_update('feature', False) self.assertItemsEqual(['Bug'], label_titles(labeling.active_labels())) labeling.update([]) self.assertEqual([], 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', '', 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_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', '', 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_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()))
class TestLabeling(MockTestCase): layer = ADAPTERS_ZCML_LAYER def setUp(self): super(TestLabeling, self).setUp() self.root = self.providing_stub([ILabelRoot, IAttributeAnnotatable]) self.document = self.providing_stub([ILabelSupport, IAttributeAnnotatable]) self.set_parent(self.document, self.root) self.replay() self.jar = ILabelJar(self.root) def test_adapter(self): self.assertTrue( queryAdapter(self.document, ILabeling), 'The labeling adapter is not registered for ILabeling') def test_available_labels(self): self.jar.add('Question', '#00FF00') labeling = ILabeling(self.document) self.assertEqual( [{'label_id': 'question', 'title': 'Question', 'color': '#00FF00', 'active': False}], list(labeling.available_labels())) def test_available_labels_empty(self): labeling = ILabeling(self.document) self.assertEqual([], list(labeling.available_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') 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_update__disable_labels(self): self.jar.add('Bug', 'red') self.jar.add('Question', 'green') self.jar.add('Feature', 'purple') labeling = ILabeling(self.document) labeling.update(['bug', 'question', 'feature']) self.assertItemsEqual(['Bug', 'Feature', 'Question'], label_titles(labeling.active_labels())) labeling.update(['bug']) self.assertItemsEqual(['Bug'], label_titles(labeling.active_labels())) labeling.update([]) self.assertEqual([], 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(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_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_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()))
class Labeling(object): implements(ILabeling) adapts(ILabelSupport) def __init__(self, context): self.context = context self.jar = ILabelJar(self.context) def update(self, label_ids): jar_keys = self.jar.storage.keys() # removes deselected labels for label_id in self.storage.keys(): # use keys to avoid RuntimeError: dictionary changed size during iteration if label_id not in jar_keys: continue # do we remove key ?? label = self.jar.get(label_id) if label_id not in label_ids: if not label['by_user']: self.storage.pop(label_id) # adds selected labels for label_id in label_ids: if label_id not in jar_keys: raise LookupError( 'Cannot activate label: ' 'the label "{0}" is not in the label jar. ' 'Following labels ids are available: {1}'.format( label_id, ', '.join(jar_keys))) if label_id not in self.storage: self.storage[label_id] = PersistentList() def pers_update(self, label_id, activate): user_id = self.user_id() if not user_id: return False if activate: if label_id not in self.storage: self.storage[label_id] = PersistentList() if user_id not in self.storage[label_id]: self.storage[label_id].append(user_id) else: if user_id in self.storage[label_id]: self.storage[label_id].remove(user_id) if not self.storage[label_id]: self.storage.pop(label_id) return True def active_labels(self): # selected labels labels = [] for label_id in self.storage: try: label = self.jar.get(label_id) if label['by_user']: if self.user_id() in self.storage[label_id]: labels.append(label) else: labels.append(label) except KeyError: pass return sorted(labels, key=lambda cls: make_sortable(cls['title'])) def available_labels(self): # possible labels, marking active ones labels = [[], []] for label in self.jar.list(): if label['by_user']: label['active'] = (label['label_id'] in self.storage and self.user_id() in self.storage[label['label_id']]) labels[0].append(label) else: label['active'] = (label.get('label_id') in self.storage) labels[1].append(label) labels[0].sort(key=lambda cls: make_sortable(cls['title'])) labels[1].sort(key=lambda cls: make_sortable(cls['title'])) return labels def user_id(default=None): """ Return current userid """ cur_user = api.user.get_current() if not cur_user: return default return cur_user.getId() @property def storage(self): if getattr(self, '_storage', None) is None: annotation = IAnnotations(self.context) if ANNOTATION_KEY not in annotation: annotation[ANNOTATION_KEY] = PersistentMapping() self._storage = annotation[ANNOTATION_KEY] return self._storage