# -*- coding: utf-8
# Copyright 2005 by Meelis Mets
from Products.Archetypes.public import BaseSchema, Schema
from Products.Archetypes.public import StringField, LinesField, DateTimeField
from Products.Archetypes.public import LinesWidget, TextAreaWidget, IdWidget, StringWidget, SelectionWidget, CalendarWidget
from Products.Archetypes.public import BaseContent, OrderedBaseFolder, registerType
from Globals import InitializeClass
from Products.CMFCore.utils import getToolByName
from AccessControl import ClassSecurityInfo, Unauthorized
from config import PROJECT_NAME
schema = BaseSchema
class ToDo(BaseContent):
""" ToDo """
meta_type = "ToDo"
archetype_name = "ToDo"
global_allow = 0
filter_content_types = True
allowed_content_types = ()
security = ClassSecurityInfo()
schema = schema + Schema((
DateTimeField('Deadline',
required=1,
accessor="getDeadline",
isMetadata=1,
searchable=1,
widget=CalendarWidget(
format="%d.%m.%Y",
label="Deadline",
label_msgid='label_deadline',
description="Deadline of task.",
description_msgid='desc_deadline',
visible={'view':'invisible','edit':'visible'},
i18n_domain="eportfolio"),
),
LinesField('Target',
required=1,
accessor="getTarget",
isMetadata=1,
searchable=1,
vocabulary='getTargets',
widget=SelectionWidget(
format="select",
label="Target",
label_msgid='label_target',
description="Target whos get that task.",
description_msgid='desc_target',
visible={'view':'invisible','edit':'visible'},
i18n_domain="eportfolio"),
),
StringField('task',
widget=TextAreaWidget(
label="Task",
description="",
label_msgid='label_task',
description_msgid='description_task',
visible={'view':'visible','edit':'visible'},
i18n_domain="eportfolio"),
),
))
actions = (
{
'id':'view',
'name':'View',
'action':'string:${object_url}/task_view',
'permissions': ('View',),
},
{
'id':'edit',
'name':'Edit',
'action':'string:${object_url}/base_edit',
'permissions': ('Manage Portal',),
},
{
'id':'metadata',
'name':'Properties',
'action':'string:${object_url}/base_metadata',
'permissions': ('Manage Portal',),
},
)
def getTargets(self):
""" Targets array """
targets = []
groups = self.prefs_user_group_search('','groups')
for target in groups:
target = str(target)
if target not in ['Administrators', 'Reviewers']:
targets.append(target)
return targets
def getAnswerText(self, student):
""" gets students answer if possible"""
homeFolder = self.portal_membership.getHomeFolder(student)
if hasattr(homeFolder, 'tasks'):
taskFolder = getattr(homeFolder, 'tasks')
answer_id = self.id+"-answer"
if hasattr(taskFolder, answer_id):
answerObj = getattr(taskFolder, answer_id)
return answerObj.answer
else:
return ""
else:
return ""
def getAccepted(self, student):
""" gets students answer if possible"""
homeFolder = self.portal_membership.getHomeFolder(student)
if hasattr(homeFolder, 'tasks'):
taskFolder = getattr(homeFolder, 'tasks')
answer_id = self.id+"-answer"
if hasattr(taskFolder, answer_id):
answerObj = getattr(taskFolder, answer_id)
if answerObj.accepted!='none':
return True
else:
return False
else:
return False
else:
return False
def answerTask(self, REQUEST):
""" Copies already made form to users forms folder """
user = str(REQUEST.AUTHENTICATED_USER)
homeFolder = self.portal_membership.getHomeFolder(user)
taskFolder = getattr(homeFolder, 'tasks')
answer_id = str(self.id)+"-answer"
if hasattr(taskFolder, answer_id):
answerObj = getattr(taskFolder,answer_id)
answerObj.answer = REQUEST.get('answer')
else:
#make new answer
from Task import Answer
new_answer = Answer(answer_id)
taskFolder._setObject(new_answer.id, new_answer)
answerObj=getattr(taskFolder, new_answer.id)
answerObj.setTitle(self.title)
answerObj.answer = REQUEST.get('answer')
answerObj.task = self.task
answerObj.parent_task = self.id
answerObj.reindexObject()
self._p_changed = 1
return answerObj
def allowAccept(self, user):
""" is accepting allowed no, not here """
return False
registerType(ToDo, PROJECT_NAME)
class Answer(BaseContent):
""" Answer """
meta_type = "Answer"
archetype_name = "Answer"
global_allow = 0
filter_content_types = True
allowed_content_types = ()
security = ClassSecurityInfo()
schema = schema + Schema((
StringField('task',
widget=TextAreaWidget(
label="Task",
description="",
label_msgid='label_task',
description_msgid='description_task',
visible={'view':'visible','edit':'visible'},
i18n_domain="eportfolio"),
),
StringField('answer',
widget=TextAreaWidget(
label="Answer",
description="",
label_msgid='label_answer',
description_msgid='description_answer',
visible={'view':'visible','edit':'visible'},
i18n_domain="eportfolio"),
),
))
actions = (
{
'id':'view',
'name':'View',
'action':'string:${object_url}/task_view',
'permissions': ('View',),
},
{
'id':'edit',
'name':'Edit',
'action':'string:${object_url}/base_edit',
'permissions': ('Manage Portal',),
},
{
'id':'metadata',
'name':'Properties',
'action':'string:${object_url}/base_metadata',
'permissions': ('Manage Portal',),
},
)
def saveTask(self, REQUEST):
""" Saves data """
self.answer = REQUEST.get('answer')
return 1
def getAnswerText(self, student):
""" gets answer """
return self.answer
def getAccepted(self, student):
""" gets answer """
if self.accepted!='none':
return True
else:
return False
def acceptAnswer(self):
""" Accept this answer """
self.accepted = "True"
self._p_changed = 1
return 1
def allowAccept(self, user):
""" is accepting allowed """
if user in ['metz'] and self.accepted=='none':
return True
return False
def __init__(self, id):
self.id = id
self.parent_task = "none"
self.accepted = "none"
registerType(Answer, PROJECT_NAME)
class TaskFolder(OrderedBaseFolder):
""" TaskFolder """
meta_type = "TaskFolder"
archetype_name = "TaskFolder"
global_allow = 0
exclude_from_nav = True
filter_content_types = True
allowed_content_types = ('ToDo',)
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': ('Manage Portal',),
},
{
'id':'metadata',
'name':'Properties',
'action':'string:${object_url}/base_metadata',
'permissions': ('Manage Portal',),
},
)
def __init__(self, id):
self.id = id
self.largeIcon = "presentation.gif"
def getLargeIcon(self):
""" returns subfolders large icon """
return self.largeIcon
registerType(TaskFolder, PROJECT_NAME)
class WorkSheet(BaseContent):
""" WorkSheet """
meta_type = "WorkSheet"
archetype_name = "WorkSheet"
global_allow = 0
exclude_from_nav = True
filter_content_types = True
security = ClassSecurityInfo()
schema = schema
actions = (
{
'id':'view',
'name':'View',
'action':'string:${object_url}/list_view',
'permissions': ('View',),
},
{
'id':'prepractice',
'name':'Pre practice',
'action':'string:${object_url}/worksheet_view?practice=0',
'permissions': ('View',),
},
{
'id':'practice 1',
'name':'Practice 1',
'action':'string:${object_url}/worksheet_view?practice=1',
'permissions': ('View',),
},
{
'id':'edit',
'name':'Edit',
'action':'string:${object_url}/base_edit',
'permissions': ('Manage Portal',),
},
{
'id':'metadata',
'name':'Properties',
'action':'string:${object_url}/base_metadata',
'permissions': ('Manage Portal',),
},
)
def __init__(self, id):
self.id = id
self.largeIcon = "forms.gif"
def getLargeIcon(self):
""" returns subfolders large icon """
return self.largeIcon
def getLinks(self):
""" return links """
return [{'title':'Pre practice','url':'worksheet_view?practice=0'},{'title':'Practice','url':'worksheet_view?practice=1'}]
def getThisTime(self):
""" Time """
import time
return str(time.strftime('%Y/%m/%d'))
def getExpiration(self, deadline):
""" Expiration """
if deadline < self.getThisTime():
return True
else:
return False
def reverseDate(self, date):
"""reverse date string yyyy/mm/dd to dd/mm/yyyy"""
date = date.split('/')
return date[2]+'/'+date[1]+'/'+date[0]
def getTasks(self, target):
""" Tasks """
query_string = {'SearchableText': {'operator': 'or', 'query': (target,)}}
pcatalog = getToolByName(self, 'portal_catalog')
results = pcatalog.searchResults(query_string)
return results
def getTaskList(self, target, practice):
""" Tasks """
taskList = []
student = self.Creator()
#registration
regDatabase = getattr(self,'registrationdatabase')
regStatus = regDatabase.getStatusOfMember(practice, student)
if practice==0:
prac = 'registration_form_pp'
else:
prac = 'registration_form'
if regStatus == 'sleeping':
status = ''
elif regStatus == 'pending':
status = ''
elif regStatus == 'accepted':
status = ''
link = self.portal_url()+'/registrationdatabase/'+prac
deadline = regDatabase.practice[practice]['deadline']
title = 'Registreeru praktikale'
locked = self.getExpiration(deadline)
taskList.append({'deadline':deadline,'title':title,'status':status,'locked':locked,'link':link})
#tasks
for task in self.getTasks(target):
owner = task.Creator
ownerFolder = self.portal_membership.getHomeFolder(owner)
if task.meta_type=='ToDo':
taskFolder = getattr(ownerFolder, 'tasks')
answerFolder = getattr(self.aq_parent, 'tasks')
answer_id = task.id+"-answer"
else:
taskFolder = getattr(ownerFolder, 'forms')
answerFolder = getattr(self.aq_parent, 'forms')
answer_id = task.id+"-copy"
task_obj = getattr(taskFolder, task.id)
deadline = str(task_obj.Deadline)
locked = self.getExpiration(deadline)
title = task.Title
status = ''
if hasattr(answerFolder, answer_id):
answerObj = getattr(answerFolder, answer_id)
if task_obj.meta_type=='Answer':
if answerObj.accepted == "none":
link = answerObj.absolute_url()
status = ''
else:
status = ''
link = answerObj.absolute_url()
else:
status = ''
link = answerObj.absolute_url()
else:
status = ''
link = task_obj.absolute_url()
taskList.append({'deadline':deadline,'title':title,'status':status,'locked':locked,'link':link})
taskList.sort()
return taskList
def getStudentsByGroup(self, group_name):
""" returns students of requested group """
group = self.prefs_user_group_search(group_name, 'groups')
group = group[0]
return group.getGroupMembers()
def getTasksDoneByStudent(self, student, task):
""" gets task from students home folder if it exists """
homeFolder = self.portal_membership.getHomeFolder(student)
if hasattr(homeFolder, 'tasks'):
taskFolder = getattr(homeFolder, 'tasks')
answer_id = task+"-answer"
if hasattr(taskFolder, answer_id):
answerObj = getattr(taskFolder, answer_id)
if answerObj.accepted == "none":
return {'status':'O','url':answerObj.absolute_url()}
else:
return {'status':'V','url':answerObj.absolute_url()}
return {'status':'X','url':'None'}
def getTasksDoneByLector(self, lector, task):
""" gets task from lectors home folder if it exists """
homeFolder = self.portal_membership.getHomeFolder(lector)
if hasattr(homeFolder, 'tasks'):
taskFolder = getattr(homeFolder, 'tasks')
task_id = task
if hasattr(taskFolder, task_id):
taskObj = getattr(taskFolder, task_id)
return taskObj.absolute_url()
return False
registerType(WorkSheet, PROJECT_NAME)