Test coverage for vnccollab.theme.vocabularies
1: import logging
1: import time
1: from pytz import common_timezones
1: from pyactiveresource.activeresource import ActiveResource
1: from zope.interface import implements
1: from zope.schema.interfaces import IVocabularyFactory
1: from zope.schema.vocabulary import SimpleTerm, SimpleVocabulary
1: from zope.component import getUtility
1: from Products.CMFCore.utils import getToolByName
1: from Products.CMFPlone.utils import safe_unicode
1: from plone import api
1: from plone.registry.interfaces import IRegistry
1: from plone.memoize import ram
1: from vnccollab.theme import messageFactory as _
1: from vnccollab.theme.util import getAllActiveResources
1: from vnccollab.theme.portlets.zimbra_mail import logException
1: from vnccollab.theme.config import REDMINE_ENUMERATORS_CACHE_TIME
1: logger = logging.getLogger('vnccollab.theme.redmine_enumerator_vocabularies')
1: def cachemethod(interval):
1: def _cachekey(method, self, *args):
"""Time and settings based cache"""
>>>>>> return hash(tuple(list(args) + [interval]))
1: return _cachekey
1: @ram.cache(cachemethod(time.time() // REDMINE_ENUMERATORS_CACHE_TIME))
def getRedmineEnumerators(url, username, password):
"""Returns redmine enumerators:
* projects
* trackers
* priorities
* users
"""
>>>>>> data = {}
# projects, result is sensitive to user, so only those projects are returned
# where current logged in user has an access
>>>>>> projects = []
>>>>>> Project = type("Project", (ActiveResource,), {'_site': url, '_user':
>>>>>> username, '_password': password})
>>>>>> for item in getAllActiveResources(Project):
>>>>>> projects.append((item.id, item.name))
# projects.sort(lambda x,y:cmp(x[1], y[1]))
>>>>>> data['projects'] = tuple(projects)
# trackers
# TODO: redmine 1.1 do not support tracker REST API call, so also hard-code
# it so far
# trackers = []
# Tracker = type("Tracker", (ActiveResource,), {'_site': url, '_user':
# username, '_password': password})
# for item in getAllActiveResources(Tracker):
# trackers.append((item.id, item.name))
# trackers.sort(lambda x,y:cmp(x[1], y[1]))
# data['trackers'] = tuple(trackers)
data['trackers'] = (
>>>>>> ('15', '01 - PSR - Presales Request'),
>>>>>> ('16', '02 - RFP -Request For Proposal'),
>>>>>> ('17', '03 - APP - APProval'),
>>>>>> ('18', '04 - FR - Feature Request'),
>>>>>> ('19', '05 - WR - Work Request'),
>>>>>> ('20', '06 - BR - Bug Report'),
>>>>>> ('21', '07 - CR - Change Request'),
>>>>>> ('22', '08 - SO - Sign Off'),
>>>>>> ('23', 'SR - Support Request')
)
# priorities
# TODO: switch to using REST API after this ticket is closed:
# http://www.redmine.org/issues/7402
# for now we are just hard-coding this list
# priorities = []
# IssuePriority = type("IssuePriority", (ActiveResource,), {'_site': url,
# '_user': username, '_password': password})
# for item in getAllActiveResources(IssuePriority):
# priorities.append((item.id, item.name))
# priorities.sort(lambda x,y:cmp(x[1], y[1]))
# data['priorities'] = tuple(priorities)
>>>>>> data['priorities'] = (('3', 'Low'), ('4', 'Normal'), ('5', 'High'),
>>>>>> ('6', 'Urgent'), ('7', 'Immediate'))
# users
>>>>>> users = []
>>>>>> User = type("User", (ActiveResource,), {'_site': url, '_user':
>>>>>> username, '_password': password})
# only Redmine Administrator could do this call
>>>>>> try:
>>>>>> for item in getAllActiveResources(User):
>>>>>> users.append((item.id, '%s %s' % (item.firstname, item.lastname)))
>>>>>> except Exception, e:
>>>>>> pass
>>>>>> users.sort(lambda x,y:cmp(x[1], y[1]))
>>>>>> data['users'] = tuple(users)
>>>>>> return data
2: class RedmineVocabularyFactory(object):
1: implements(IVocabularyFactory)
1: def __init__(self, resource):
4: self.resource = resource
1: def __call__(self, context):
# get authenticated user
>>>>>> mtool = getToolByName(context, 'portal_membership')
>>>>>> member = mtool.getAuthenticatedMember()
>>>>>> if not member:
>>>>>> return SimpleVocabulary([])
>>>>>> username, password = member.getProperty('redmine_username', ''), \
>>>>>> safe_unicode(member.getProperty('redmine_password',
>>>>>> '')).encode('utf-8')
>>>>>> registry = getUtility(IRegistry)
>>>>>> url = registry.get('vnccollab.theme.redmine.url')
>>>>>> field_id = registry.get(
>>>>>> 'vnccollab.theme.redmine.plone_uid_field_id')
>>>>>> if not (username and password and url and field_id):
>>>>>> return SimpleVocabulary([])
>>>>>> try:
>>>>>> data = getRedmineEnumerators(url, username, password)
>>>>>> except Exception, e:
>>>>>> logException(_(u"Error during fetching redmine enumerators"),
>>>>>> context=context, logger=logger)
>>>>>> return SimpleVocabulary([])
>>>>>> return SimpleVocabulary([SimpleTerm(key, key, value)
>>>>>> for key, value in data.get(self.resource, ())])
1: ProjectsRedmineVocabulary = RedmineVocabularyFactory('projects')
1: TrackersRedmineVocabulary = RedmineVocabularyFactory('trackers')
1: PrioritiesRedmineVocabulary = RedmineVocabularyFactory('priorities')
1: UsersRedmineVocabulary = RedmineVocabularyFactory('users')
2: class TimeZonesVocabularyFactory(object):
"""Returns list of common timezones with user friendly titles.
It uses python timezone library: pytz.
1: """
1: implements(IVocabularyFactory)
1: def __call__(self, context):
12: terms = []
5184: for zone_name in common_timezones:
# prepare zone title: America/New_York -> America/New York
5172: terms.append(SimpleTerm(zone_name, zone_name,
5172: zone_name.replace('_', ' ')))
12: return SimpleVocabulary(terms)
1: TimeZonesVocabulary = TimeZonesVocabularyFactory()
2: class ATLinkVocabularyFactory(object):
1: '''Return vocabulary with references to ATLink objects'''
1: implements(IVocabularyFactory)
1: def __call__(self, context):
>>>>>> catalog = getToolByName(context, 'portal_catalog')
>>>>>> brains = catalog.searchResults(Type = 'Link')
>>>>>> terms = [SimpleTerm(value=x.getObject(),
token=x.UID,
title=x.getObject().Title())
>>>>>> for x in brains]
>>>>>> return SimpleVocabulary(terms)
1: ATLinkVocabulary = ATLinkVocabularyFactory()
2: class SimpleVocabularyFactory:
1: implements(IVocabularyFactory)
1: def __init__(self, lst):
4: self.lst = lst
1: def __call__(self, context):
>>>>>> terms = [SimpleTerm(value=x[0], token=x[0], title=x[1]) for x in self.lst]
>>>>>> vocabulary = SimpleVocabulary(terms)
>>>>>> return vocabulary
ZIMBRA_STATUS_VOCAB = [
1: ('NEED', 'Not initiated'),
1: ('INPR', 'In process'),
1: ('COMP', 'Complete'),
1: ('WAITING', 'Waiting'),
1: ('DEFERRED', 'Deferred')
]
ZIMBRA_PRIORITIES_VOCAB = [
1: ('1', 'High'),
1: ('5', 'Normal'),
1: ('9', 'Low')
]
11: ZIMBRA_PERCENTAGE_VOCAB = [(str(x), str(x)+'%') for x in range(0, 100, 10)]
NEW_TICKET_VOCAB = [
1: ('zimbra', 'CloudMail'),
1: ('redmine', 'CloudProject')
]
1: StatusZimbraTaskVocabulary = SimpleVocabularyFactory(ZIMBRA_STATUS_VOCAB)
1: PrioritiesZimbraTaskVocabulary = SimpleVocabularyFactory(ZIMBRA_PRIORITIES_VOCAB)
1: PercentageZimbraTaskVocabulary = SimpleVocabularyFactory(ZIMBRA_PERCENTAGE_VOCAB)
1: NewTicketVocabulary = SimpleVocabularyFactory(NEW_TICKET_VOCAB)