# -*- coding: utf-8
# Copyright 2005 by Vahur Rebas
from Products.Archetypes.public import BaseSchema, Schema
from Products.Archetypes.public import StringField, TextField, LinesField
from Products.Archetypes.public import SelectionWidget, TextAreaWidget, KeywordWidget, SelectionWidget, StringWidget, IdWidget
from Products.Archetypes.public import RichWidget
from Products.Archetypes.public import BaseContent, registerType
from AccessControl import ClassSecurityInfo, Unauthorized
import xml.dom.minidom
from xml.dom.minidom import Document
from config import PROJECT_NAME
import Globals
schema = BaseSchema
def to_utf8(s):
return s.encode('utf-8')
def get_text(elem):
res = u''
for line in elem.childNodes:
res += line.nodeValue
return to_utf8(res)
class teacherdata:
security = ClassSecurityInfo()
#security.declareObjectPublic()
security.setDefaultAccess("allow")
def __init__(self,fullname,uname,js_id=None):
self.fullname = fullname
self.uname = uname
self.js_id = js_id
def __str__(self):
return self.uname
#security.declareProtected('View','getFullName')
def getFullName(self):
return self.fullname
Globals.InitializeClass(teacherdata)
class subjectManager:
def __init__(self):
self.map = []
def add(self,subject, klass, hour, day, teacher):
new = OneSubject(subject, klass, hour, day, teacher)
self.map.append(new)
self._p_changed = True
def __call__(self, subject=None, klass=None, hour=None, day=None, teacher=None):
retult = []
for x in self.map:
if x(subject, klass, hour, day, teacher):
retult.append(x)
return retult
class OneSubject:
security = ClassSecurityInfo()
security.declareObjectPublic()
security.setDefaultAccess("allow")
def __init__(self, subject=[], klass=0, hour=0, day=0, teacher=None):
self.subject = subject
self.klass = klass
self.hour = hour
self.teacher = teacher
self.day = day
def __call__(self, subject=None, klass=None, hour=None, day=None, teacher=None):
# NOTE: subject is now tuple: ['pretty name','uid','short name']
if subject is not None:
if subject in self.subject: return None
if klass is not None:
if self.klass != klass: return None
if hour is not None:
if self.hour != hour: return None
if teacher is not None:
if self.teacher != teacher: return None
if day is not None:
if self.day != day: return None
return 1
def __str__(self):
""" debugging purpose only! """
string = "teacher: "+ str(self.teacher)+" subject: "+ str(self.subject)+ " class: "+ str(self.klass)+ " hour: "+ str(self.hour)+" day: "+str(self.day)
return string
#security.declareProtected('View','getPrettySubject')
security.declarePublic('getPrettySubject')
def getPrettySubject(self):
return self.subject[0]
Globals.InitializeClass(OneSubject)
class TimeTable(BaseContent):
""" This is Time Table. """
meta_type = "TimeTable"
archetype_name = "TimeTable"
global_allow = 1
security = ClassSecurityInfo()
schema = schema
actions = (
{
'id':'view',
'name':'View',
'action':'string:${object_url}/timetable_view',
'permissions': ('Manage portal',),
},
{
'id':'change1',
'name':'Compose',
'action':'string:${object_url}/timetable_compose',
'permissions': ('Manage portal',),
},
)
def manage_afterAdd(self, item, container):
self._filled_classes = {}
self._stored_table = None
if not hasattr(self.aq_self, 'right_slots'):
self.manage_addProperty('right_slots',[],'lines')
self.updateBaseProperties()
def updateBaseProperties(self):
""" copies teachers, classes mapping from time table tool """
from copy import deepcopy
tool = getattr(self,'time_tool')
self._lectures = deepcopy(tool._lectures)
self._teach_map = deepcopy(tool._teach_map)
self._relat_map = deepcopy(tool._relat_map)
self.setTeacherList()
self._groups_list = tool.getGroups()
self._p_changed = True
self._js_subjs = self.renderSubjectsJS()
self._js_teach = self.renderTeachersJS()
def getSubjectsJS(self):
return self._js_subjs
def getTeachersJS(self):
self._js_teach = self.renderTeachersJS()
return self._js_teach
def setTeacherList(self):
tool = getattr(self,'time_tool')
list = tool.getTeachers()
self._teachers_list = []
for x in list:
self._teachers_list.append(teacherdata(x.fullname, str(x)))
self._p_changed = True
def getTeachers(self):
return self._teachers_list
def getTeacher(self, uname=None, js_id=None):
if uname is None and js_id is None:
return None
result = []
for x in self._teachers_list:
if uname is not None and js_id is None:
if str(x) == uname:
result.append(x)
if js_id is not None and uname is None:
if x.js_id == js_id:
result.append(x)
if uname is not None and js_id is not None:
if x.js_id == js_id and uname == str(x):
result.append(x)
return result
def changeTeacher(self, uname=None, fullname=None, js_id=None):
result = []
for x in self.getTeachers():
change = 0
if uname != None and js_id is None:
if uname != str(x):
result.append(x)
else: change = 1
if js_id != None and uname is None:
if js_id != x.js_id:
result.append(x)
else: change = 1
if js_id != None and uname != None:
if x.js_id != js_id and uname != str(x):
result.append(x)
else:
change = 1
if change:
if fullname is None: fullname = x.fullname
if js_id is None: js_id = x.js_id
if uname is None: uname = str(x)
result.append(teacherdata(fullname, uname, js_id))
self._teachers_list = result
self._p_changed = True
return True
def getGroups(self):
return self._groups_list
def getFilledClasses(self, teacher=''):
""" returns fill spots in timetable composing page """
if teacher:
return self._filled_classes
return self._filled_classes
def getLectures(self):
return self._lectures
def getLecture(self,key):
return self._lectures[key]
def getTeacherMap(self, teacher_name):
if not self._teach_map.has_key(teacher_name): return {}
return self._teach_map[teacher_name]
def getRelationsMap(self, subject):
if not self._relat_map.has_key(subject): return {}
return self._relat_map[subject]
def renderSubjectsJS(self):
""" returns javascript array """
import re
arr = ""
return arr
def renderGroupsJS(self):
""" get groups/classes for javascript(timetable_compose) """
list = self.getGroups()
done = []
res = {}
for x in list:
x = str(x.name)
if x in done: continue
done.append(x)
try:
i = int(x)
cn = x
except ValueError:
if len(x)==3: cn = x[:2] # 11b
if len(x)==2: cn = x[:1] # 2b
if len(x)==1: cn = x # 3
int(cn)
if not hasattr(res, str(cn)): res[str(cn)] = []
if not x[len(cn):]:
res[str(cn)].append('')
else:
res[str(cn)].append(x[len(cn):])
for y in list:
y = str(y.name)
if y in done: continue
if y.startswith(cn) and len(x)==len(y):
try:
int(y)
continue
except ValueError:
pass
ct = y[:len(cn)]
res[str(cn)].append(y[len(cn):])
done.append(y)
result = []
for x in res.keys():
tmp = []
tmp.append([])
tmp.append([])
tmp[0].append(int(x))
for y in res[x]:
tmp[1].append(y)
result.append(tmp)
result.sort()
self.klmap = []
self.klmapTitle = []
romans = ['','I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII']
for x in result:
for y in x[1]:
self.klmap.append(str(x[0][0])+y)
self.klmapTitle.append(romans[x[0][0]]+' '+y.upper())
self._p_changed = True
return str(result)
def renderTeachersJS(self):
""" return a list of teachers for javascript timetable composer """
list = self.getTeachers()
res = ""
return res
def createSubjectMap(self, node):
map = []
data = node.getElementsByTagName('data')[0]
for x in data.childNodes:
tmp = []
if x.nodeType != 1: continue
subPretty = get_text(x.getElementsByTagName('string')[0])
subId = get_text(x.getElementsByTagName('string')[1])
subShort = get_text(x.getElementsByTagName('string')[2])
tmp.append(subPretty)
tmp.append(subId)
tmp.append(subShort)
map.append(tmp)
return map
def xmlSave(self, REQUEST, RESPONSE):
""" lasasd """
data = REQUEST.jsrs
xmldata = data
RESPONSE.setHeader('Content-Type','text/xml')
doc = xml.dom.minidom.parseString(data)
i = 0
mass = []
for x in doc.getElementsByTagName('param'):
if i == 0: ained = x
if i == 1: itk = x
if i == 2: mass.append(x)
if i == 3: mass.append(x)
i = i + 1
if i == 4: i = 2
#param = doc.getElementsByTagName('param')[0]
subject_map = self.createSubjectMap(ained)
# number of teachers.
iTotalTeachers = int(get_text(itk.getElementsByTagName('value')[0].getElementsByTagName('i4')[0]))
print "\n\n",iTotalTeachers,"\n"
print self.klmap
print "subject map->",subject_map #XXX: store it!
self.jsSubMap = subject_map
# koostame ainete loendi
param = doc.getElementsByTagName('param')[1]
# hangime i4'st numbri palju on õpetajaid
# XXX: do we need it !?a I thinks we need it but let's see.
#for x in i4stSaadudnumber*2: uuel juhul for x in mass
# võta õpetaja nimi, ainete massiiv id number(scode)
# võta järgmine
# all two value tuleb mappida vast scode-i ja one-i i4 valued tuleb mappida vastu klassidemappingut mida praegu pole.
# seal kus 1a ja 1b lahti harutatakse ja javascriptile antakse tuleb koostada mapping:
# klasside 1a, 1b, 2a, 2b, 3, 4a korral
# 1a->0 1b->1 2a->2 2b->3 3->4 4a->5
# tegelt tuleb vist püsiv mapping teha stiilis
# map = ['1a','1b','2a','2b',... jne] kus map[0] = '1a' ja 0 on javascriptist tulev suurus
# map tehtud -> self.klmap
#pass
#for x in mass:
#pass
# esimene( e. paaritu? ) on õpetaja massiiv.
# teine (e. paaris ) on õpetaja tundide massiiv.
count = 0
time_map = subjectManager()
while 1==1:
try:
node = mass[count]
except IndexError:
break
# get some teacher information
uname = get_text(node.getElementsByTagName('string')[1])
count = count + 1
node = mass[count]
# get classes assigned to teacher
datas = node.getElementsByTagName('data')[0]
iDay = 0
for value in datas.childNodes: # loop over days
if value.nodeType != 1: continue
z = value.getElementsByTagName('data')[0]
iHour = 0
for hour in z.childNodes: # loop over hours.
if hour.nodeType != 1: continue
data = hour.getElementsByTagName('data')[0]
vbs = 0
aKlasses = []
for w in data.childNodes:
if w.nodeType != 1: continue
if vbs == 0:
# fetch classes
kl_data = w.getElementsByTagName('data')[0]
for klass in kl_data.childNodes:
if klass.nodeType != 1: continue
i4 = int(get_text(klass.getElementsByTagName('i4')[0]))
aKlasses.append(i4)
pass
if vbs == 1:
# fetch subject code
i4 = get_text(w.getElementsByTagName('i4')[0])
if i4 != "-1":
print ">> õpetaja: ",uname ," ainet ",subject_map[int(i4)][1], " tunnis: ",iHour, "klassides: ", aKlasses
for oneKlass in aKlasses:
# subject, klass, hour, teacher
time_map.add(subject_map[int(i4)], oneKlass, iHour, iDay, uname)
vbs = vbs + 1
iHour = iHour + 1
iDay = iDay + 1
count = count + 1
if count > iTotalTeachers*2:
print "----------- lõpetan ------------", count, iTotalTeachers
break #?
print self.getTeachers()
print "\n\n"
for x in time_map.map:
print "x->",x
self._stored_table = time_map
self._p_changed = True
x = self._stored_table(subject='neljas')
for y in x:
print "x->>",y
print self.getTeacher(js_id=0)
return REQUEST.RESPONSE.redirect('timetable_view')
def fillSlots(self):
if self._stored_table is None:
return
list = self.getTeachers()
result = ""
return result
def fillSlot(self, teacher=None, day=None, hour=None, klass=None, subject=None):
if self._stored_table is None:
return
# self, subject=None, klass=None, hour=None, day=None, teacher=None
print self._stored_table(subject, klass, hour, day, teacher)
return self._stored_table(subject, klass, hour, day, teacher)
def getPrettyDay(self, day):
days = ['Esmaspäev','Teisipäev','Kolmapäev','Neljapäev','Reede']
return days[day]
registerType(TimeTable, PROJECT_NAME)