# -*- coding: utf-8 # Copyright 2005 by Meelis Mets from Products.Archetypes.public import BaseSchema, Schema from Products.Archetypes.public import StringField, LinesField from Products.Archetypes.public import LinesWidget, TextAreaWidget, IdWidget, StringWidget, SelectionWidget from Products.Archetypes.public import OrderedBaseFolder, registerType from Globals import InitializeClass from Products.CMFCore.utils import getToolByName from Permissions import * from types import UnicodeType from AccessControl import ClassSecurityInfo, Unauthorized from config import PROJECT_NAME, SUBJECTS schema = BaseSchema class RegistrationDataBase(OrderedBaseFolder): """ RegistrationDataBase type """ meta_type = "RegistrationDataBase" archetype_name = "RegistrationDataBase" exclude_from_nav = True global_allow = 0 filter_content_types = True allowed_content_types = () 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}/registration_form_pp', 'permissions': ('View',), }, { 'id':'practice1', 'name':'Practice 1', 'action':'string:${object_url}/registration_form', 'permissions': ('View',), }, ) def __init__(self, id): self.id = id self.registrationData = [] self.deadline = ['2006/09/01',] self.practice = [{'deadline':'2006/09/01','data':[]},{'deadline':'2006/09/01','data':[]},{'deadline':'2006/09/01','data':[]},{'deadline':'2006/09/01','data':[],}] def getLinks(self): """ return links """ return [{'title':'Pre practice','url':'registration_form_pp'},{'title':'Practice','url':'registration_form'}] def addRegistrationData(self, REQUEST): """add registration data to array""" practice = int(REQUEST.get('practice')) name = REQUEST.get('name') id = str(REQUEST.AUTHENTICATED_USER) if practice == 0: school1 = REQUEST.get('school1') school2 = '' subjects = [REQUEST.get('subject'),] else: school1 = REQUEST.get('school1') school2 = REQUEST.get('school2') subjects = [REQUEST.get('sub1'),REQUEST.get('sub2'),REQUEST.get('sub3'),] print practice self.practice[practice]['data'].append({'name':name, 'id': id,'school':'None','school1':school1,'school2':school2, 'status':'pending','subjects':subjects}) self._p_changed = 1 def acceptRegistrationDataByUser(self, REQUEST): """gets registration data from array by user id""" itemNr = 0 practice = int(REQUEST.get('practice')) for userInfo in self.practice[practice]['data']: if userInfo['status']=='pending': if REQUEST.get('cb_'+userInfo['id'])=='on': info = self.practice[practice]['data'][itemNr] info['status'] = 'accepted' pm = getToolByName(self, 'portal_membership') member = pm.getMemberById(str(REQUEST.AUTHENTICATED_USER)) info['school'] = member.school self.practice[practice]['data'][itemNr] = info itemNr = itemNr + 1 self._p_changed = 1 return 'None' def rejectRegistrationDataByUser(self, REQUEST): """gets registration data from array by user id""" itemNr = 0 practice = int(REQUEST.get('practice')) for userInfo in self.practice[practice]['data']: if REQUEST.get('ac_'+userInfo['id'])=='on' or REQUEST.get('cb_'+userInfo['id'])=='on': self.practice[practice]['data'].pop(itemNr) self._p_changed = 1 return 1 itemNr = itemNr + 1 return 'None' def getRegistrationData(self,practice): """gets registration data from array""" return self.practice[practice]['data'] def getRegistrationDataByStatus(self, practice, status): """gets pending registration data from array""" regData = [] for userInfo in self.practice[practice]['data']: if userInfo['status']==status: regData.append(userInfo) return regData def getPendingRegistrationDataBySchool(self, practice, school): """gets pending registration data by school from array""" regData = [] for userInfo in self.practice[practice]['data']: if userInfo['status']=='pending': if userInfo['school1']==school: regData.append(userInfo) if userInfo['school2']==school: regData.append(userInfo) return regData def getAcceptedRegistrationData(self, practice): """gets accepted registration data from array""" regData = [] for userInfo in self.practice[practice]['data']: if userInfo['status']=='accepted': regData.append(userInfo) return regData def getAcceptedRegistrationDataBySchool(self, practice, school): """gets accepted registration data from array""" regData = [] for userInfo in self.practice[practice]['data']: if userInfo['school']==school: regData.append(userInfo) return regData def getRegistrationDataByUser(self, practice, member): """gets registration data from array by user id""" itemNr = 0 for userInfo in self.practice[practice]['data']: if str(userInfo['id'])==str(member): return self.practice[practice]['data'][itemNr] itemNr = itemNr + 1 return 'None' def getStatusOfMember(self, practice, member): """ gets some info for member """ for userInfo in self.practice[practice]['data']: if str(userInfo['id'])==str(member): return userInfo['status'] return 'sleeping' def delFromRegistrationData(self, REQUEST): """ removes user from queue """ id = str(REQUEST.AUTHENTICATED_USER) practice = int(REQUEST.get('practice')) itemNr = 0 for userInfo in self.practice[practice]['data']: if str(userInfo['id'])==id: self.practice[practice]['data'].pop(itemNr) self._p_changed = 1 return 1 itemNr = itemNr + 1 def getSchoolsFromDB(self): """returns list of schools from school database""" database = getattr(self,'schooldatabase') data = database.getFolderContents() schoolLocation = [] schoolList = [] locations = 0 for school in data: school = getattr(database,school.id) if school.location in schoolLocation: locs = 0 for loc in schoolLocation: if loc == school.location: schoolList[locs]['schools'].append(school.title) locs = locs + 1 else: schoolLocation.append(school.location) schoolList.append({'location':school.location, 'schools': []}) schoolList[locations]['schools'].append(school.title) locations = locations + 1 return schoolList def getSchoolLocationsFromDB(self): """returns list of school locations from school database""" database = getattr(self,'schooldatabase') data = database.getFolderContents() schoolLocation = [] for school in data: if school.location in schoolLocation: pass else: schoolLocation.append(school.location) return schoolLocation def getSchoolsByLocationFromDB(self, loc): """returns list of schools by locations from school database""" locations = self.getSchoolLocationsFromDB() if loc==None: loc='0' location = locations[int(loc)] database = getattr(self,'schooldatabase') data = database.getFolderContents() schoolList = [] for school in data: school = getattr(database,school.id) if school.location == location: schoolList.append({'name':school.title, 'id':school.id}) return schoolList def getSchoolsByLocationAndBySubjects(self, loc, sub): """returns list of schools by locations from school database""" locations = self.getSchoolLocationsFromDB() subjects = [0,0,0] if loc==None: loc='0' c = 0 for s in sub: if sub[c]!=None: subjects[c]=sub[c] c = c + 1 location = locations[int(loc)] database = getattr(self,'schooldatabase') data = database.getFolderContents() schoolList = [] for school in data: school = getattr(database,school.id) if school.location == location: if school.subjects[int(subjects[0])]['nr']>0 and school.subjects[int(subjects[1])]['nr']>0 and school.subjects[int(subjects[2])]['nr']>0: nr = school.subjects[int(subjects[0])]['nr'] if school.subjects[int(subjects[1])]['nr'] < nr: nr = school.subjects[int(subjects[1])]['nr'] if school.subjects[int(subjects[2])]['nr'] < nr: nr = school.subjects[int(subjects[2])]['nr'] schoolList.append({'name':school.title, 'id':school.id, 'nr':nr}) return schoolList def getSchoolByIdFromDB(self, id): """ gets school by its id """ database = getattr(self,'schooldatabase') ob = getattr(database,str(id)) return ob.Title() def getSubjects(self): """returns subject list""" return SUBJECTS # this block is not needed anymore! def quote(self, string): """aaa""" import urllib return urllib.quote(string) def unquote(self, string): """aaa""" import urllib return urllib.unquote(string) # def manage_afterAdd(self,item,container): # """sets permissions""" # self.manage_permission('Manage Portal', ('Manager','School') ,1) # self.manage_permission('View', ('Manager','School') ,1) registerType(RegistrationDataBase, PROJECT_NAME)