# -*- 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 SchoolDataBase(OrderedBaseFolder): """ SchoolDataBase product """ meta_type = "SchoolDataBase" archetype_name = "SchoolDataBase" global_allow = 0 filter_content_types = True allowed_content_types = ('School',) security = ClassSecurityInfo() schema = schema actions = ( { 'id':'view', 'name':'View', 'action':'string:${object_url}/schooldatabase_view', 'permissions': ('View',), }, { 'id':'registration', 'name':'Registration', 'action':'string:${object_url}/registration_view', 'permissions': ('View',), }, { 'id':'update', 'name':'Update', 'action':'string:${object_url}/update_schooldata', 'permissions': ('Manage Portal',), }, ) def __init__(self, id): self.id = id self.schoolList = [] def manage_afterAdd(self,item,container): """sets permissions""" self.manage_permission('Manage Portal', ('Manager','School') ,1) def getSchoolObj(self,id='all'): if id=='all': objects=[] for name in self.getFolderContents(): objects.append(getattr(self,name.id)) return objects else: return getattr(self,id) security.declareProtected('View', 'getSchoolTitle') def getSchoolTitle(self,id): """returns title""" return getattr(self,id).title security.declareProtected('View', 'getMembersName') def getMembersName(self,member): """returns members fullname""" pm = getToolByName(self, 'portal_membership') member = pm.getMemberById(str(member)) return member.getProperty('fullname') security.declareProtected('View', 'acceptRegistrationDataByUser') def acceptRegistrationDataByUser(self, REQUEST): """gets registration data from array by user id""" practice = int(REQUEST.get('practice')) for schoolObj in self.getSchoolObj('all'): for userInfo in schoolObj.getRegistrationData(int(practice)): if userInfo['state']=='pending': if REQUEST.get('cb_'+userInfo['id']+'_'+userInfo['school'])=='on': schoolObj.setAccepted(practice,userInfo['id']) return 1 security.declareProtected('View', 'rejectRegistrationDataByUser') def rejectRegistrationDataByUser(self, REQUEST): """gets registration data from array by user id""" practice = int(REQUEST.get('practice')) for schoolObj in self.getSchoolObj('all'): for userInfo in schoolObj.getRegistrationData(int(practice)): if REQUEST.get('cb_'+userInfo['id']+'_'+userInfo['school'])=='on' or REQUEST.get('ac_'+userInfo['id']+'_'+userInfo['school'])=='on': schoolObj.delRegistrationData(practice,userInfo['id']) return 1 security.declareProtected('View', 'delFromRegistrationData') def delFromRegistrationData(self, REQUEST): """ removes user from queue """ member = str(REQUEST.AUTHENTICATED_USER) practice = int(REQUEST.get('practice')) for schoolObj in self.getSchoolObj('all'): for userInfo in schoolObj.getRegistrationData(int(practice)): if userInfo['id']==member: schoolObj.delRegistrationData(practice,userInfo['id']) return 1 security.declareProtected('View', 'addStudentToPractice') def addStudentToPractice(self,REQUEST): student=str(REQUEST.AUTHENTICATED_USER) practice=REQUEST.get('practice') school1=REQUEST.get('school1') school2=REQUEST.get('school2') subjects=REQUEST.get('subjects') if school1 != None: self.getSchoolObj(school1).addStudentToPractice(student,practice,subjects,'1') if school2 != None: self.getSchoolObj(school2).addStudentToPractice(student,practice,subjects,'2') return 1 security.declareProtected('View', 'getStudentsFromPractice') def getStudentsFromPractice(self,practice): """gets students from practice""" pass security.declareProtected('View', 'getStudentFromPractice') def getStudentFromPractice(self,practice,member): """get member data""" allSchools = self.getFolderContents() info = [] for school in allSchools: data = getattr(self,school.id).getStudentFromPractice(practice,member) if data != False: info.append(data) return info security.declareProtected('View', 'getRegistrationDataBySchool') def getRegistrationDataBySchool(self,practice,school,filter='all'): """get member data""" info = [] if filter == 'all': return getattr(self,school).getRegistrationData(practice) else: for data in getattr(self,school).getRegistrationData(practice): if data['state']==filter: info.append(data) return info security.declareProtected('View', 'getRegistrationData') def getRegistrationData(self,practice,filter='all'): """get member data""" allSchools = self.getFolderContents() info = [] for school in allSchools: for data in getattr(self,school.id).getRegistrationData(practice): if filter == 'all': info.append(data) else: if data['state']==filter: info.append(data) return info security.declareProtected('View', 'getStateOfMember') def getStateOfMember(self, practice, member): """get member status""" allSchools = self.getFolderContents() states = [] for school in allSchools: data = getattr(self,school.id).getStudentFromPractice(practice,member) if data != False: states.append(data['state']) if 'accepted' in states: return 'accepted' elif 'pending' in states: return 'pending' return 'sleeping' security.declareProtected('View', 'getSchoolLocations') def getSchoolLocations(self): """returns list of school locations""" data = self.getFolderContents() schoolLocation = [] for school in data: if school.location in schoolLocation: pass else: schoolLocation.append(school.location) return schoolLocation security.declareProtected('View', 'getSchoolsByLocationAndBySubjects') def getSchoolsByLocationAndBySubjects(self, loc, sub): """returns list of schools by locations""" locations = self.getSchoolLocations() 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)] data = self.getFolderContents() schoolList = [] for school in data: school = getattr(self,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 #security.declareProtected('Manage Portal', 'viewSchoolDataBase') #def viewSchoolDataBase(self): # """view School Database permission""" # pass security.declareProtected('Manage Portal', 'updateSchoolList') def updateSchoolList(self): """ get data from db as list of schools""" import urllib2 id_list = [] count=0 size='50' for value in range(1, 901, 50): if count>16: size='34' count=count+1 url='https://eh-jas.hm.ee/avalik/oas/Otsing.uix?event=goto&source=tabel&value='+str(value)+'&size='+size f = urllib2.urlopen(url) html = f.read() list = html.split('') list.pop(0) for item in list: name='' id='' reg='' splitter=item.split('') reg=splitter[0] if reg not in ['','0']: if len(splitter)>1: splitter=splitter[1].split('') id=splitter[0].split('?') id=id[1].split('&') id='?'+id[0] if len(splitter)>1: splitter=splitter[1].split('<') name=splitter[0] print name if name!='' and id!='': id_list.append({'name':name,'id':id,'reg':reg}) self.schoolList = id_list self._p_changed = 1 security.declareProtected('View', 'getSchoolList') def getSchoolList(self): """returns schoolList""" return self.schoolList security.declareProtected('Manage Portal', 'updateSchool') def updateSchool(self, REQUEST): """ get data from db as data of schools""" import urllib2 new_list = ['null'] key = REQUEST.get('otsingKey') url='https://eh-jas.hm.ee/avalik/oas/muuda2.uix?otsingKey='+key+'&event=valiOtsing' f = urllib2.urlopen(url) html = f.read() html = self.charReplace(html) html = html.replace('','') html = html.replace('','') html = html.replace('','') html = html.replace('','') html = html.replace('','') html = html.replace('','') html = html.replace('','') data = html.split('') if len(data)>1: data.pop(0) data.pop(0) new_data = [] for d in data: d=d.split('') da=d[0].split('">') if len(da)>1: da=da[1] else: da=da[0] db=d[1].split('') if len(db)>1: db=db[1].split('') db=db[0] else: db='' if da!='': new_data.append({'name':da,'value':db}) schoolInfo = new_data #school abjects typestool = getToolByName(self, 'portal_types') puf_type = getattr(typestool, 'School', None) if not puf_type: return 0 puf_type.global_allow = 1 id = schoolInfo[0]['value'] if hasattr(self, id): #this school object is already in database pass else: id = self.invokeFactory("School", id=id, title=schoolInfo[1]['value'], schooltype=schoolInfo[4]['value'], address=schoolInfo[12]['value'], location=schoolInfo[11]['value'].replace(',',''), email=schoolInfo[9]['value'], www=schoolInfo[10]['value'], phone=schoolInfo[7]['value'],) puf_type.global_allow = 0 return REQUEST.RESPONSE.redirect(self.absolute_url()+'/update_schooldata') security.declareProtected('Manage Portal', 'isInDB') def isInDB(self, id): """if school is in db""" if hasattr(self, id): return '#CCFFCC' return '#FFCCCC' def charReplace(self,string): string=string.replace('ä','\xc3\xa4') #ä string=string.replace('Ä','\xc3\x84') #Ä string=string.replace('ö','\xc3\xb6') #ö string=string.replace('Ö','\xc3\xb6') #Ö string=string.replace('õ','\xc3\xb5') #õ string=string.replace('Õ','\xC3\x95') #Õ string=string.replace('ü','\xc3\xbc') #ü string=string.replace('Ü','\xc3\x9c') #Ü string=string.replace('ž','z') #z~ string=string.replace('š','s') #s~ return string registerType(SchoolDataBase, PROJECT_NAME) class School(OrderedBaseFolder): """ School """ meta_type = "School" archetype_name = "School" exclude_from_nav = True global_allow = 0 filter_content_types = True allowed_content_types = () security = ClassSecurityInfo() schema = schema + Schema(( StringField('title', widget=StringWidget( label="School name", description="", label_msgid='label_school_name', description_msgid='description_school_name', i18n_domain="eportfolio"), ), StringField('schooltype', widget=StringWidget( label="School type", description="", label_msgid='label_school_type', description_msgid='description_school_type', i18n_domain="eportfolio"), ), StringField('address', widget=StringWidget( label="Address", description="", label_msgid='label_school_address', description_msgid='description_school_address', i18n_domain="eportfolio"), ), StringField('location', widget=StringWidget( label="Location", description="", label_msgid='label_school_location', description_msgid='description_school_location', i18n_domain="eportfolio"), ), StringField('headmaster', widget=StringWidget( label="Headmaster", description="", label_msgid='label_school_headmaster', description_msgid='description_school_headmaster', i18n_domain="eportfolio"), ), StringField('email', widget=StringWidget( label="E-mail", description="", label_msgid='label_school_email', description_msgid='description_school_email', i18n_domain="eportfolio"), ), StringField('www', widget=StringWidget( label="WWW", description="", label_msgid='label_school_www', description_msgid='description_school_www', i18n_domain="eportfolio"), ), StringField('phone', widget=StringWidget( label="Phone", description="", label_msgid='label_school_phone', description_msgid='description_school_phone', i18n_domain="eportfolio"), ), StringField('studinglanguage', widget=StringWidget( label="Language", description="", label_msgid='label_school_language', description_msgid='description_school_language', i18n_domain="eportfolio"), ), StringField('nrofstudents', widget=StringWidget( label="Number of students", description="", label_msgid='label_school_nrofstudents', description_msgid='description_school_nrofstudents', i18n_domain="eportfolio"), ), )) actions = ( { 'id':'view', 'name':'View', 'action':'string:${object_url}/base_view', 'permissions': ('View',), }, { 'id':'owner', 'name':'Owner', 'action':'string:${object_url}/school_owner', 'permissions': ('Manage Portal',), }, { 'id':'subjects', 'name':'Subjects', 'action':'string:${object_url}/school_subjects', 'permissions': ('Manage Portal',), }, { 'id':'timetable', 'name':'Timetable', 'action':'string:${object_url}/timetable_view', 'permissions': ('View',), }, ) def __init__(self, id): self.id = id self.subjects = [] self.time = [] self.practice = [] self.setSubjects() self.setTime() self.setPractice() def manage_afterAdd(self,item,container): """sets permissions""" self.manage_permission('Manage Portal', ('Manager','School') ,1) self.manage_permission('View', ('Manager','School') ,1) def getPossibleMasterUsers(self): """ get list of possible master users """ pm = getToolByName(self, 'portal_membership') users = [] members = pm.listMemberIds() for memberid in members: member = pm.getMemberById(memberid) if 'School' in member.getRolesInContext(self): users.append(member) return users def getMasterUser(self): """ get list of possible master users """ pm = getToolByName(self, 'portal_membership') users = [] members = pm.listMemberIds() for memberid in members: member = pm.getMemberById(memberid) if str(member.school)==str(self.id): return {'id':member.id,'name':member.getProperty('fullname')} return {'id':'','name':''} def setMasterUserForSchool(self, REQUEST): """ sets master user for school """ user = REQUEST.get('school_owner') school = self.id pm = getToolByName(self, 'portal_membership') member = pm.getMemberById(user) member.school = school def setSubjects(self): """subjects""" self.subjects = [] for subject in SUBJECTS: self.subjects.append({'name':subject,'nr':0}) self._p_changed = 1 def setTime(self): """time""" self.time = ['00:00','00:00','00:00','00:00'] self._p_changed = 1 def setPractice(self): """practice""" self.practice = [[],[],[],[]] self._p_changed = 1 def saveTime(self,REQUEST): """time""" times=[] nr=1 for time in self.time: times.append(REQUEST.get('time_h_'+str(nr))+':'+REQUEST.get('time_m_'+str(nr))) nr=nr+1 self.time=times self._p_changed = 1 def getTime(self,how='string'): """time""" if how == 'array': times=[] for time in self.time: hhmm=time.split(':') times.append(hhmm) return times return self.time def getSubjects(self): """get subjects""" return self.subjects def getNumbers(self,max,min=0): """get numbers""" numbers = [] for nr in range(min,max+min,1): nr = str(nr) if len(nr)<2: nr='0'+nr numbers.append(nr) return numbers def saveSubjects(self, REQUEST): """save subjects""" i = 0 for subject in self.subjects: nr=REQUEST.get('subject_'+str(i)) self.subjects[i]['nr'] = nr i = i + 1 self._p_changed = 1 return 1 def changeSubject(self,subject,change): """chace subjects""" if len(subject)<2: self.subjects[int(subject)]['nr'] = int(self.subjects[int(subject)]['nr']) + int(change) else: for sub in data['subjects']: self.subjects[int(sub)]['nr'] = int(self.subjects[int(sub)]['nr']) + int(change) self._p_changed = 1 return 1 #def addStudentToTimetable(self,student): # """adds student to timetable array as key""" # self.timetable.append({'student':student,'timetable':[]}) # self._p_changed = 1 def delStudentFromPractice(self,practice,student): """deletes student from practice array by practice""" counter = 0 for tt in self.practice[int(practice)]: if tt['student']==student: self.practice[int(practice)].pop(counter) self._p_changed = 1 continue counter = counter + 1 def getStudentFromPractice(self,practice,student): """gets student from timetable array""" print 'getStudentFromPractice' print self.practice[int(practice)] print student for st in self.practice[int(practice)]: if str(st['id'])==str(student): return st return False def addStudentToPractice(self,student,practice,subjects,choice): """add student to practice""" self.practice[int(practice)].append({'id':student,'school':self.id,'subjects':subjects,'state':'pending','choice':choice,'timetable':[]}) self._p_changed = 1 def setAccepted(self,practice,member): for data in self.getRegistrationData(practice): if data['id']==member: data['state']='accepted' self.changeSubject(data['subjects'],-1) self._p_changed = 1 return 1 def delRegistrationData(self,practice,member): counter=0 for data in self.getRegistrationData(practice): if data['id']==member: if data['state']=='accepted': self.changeSubject(data['subjects'],1) self.getRegistrationData(practice).pop(counter) counter=counter+1 self._p_changed = 1 return 1 def getRegistrationData(self,practice): """gets data""" return self.practice[int(practice)] registerType(School, PROJECT_NAME)