# -*- coding: utf-8 # Copyright 2006-2007 by Vahur Rebas from Products.Archetypes.public import BaseSchema, Schema from Products.Archetypes.public import StringField, LinesField from Products.Archetypes.public import LinesWidget, TextAreaWidget, IdWidget, StringWidget, SelectionWidget, MultiSelectionWidget from Widgets import TargetWidget from Products.Archetypes.public import BaseContent, registerType from Globals import InitializeClass from Products.CMFCore.utils import getToolByName from Common import CommonMix as Common from AccessControl import ClassSecurityInfo, Unauthorized from config import PROJECT_NAME, TASK_TYPES schema = BaseSchema + Schema(( # LinesField('groups', # mutator = 'setReportGroups', # accessor = 'getReportGroups', # vocabulary='vocReportGroups', # enforceVocabulary=True, # widget = MultiSelectionWidget( # label = 'Groups', # label_msgid='label_report_groups', # description = 'Select some groups', # description_msgid='desc_report_groups', # visible = {'view': 'hidden', 'edit':'visible'}, # i18n_domain="eportfolio" # ), # ), # LinesField('users', # mutator = 'setReportUsers', # accessor = 'getReportUsers', # vocabulary = 'vocReportUsers', # enforceVocabulary = True, # widget = MultiSelectionWidget( # label = 'Users', # label_msgid='label_report_users', # description = 'Select some users', # description_msgid='desc_report_users', # visible = {'view': 'hidden', 'edit':'visible'}, # i18n_domain="eportfolio" # ), # ), LinesField('Target', accessor = 'getTarget', mutator = 'setTarget', searchable = 1, index='KeywordIndex:schema', widget = TargetWidget( label = 'Groups and users', label_msgid='label_groups_and_users', description="Select groups and users.", description_msgid='desc_groups_and_users', visible={'view':'invisible','edit':'visible'}, i18n_domain="eportfolio" ), ), LinesField('report_fields', mutator = 'setReportFields', accessor = 'getReportFields', vocabulary = 'vocReportFields', enforceVocabulary = True, widget = MultiSelectionWidget( label = 'Columns', label_msgid='label_report_fields', description = 'Select some fields', description_msgid='desc_report_fields', visible = {'view': 'hidden', 'edit':'visible'}, i18n_domain="eportfolio" ), ), StringField('Report', accessor = 'getReportHTML', widget = StringWidget( label = 'Report', label_msgid='label_the_report', description = 'here is a report', description_msgid='desc_the_report', visible = {'view': 'visible', 'edit':'hidden'}, i18n_domain="eportfolio" ), ), )) def _assign_helper(uobj, uid, instance): """ assignment status helper. """ q = { 'targetUID': uid, 'relationship': 'answer_to' } rc = getToolByName(instance, 'reference_catalog') res = rc(q) msg = '' assignment_obj = rc.lookupObject(uid) deadline = str(assignment_obj.getDeadline()) + ' 23:59:59' deadline = assignment_obj.toLocalizedTime(deadline, long_format=1) curuser = str(instance.REQUEST.AUTHENTICATED_USER) for r in res: v = rc.lookupObject(r.UID) obj = v.getSourceObject() if obj.Creator() == uobj.getId(): ct = obj.toLocalizedTime(obj.ModificationDate(), long_format=1) if deadline >= ct: msg = """%s""" % ( obj.absolute_url(), ct ) else: msg = """%s""" % ( obj.absolute_url(), ct ) # references to this try: msg += " ("+ str(len(obj.getReplyReplies(obj)))+ "/" self_count = 0 for t in obj.getReplyReplies(obj): if curuser in t['object'].listCreators(): self_count += 1 msg += str(self_count)+ ")" except Unauthorized: pass break return msg def _assign_title(uobj, uid, instance): """ assignment title helper """ q = { 'targetUID': uid, 'relationship': 'answer_to' } rc = getToolByName(instance, 'reference_catalog') res = rc(q) msg = '' assignment_obj = rc.lookupObject(uid) sta = _translator('Assignment: ', instance) return sta+assignment_obj.Title() def _translator(val, instance): trs = getToolByName(instance, 'translation_service') res = val try: res = trs.utranslate('eportfolio', val, instance, instance).encode('utf-8') except UnicodeDecodeError: #print "unicode error", val, res pass return res FIELDS = { 'email' : { 'expr' : lambda **u: u['u'].getProperty('email'), 'title' : lambda **u: _translator('Email', u['instance']) }, 'fullname' : { 'expr' : lambda **u: u['u'].getProperty('title') or u['u'].getProperty('fullname'), 'title' : lambda **u: _translator('Fullname', u['instance']) }, 'userid' : { 'expr' : lambda **u: u['u'].getProperty('id'), 'title' : lambda **u: _translator('User ID', u['instance']) }, 'groups' : { 'expr' : lambda **u: ' '.join(u['u'].getGroups()), 'title' : lambda **u: _translator('Users groups', u['instance']) }, 'assignment': { 'expr' : lambda **u: _assign_helper(u['u'], u['extr'], u['instance']), 'title' : lambda **u: _assign_title(u['u'], u['extr'], u['instance']), 'hidden': True, }, } class Report(BaseContent, Common): """ Report """ meta_type = "Report" archetype_name = "Report" global_allow = 1 security = ClassSecurityInfo() schema = schema actions = ( { 'id':'view', 'name':'View', 'action':'string:${object_url}/base_view', 'permissions': ('View',), }, { 'id':'edit', 'name':'Edit', 'action':'string:${object_url}/base_edit', 'permissions': ('Modify portal content',), }, { 'id':'metadata', 'name':'Properties', 'action':'string:${object_url}/base_metadata', 'permissions': ('Manage Portal',), }, { 'id':'delete', 'name':'Delete', 'action':'string:${object_url}/delete_confirmation', 'permissions': ('Modify portal content',), }, { 'id':'competencies', 'name':'Associate with competency', 'action':'string:${object_url}/add_reference_to_competency?uid=${object/UID}', 'permissions': ('Modify portal content',), }, ) def vocReportFields(self): """ get a list of report columns/fields """ results = [] for f in FIELDS.keys(): if FIELDS.get(f).get('hidden', None): continue tmp = [f, FIELDS.get(f).get('title')(instance=self)] results.append(tmp) # get assignments... trs = getToolByName(self, 'translation_service') for a in self.taskmanager.objectValues(TASK_TYPES): assignment = 'Assignment: ' try: assignment = trs.utranslate('eportfolio', 'Assignment: ', self, self).encode('utf-8') except UnicodeDecodeError: pass tmp = ['assignment_'+a.UID(), assignment +a.Title()] #tmp = ['assignment_'+a.UID(), 'Assignment: '+a.Title()] results.append(tmp) #print "VOC::", results return results def getReportHTML(self, fields=None, groups=None): """ generate report """ mt = getToolByName(self, 'portal_membership') gt = getToolByName(self, 'portal_groups') ct = getToolByName(self, 'caching_tool') if not fields: fields = self.getReportFields() users = [] data = self.getTarget() groups = ct._getGroups() for group in groups: if group.id in data: #for user in gt.getGroupMembers(group.id): for user in ct._getGroupMembers(group): #print "adding user 1:", user, type(user) users.append(user) for user in data: if mt.getMemberById(user): #print "adding user 2:", user, type(user) users.append(user) res = "" trs = getToolByName(self, 'translation_service') for fie in fields: f = fie.split('_')[0] extrop = None try: extrop = fie.split('_')[1] except IndexError: pass tfunc = FIELDS.get(f).get('title') field = tfunc(u=None, extr=extrop, instance=self) res += "" % field #res += "" % trs.utranslate('eportfolio', FIELDS.get(f).get('title'), self, self) res += "" for r in users: u = None if isinstance(r, type('')): r = r.strip() u = mt.getMemberById(r) else: u = r res += "" for cie in fields: c = cie.split('_')[0] extrop = None try: extrop = cie.split('_')[1] except IndexError: pass expr = FIELDS.get(c).get('expr') args = FIELDS.get(c).get('args') if u == None: val = '' else: val = expr(u=u, extr=extrop, instance=self) if str(c) == 'fullname' and val == '': c = 'userid' extrop = None try: extrop = cie.split('_')[1] except IndexError: pass expr = FIELDS.get(c).get('expr') args = FIELDS.get(c).get('args') val = expr(u=u, extr=extrop, instance=self) try: res += "" % val except UnicodeDecodeError: try: res += '' except UnicodeDecodeError: pass # if not isinstance(val, unicode): # res += "" % val.decode('utf-8') # else: # res += "" % val res += "" res += "
%s%s
%s'+str(val)+'%s%s
" return res registerType(Report, PROJECT_NAME)