# -*- coding: utf-8 # Copyright 2005 by Meelis Mets from Products.Archetypes.public import BaseSchema, Schema from Products.Archetypes.public import StringField, LinesField, DateTimeField, TextField, ReferenceField, FileField, ComputedField from Products.Archetypes.public import LinesWidget, TextAreaWidget, IdWidget, StringWidget, SelectionWidget, CalendarWidget, RichWidget, MultiSelectionWidget, FileWidget, ComputedWidget from Products.ATReferenceBrowserWidget.ATReferenceBrowserWidget import ReferenceBrowserWidget from Products.Eportfolio.Widgets import DictField, LinksWidget from Products.Archetypes.public import BaseContent, OrderedBaseFolder, registerType from Globals import InitializeClass from Products.CMFCore.utils import getToolByName from Products.CMFCore.permissions import View from Products.CMFCore.permissions import ModifyPortalContent from Permissions import MarkTask, EditTask from Widgets import TargetWidget, AnswerReferenceWidget from Common import CommonMix as Common from AccessControl import ClassSecurityInfo, Unauthorized from config import PROJECT_NAME, PERMISSIONS schema = BaseSchema class linksWidgetFunctions: """Functions for links""" def getLinks(self): """ returns links """ return self.getAttachedLinks() def setAttachedLinks(self, val): if val=='' or val == (): return if val[0]!='' and val[1]!='': titles = val[0].split(";") urls = val[1].split(";") c = 0 delKeys = [] for r in self.getAttachedLinks(): if r not in titles: delKeys.append(r) f = self.getField('attachedLinks').delValue(self, delKeys) for t in titles: u = urls[c] f = self.getField('attachedLinks').setValue(self, t, u) c = c + 1 def getLinkValues(self): """returns stuff""" links = self.getAttachedLinks() stuff = ['',''] for title in links: stuff[0]=stuff[0]+title+";" stuff[1]=stuff[1]+links[title]+";" return stuff class BaseTask(BaseContent, Common, linksWidgetFunctions): """ BaseTask """ meta_type = "BaseTask" archetype_name = "BaseTask" global_allow = 0 allow_discussion = 0 #commenting is turned off filter_content_types = True allowed_content_types = () security = ClassSecurityInfo() schema = schema + Schema(( DateTimeField('Deadline', required=1, accessor="getDeadline", searchable=1, index='KeywordIndex:schema', widget=CalendarWidget( format="%d.%m.%Y", show_hm=False, label="Deadline", label_msgid='label_deadline', description="Deadline of task.", description_msgid='desc_deadline', visible={'view':'invisible','edit':'visible'}, i18n_domain="eportfolio"), ), LinesField('Target', accessor = 'getTarget', searchable = 1, index='KeywordIndex:schema', widget = TargetWidget( label = 'Target', label_msgid='label_target', description="Target who gets that task.", description_msgid='desc_target', visible={'view':'invisible','edit':'visible'}, i18n_domain="eportfolio" ), ), LinesField('Lecturer', accessor = 'getLecturer', searchable = 1, index='KeywordIndex:schema', widget = TargetWidget( label = 'Lecturer', label_msgid='label_lecturer', description="Lecturer who get this task for marking.", description_msgid='desc_lecturer', visible={'view':'invisible','edit':'visible'}, i18n_domain="eportfolio" ), ), StringField('somePermissions', mutator = 'setSomePermissions', widget = StringWidget( label = 'Target', visible={'view':'hidden','edit':'hidden'}, ), ), ReferenceField('refsForAnswers', accessor = 'getrefsForAnswers', relationship = 'References', mutator = 'setrefsForAnswers', allowed_types = ('CompetencyGroup'), multiValued= True, widget = AnswerReferenceWidget( destination_types = ('CompetencyGroup'), label= "Related competencies", description = "Select competencies that will be linked with answers to this task.", label_msgid = 'label_related_competencies', description_msgid = 'desc_related_competencies', i18n_domain = "eportfolio", visible = {'view':'visible', 'edit':'visible'},) ), FileField('attachedFile', accessor="getAttachedFile", widget=FileWidget( label="Attached File", label_msgid='label_attached_file', description="", description_msgid='', visible={'view':'visible','edit':'visible'}, i18n_domain="eportfolio" ), ), )) def start(self): """ """ return self.getDeadline() def end(self): """ """ return self.getDeadline() def getStatus(self): """return status""" #print getattr(self,'review_state') return self.getProperty('review_state') def getWorkflowStatus(self, object): """ """ status = getToolByName(self, 'portal_workflow', None).getStatusOf('task_workflow',object) if status != None: state = status['review_state'] else: state = ' ' return state def getTargets(self): """ Targets array """ targets = ['None',] groupstool = getToolByName(self, 'portal_groups') groups = groupstool.listGroupIds() for target in groups: target = str(target) if target not in ['Administrators', 'Reviewers']: targets.append(target) return targets def calcTargets(self): """ calculate targets. targets is a list of users. """ return self.getTarget() def getStudentsByTarget(self, target): """ students array """ students = [] groupstool = getToolByName(self, 'portal_groups') groups = groupstool.listGroupIds() roles = PERMISSIONS target = target[0] for group in groups: if str(target) == str(group): group2 = groupstool.getGroupById(group) for m in group2.getGroupMembers(): access = True for role in m.getRolesInContext(self): if role in roles['notInTaskList']: access = False if access: students.append(m.id) return students def setSomePermissions(self,data): """set permissions""" #students permissions self.setPermissionsHere(self.getTarget(),'Student') #lecturers permissions self.setPermissionsHere(self.getLecturer(),'Lecturer') def getReportHTML(self): """ generate report for assignment """ from Report import Report print 'getReport' if not hasattr(self, 'report'): self.report = Report('report') self.report.setReportFields(['email', 'fullname', 'assignment_'+self.UID()]) userslist = [] groupslist = [] self.report.setTarget(self.getTarget()) return self.report.getReportHTML() def hasAnswer(self, uname=None, asObj = False): """ has answer """ if uname is None: uname = str(self.REQUEST.AUTHENTICATED_USER) q = {'targetUID': self.UID(), 'relationship': 'answer_to' } rc = getToolByName(self, 'reference_catalog') res = rc(q) for r in res: v = rc.lookupObject(r.UID) obj = v.getSourceObject() if obj.Creator() == uname: if not obj.hasAnswerContent(): obj.aq_parent._delObject(obj.id) return False if asObj: return obj return True return False def getAnswer(self, uname = None): """ get user's answer """ obj = self.hasAnswer(uname, True) return obj def countAnswers(self): """ task answers """ q = {'targetUID': self.UID(), 'relationship': 'answer_to' } rc = getToolByName(self, 'reference_catalog') res = rc(q) counter = 0 for r in res: v = rc.lookupObject(r.UID) obj = v.getSourceObject() objstatus = self.getWorkflowStatus(obj) print obj, objstatus if objstatus != 'restricted_accepted' and objstatus != 'public_accepted': counter = counter + 1 print '-----------------' return counter def isTask(self): deadline = self.getDeadline() #print 'Deadline: ',deadline if deadline == None: return False return True registerType(BaseTask, PROJECT_NAME)