# -*- coding: utf-8 # $Id$ # import zipfile import xml.dom.minidom import xml.sax from xml.dom.minidom import Document import string from xml.sax import saxutils from xml.dom.ext.reader import Sax2 from ImportExport import Counter, DataTable, Exporter, Dump from common import FakeRequest, FakeUpload, to_utf8, to_unicode, get_text, get_attr from xml.dom import implementation from xml.dom import EMPTY_NAMESPACE, XML_NAMESPACE from xml.dom.ext import PrettyPrint from Products.ZWiki.ZWikiPage import ZWikiPage from types import UnicodeType from input_checks import strip_non_xml from StringIO import StringIO import OFS.Image import types, time import re, time __version__ = "$Revision$"[11:-2] class Importer: def __init__(self, uname,ivajuur,prefix='', kursus='',binaryd='',testonly=0,userwikionly=0,kasuta_jooksvat=0, userArchive=0, out=''): """Creates impoter""" self.idmap = {} self.uname = uname self.prefix = prefix self.iva = ivajuur self.kasuta_jooksvat = kasuta_jooksvat self.out = out if kursus: self.kursus = kursus else: self.kursus = '' if binaryd: self.binarydata = binaryd else: self.binarydata = DataTable() self.timecounters=[0,0,0,0,0,0,0,0,0,0] self.processed = [] self.testonly = testonly self.userwikionly = userwikionly self.userArchive = userArchive def importTestAnswers(self,elem,tid,map): from YlTest import TestiVastused for r in elem.getElementsByTagName('response'): owner = get_attr(r,'owner') # get userobject. Vastuse omanik try: wt = getattr(self.iva.fle_users, owner).webtop wtc = getattr(wt, 'c'+self.kursus.get_id()) except: raise 'reise-reise' continue if not hasattr(wtc.aq_self, 'testivastused'): wtc.manage_addProduct['OFSP'].manage_addFolder('testivastused') wtc.testivastused.id='testivastused' tv = getattr(wtc, 'testivastused') if not hasattr(tv, tid): tid = str(tid) tv.manage_addProduct['OFSP'].manage_addFolder(tid) tvx = getattr(tv, tid) tvx.id = tid else: tvx = getattr(tv,tid) #käiakse läbi erinevad testilahenduse variandid for ver in r.getElementsByTagName('version'): # lahendusvariandi id id = get_attr(ver,'id') id = str(id) if not hasattr(tvx.aq_self, id): t = TestiVastused() t.testinimi = id t.id = id tvx._setObject(id,t) verk = getattr(tvx, id) comment = get_attr(ver,'comment') #anna kogu kommentaar try: verk.seaKoguKommentaar(comment) except AttributeError: #XXX:weird pass for yl in ver.getElementsByTagName('yl'): #testi ülesande (objekti) id id = str(get_attr(yl,'id')) comment = get_attr(yl,'comment') score = get_attr(yl,'score') tupe = get_attr(yl,'type') answer = get_text(yl) if answer == 'None': answer = None if tupe == 'List': answer = eval(answer) if not score: score = None if hasattr(verk,id): #XXX: I don't know if this is reasonable continue try: verk._setObject(id, map[id].koopia()) except KeyError: continue ylobj = getattr(verk,id) ylobj.pakutudAndmed = answer ylobj.opetajaPunktid = score ylobj.seaOpetajaKommentaar(comment) def importQT(self, elem, course, uname): import YlTyybid from YlAlus import YlAlus from YlTyybid import ValikYlesanne, TekstivastusegaYlesanne, OigeTekstivastusegaYlesanne, MitmikvalikYlesanne, YksikvalikYlesanne, ArvuvahemikuliseVastusegaYlesanne, ProtsentYlesanne, VastavusYlesanne from YlTest import YlTest, LahendusLoad, LahendusLuba, TestiVastused id_map = {} comment = get_text(elem.getElementsByTagName("qticomment")[0]) t_tyybid = ('ValikYlesanne','MitmikvalikYlesanne','YksikvalikYlesanne','OigeTekstivastusegaYlesanne','TekstivastusegaYlesanne','ArvuvahemikuliseVastusegaYlesanne','VastavusYlesanne') for abc in elem.getElementsByTagName("section"): # get id and title sekt_title = get_attr(abc,'title') id = get_attr(abc, 'ident') if not hasattr(course.testid,'nr'): course.testid.nr=0 # add test. XXX: move this to top and start adding metadata course.testid.nr = course.testid.nr+1 qt=YlTest() qt.pealkiri = sekt_title # qt.id = "test"+str(course.testid.nr) # course.testid._setObject(qt.id, qt) qt.id = id if hasattr(course.testid, qt.id): qt.id = "test"+str(course.testid.nr) # harvest data answers = get_attr(abc, 'answers') if not answers: answers=0 qt.seaVastusteNahtavus(answers) open = get_attr(abc, 'open') if not open: open=0 qt.avatudTaitmiseks = int(open) randomize = get_attr(abc, 'randomize') qt.seaKysimusteJuhuslikJarjekord(randomize) type = get_attr(abc, 'type') if not type: type=0 qt.tyyp = int(type) try: md = abc.getElementsByTagName('metadata')[0] desc = get_text(md.getElementsByTagName('description')[0]) qt.kirjeldus = desc # permission options! #timelimit and repeats tstart = float(get_attr(md.getElementsByTagName('timelimit')[0],'start')) tend = float(get_attr(md.getElementsByTagName('timelimit')[0],'end')) repeat = get_attr(md.getElementsByTagName('timelimit')[0],'repeat') qt.koigiLuba.algusAeg = tstart qt.koigiLuba.loppAeg = tend qt.koigiLuba.kordadeArv = int(repeat) from YlTest import LahendusLuba #groups for gr in md.getElementsByTagName('person'): try: name = get_attr(gr,'name') except: continue dm = LahendusLuba() #if name == 'DuMmY67891': continue tstart = float(get_attr(gr,'start')) tend = float(get_attr(gr,'end')) repeat = get_attr(gr,'repeat') dm.id = str(name) dm.algusAeg = tstart dm.loppAeg = tend dm.kordadeArv = int(repeat) qt.isikuLoad._setObject(dm.id,dm) #persion for pr in md.getElementsByTagName('group'): try: name = get_attr(pr,'name') except: continue dm = LahendusLuba() #if name == 'DuMmY67891': continue tstart = float(get_attr(pr,'start')) tend = float(get_attr(pr,'end')) repeat = get_attr(pr,'repeat') dm.id = name dm.algusAeg = tstart dm.loppAeg = tend dm.kordadeArv = int(repeat) qt.grupiLoad._setObject(dm.id,dm) #limited solving time!? #finally, add object except IndexError: pass # empty id check if not qt.id or qt.id == '': qt_id_counter = 0 qt_id_gen = 'qttest' while hasattr(course.testid, qt_id_gen+str(qt_id_counter)): qt_id_counter += 1 qt.id = qt_id_gen+str(qt_id_counter) qt.id = str(qt.id) course.testid._setObject(qt.id, qt) test_map = {} for bcd in abc.getElementsByTagName("item"): img_id = '' img_title = '' photo_data = None try: qt_tyyp = get_attr(bcd,'title') except IndexError: qt_tyyp = '' try: qt_id = get_attr(bcd,'ident') except IndexError: qt_id = '' try: qt_story = get_text(bcd.getElementsByTagName("qticomment")[0]) except IndexError: qt_story = '' pelem = bcd.getElementsByTagName("presentation")[0] try: qt_quiz = get_text(pelem.getElementsByTagName("mattext")[0]) except IndexError: qt_quiz = '' qtimg = course.testid.qtimages for image in pelem.getElementsByTagName("matimage"): photo_data = self.binarydata.getData( get_attr(image, 'uri'),self.prefix) img_uri = get_attr(image, 'uri') img_id = get_attr(image,'id') img_title = get_attr(image, 'title') if not img_id: img_id = img_uri try: qtimg.manage_addImage(img_id, photo_data, img_title) except: pass try: qt_link = get_text(pelem.getElementsByTagName("matemtext")[0]) qt_linkd = get_attr(pelem.getElementsByTagName("matemtext")[0],'uri') except IndexError: qt_link = '' qt_linkd = '' try: r_grp = pelem.getElementsByTagName("response_grp")[0] r_grp1 = 1 except IndexError: r_grp1 = 0 try: r_lid = pelem.getElementsByTagName("response_lid")[0] r_lid1 = 1 except IndexError: r_lid1 = 0 try: r_xy = pelem.getElementsByTagName("response_xy")[0] r_xy1 = 1 except IndexError: r_xy1 = 0 try: r_str = pelem.getElementsByTagName("response_str")[0] r_str1 = 1 except IndexError: r_str1 = 0 try: r_num = pelem.getElementsByTagName("response_num")[0] r_num1 = 1 except IndexError: r_num1 = 0 if r_lid1: try: r_render = pelem.getElementsByTagName("render_choice")[0] rend = 1 except: rend = 0 if rend and qt_tyyp not in t_tyybid: qt_tyyp = 'MitmikvalikYlesanne' if r_str1: r_fib1 = 0 try: r_fib = pelem.getElementsByTagName("render_fib")[0] r_fib1 = 1 except: r_fib1 = 0 if r_fib1: if qt_tyyp not in t_tyybid: try: xx = bcd.getElementsByTagName("varequal")[0] tmp = 1 except: tmp = 0 if tmp: qt_tyyp = 'OigeTekstivastusegaYlesanne' else: qt_tyyp = 'TekstivastusegaYlesanne' if qt_tyyp == 'ValikYlesanne': vastus = get_text(bcd.getElementsByTagName("varequal")[0]) if vastus == 'T': vastus =1 else: vastus =0 punkte = get_text(bcd.getElementsByTagName("setvar")[0]) qt_test = ValikYlesanne(qt_quiz) yl = qt.lisaYlesanne(qt_test) qt_test.seaKirjeldavTekst('',qt_story) qt_test.normPunktid=float(punkte) qt_test.viiteURL = qt_linkd qt_test.viiteTekst = qt_link qt_test.vastus = vastus qt_test.pilt = photo_data # pistame testi hoidlasse pass if qt_tyyp == 'MitmikvalikYlesanne': punkte = get_text(bcd.getElementsByTagName("setvar")[0]) qt_test = MitmikvalikYlesanne(qt_quiz) yl = qt.lisaYlesanne(qt_test) qt_test.seaKirjeldavTekst('',qt_story) qt_test.normPunktid=float(punkte) qt_test.viiteURL = qt_linkd qt_test.viiteTekst = qt_link qt_test.pilt = photo_data oiged = [] v_text = [] v_id = [] s_text = [] s_id = [] for xbc in bcd.getElementsByTagName("varequal"): oiged.append(get_text(xbc)) for vastused in bcd.getElementsByTagName("response_label"): v_id.append(get_attr(vastused,'ident')) v_text.append(get_text(vastused.getElementsByTagName("mattext")[0])) for seletused in bcd.getElementsByTagName("itemfeedback"): s_id.append(get_attr(seletused, 'ident')) s_text.append(get_text(seletused.getElementsByTagName("mattext")[0])) v_sisse = [] s_sisse = [] o_sisse = [] j = 0 for vmass in v_id: i = 0 for smass in s_id: if vmass == smass: v_sisse.append(v_text[j]) s_sisse.append(s_text[i]) i = i + 1 try: if str(vmass) in oiged: o_sisse.append(int(j)) except: pass j = j + 1 qt_test.valikud = v_sisse qt_test.selgitused = s_sisse qt_test.oigedValikud = o_sisse pass if qt_tyyp == 'YksikvalikYlesanne': vastus = get_text(bcd.getElementsByTagName("varequal")[0]) punkte = get_text(bcd.getElementsByTagName("setvar")[0]) qt_test = YksikvalikYlesanne(qt_quiz) yl = qt.lisaYlesanne(qt_test) qt_test.seaKirjeldavTekst('',qt_story) qt_test.normPunktid=float(punkte) qt_test.viiteURL = qt_linkd qt_test.viiteTekst = qt_link qt_test.pilt = photo_data v_text = [] v_id = [] s_text = [] s_id = [] for vastused in pelem.getElementsByTagName("response_label"): v_id.append(get_attr(vastused,'ident')) v_text.append(get_text(vastused.getElementsByTagName("mattext")[0])) for seletused in bcd.getElementsByTagName("itemfeedback"): s_id.append(get_attr(seletused, 'ident')) s_text.append(get_text(seletused.getElementsByTagName("mattext")[0])) v_sisse = [] s_sisse = [] s_id.reverse() s_text.reverse() j = 0 for vmass in v_id: i = 0 for smass in s_id: if vmass == smass: v_sisse.append(v_text[j]) s_sisse.append(s_text[i]) i = i + 1 if vmass == str(vastus): qt_test.oigeValik = j j = j + 1 qt_test.valikud = v_sisse qt_test.selgitused = s_sisse pass if qt_tyyp == 'OigeTekstivastusegaYlesanne': try: punkte = get_text(bcd.getElementsByTagName("setvar")[0]) except: punkte = 0 qt_test = OigeTekstivastusegaYlesanne(qt_quiz) yl = qt.lisaYlesanne(qt_test) qt_test.seaKirjeldavTekst('',qt_story) qt_test.normPunktid=float(punkte) qt_test.viiteURL = qt_linkd qt_test.viiteTekst = qt_link qt_test.pilt = photo_data oiged = [] for xbc in bcd.getElementsByTagName("varequal"): oiged.append(get_text(xbc)) qt_test.variandid = oiged pass if qt_tyyp == 'TekstivastusegaYlesanne': try: punkte = get_text(bcd.getElementsByTagName("setvar")[0]) except: punkte = 0 qt_test = TekstivastusegaYlesanne(qt_quiz) yl = qt.lisaYlesanne(qt_test) qt_test.seaKirjeldavTekst('',qt_story) qt_test.normPunktid=float(punkte) qt_test.viiteURL = qt_linkd qt_test.viiteTekst = qt_link qt_test.pilt = photo_data pass if qt_tyyp == 'ArvuvahemikuliseVastusegaYlesanne': punkte = get_text(bcd.getElementsByTagName("setvar")[0]) qt_test = ArvuvahemikuliseVastusegaYlesanne(qt_quiz) yl = qt.lisaYlesanne(qt_test) qt_test.seaKirjeldavTekst('',qt_story) qt_test.normPunktid=float(punkte) qt_test.viiteURL = qt_linkd qt_test.viiteTekst = qt_link qt_test.pilt = photo_data arv_tyyp = bcd.getElementsByTagName("outcomes")[0] q_arv_tyyp = get_text(arv_tyyp.getElementsByTagName("qticomment")[0]) q_alam = get_text(bcd.getElementsByTagName("vargte")[0]) q_oige = get_text(bcd.getElementsByTagName("varequal")[0]) q_ylem = get_text(bcd.getElementsByTagName("varlte")[0]) qt_test.alamPiir = float(q_alam) qt_test.ylemPiir = float(q_ylem) qt_test.oigeVastus = float(q_oige) try: qt_test.arvutusTyyp = int(q_arv_tyyp) except: pass pass if qt_tyyp == 'VastavusYlesanne': import random punkte = get_text(bcd.getElementsByTagName("setvar")[0]) qt_test = VastavusYlesanne(qt_quiz) yl = qt.lisaYlesanne(qt_test) qt_test.seaKirjeldavTekst('',qt_story) qt_test.normPunktid=float(punkte) qt_test.viiteURL = qt_linkd qt_test.viiteTekst = qt_link qt_test.pilt = photo_data v_id = [] v_text = [] for xbc in bcd.getElementsByTagName("response_label"): v_id.append(get_attr(xbc,'ident')) v_text.append(get_text(xbc.getElementsByTagName("mattext")[0])) s_vastused = [] s_kysimused = [] for xbc in bcd.getElementsByTagName("varsubset"): paar = string.split(get_text(xbc),",") koht_v = v_id.index(paar[0]) koht_p = v_id.index(paar[1]) s_kysimused.append(v_text[koht_v]) s_vastused.append(v_text[koht_p]) s_jarjestus = [] for i in range(len(s_kysimused)): s_jarjestus.append(i) random.shuffle(s_jarjestus) qt_test.kysimused = s_kysimused qt_test.vastused = s_vastused qt_test.jarjestus = s_jarjestus pass try: test_map[qt_id] = qt_test except UnboundLocalError: #XXX:some test is missing! pass do_a = 1 # import answers try: do_a = self.do_import_answers except: pass if do_a: self.importTestAnswers(abc,id,test_map) return def importAffliationCodes(self, elem): """ import affiliation codes """ elem.normalize() for x in elem.getElementsByTagName('affiliation'): self.iva.fle_users.addAffiliation(get_text(x)) return 0 def importServerSettings(self, elem): """ import server settings """ elem.normalize() auto = eval(get_text(elem.getElementsByTagName('auto_register')[0]).strip()) use_quota = eval(get_text(elem.getElementsByTagName('quota')[0]).strip()) quota_amount = get_text(elem.getElementsByTagName('quota_amount')[0]) mail_host = get_text(elem.getElementsByTagName('mail_host')[0]) mail_port = get_attr(elem.getElementsByTagName('mail_host')[0], 'port') mail_from = get_text(elem.getElementsByTagName('mail_from')[0]) def_lang = get_text(elem.getElementsByTagName('default_language')[0]) force_pass_change = eval(get_text(elem.getElementsByTagName('force_change')[0]).strip()) deny_pass_change = eval(get_text(elem.getElementsByTagName('deny_password_change')[0]).strip()) deny_aff_change = eval(get_text(elem.getElementsByTagName('deny_affiliation_change')[0]).strip()) self.iva.manage_iva_setup_handler(1, 1, auto, use_quota, quota_amount, mail_host, mail_port, mail_from, def_lang, force_pass_change, deny_pass_change, deny_aff_change) return 0 def importCourseCats(self, element): """ import course categories """ element.normalize() cats = element.getElementsByTagName('categories')[0] cats_text = get_text(cats).strip() print "cats_text", cats_text, "<<" cats_eval = eval(cats_text) cm = self.iva.courses cm.cCategories = cats_eval cm._p_changed = True return 1 def importMailMessages(self,element): # import inner-mail messages um = self.iva.fle_users for cat in element.getElementsByTagName('catalog'): owner = get_attr(cat,'owner') try: ui = getattr(um, owner) except: # print "kasutajat %s ei leitnud, jätan vahele" % owner continue box = ui.kysiKirjadeKataloog() for mess in cat.getElementsByTagName('message'): read = int(get_attr(mess,'read')) owncopy = get_attr(mess,'owncopy') title = get_text(mess.getElementsByTagName('title')[0]) body = get_text(mess.getElementsByTagName('body')[0]) sender = get_text(mess.getElementsByTagName('sender')[0]) receiver = get_text(mess.getElementsByTagName('receiver')[0]) date = get_text(mess.getElementsByTagName('date')[0]) mail = box.lisaKiri(None,title,body,sender,receiver,99999) mail.loomisaeg = float(date) if owncopy: mail.omakopi = 1 if read: mail.loetud = 1 else: mail.loetud = 0 def importKodutood(self,element): # impordime kodutööd kt_koht = getattr(self.kursus,'kodutood') grupid = [] for assign in element.getElementsByTagName('Assignment'): try: orgID = str(get_attr(assign,'originalID')) except: orgID = '' tyyp = int(get_text(assign.getElementsByTagName('type')[0])) pkiri = str(get_text(assign.getElementsByTagName('title')[0])) kirjeldus = str(get_text(assign.getElementsByTagName('description')[0])) punkt = float(get_text(assign.getElementsByTagName('points')[0])) try: res = str(get_text(assign.getElementsByTagName('resource')[0])) except: res = '' loppaeg = get_text(assign.getElementsByTagName('end')[0]) try: testiID = str(get_text(assign.getElementsByTagName('test')[0])) except: testiID = None try: for gr in assign.getElementsByTagName('group'): gnimi = get_text(gr) grupid.append(gnimi) except: grupid = None if res: sisu = res.split('/') try: res = '/courses/'+str(self.kursus.id)+"/"+sisu[3]+"/"+sisu[4] except: res = '' too = kt_koht.lisaKodutoo(tyyp,orgID) too.pealkiri = pkiri too.kirjeldus = kirjeldus too.normPunkte = punkt too.juhend = res if loppaeg != 'None': too.loppaeg = float(loppaeg) # New parameter too.lockingFolder = 0 if testiID != None: too.testiID = testiID if grupid != None: too.grupid = grupid return def handleGrades(self,isik,uid): for x in isik.getElementsByTagName('interimresult'): workid = get_attr(x, 'resulttype') try: points = float(get_text(x.getElementsByTagName('result')[0])) except: points = '' try: s_com = get_text(x.getElementsByTagName('comments')[0]) except: s_com = '' try: t_com = get_text(x.getElementsByTagName('tcomment')[0]) except: t_com = '' if points: self.kursus.kodutood.setUserPoints(workid,uid,'points',points) self.kursus.kodutood.setUserPoints(workid,uid,'comment',s_com) self.kursus.kodutood.setUserPoints(workid,uid,'teacherComment',t_com) f = isik.getElementsByTagName('finalresult')[0] try: grade = float(get_text(f.getElementsByTagName('result')[0])) if grade: self.kursus.kodutood.setUserOverall(uid,'TotPoints',grade) # TotPoints except: pass try: comment = get_text(f.getElementsByTagName('comments')[0]) #overall comment self.kursus.kodutood.setUserOverall(uid,'overallComment',comment) except: pass try: t_com = get_text(f.getElementsByTagName('tcomment')[0]) self.kursus.kodutood.setUserOverall(uid,'TotPointsCom',t_com) # TotPointsCom except: pass try: modpoints = get_text(f.getElementsByTagName('grade')[0]) # grade if modpoints: self.kursus.kodutood.setUserOverall(uid,'grade',modpoints) except: pass try: modcomment = get_text(f.getElementsByTagName('grade_com')[0]) # gradeCom self.kursus.kodutood.setUserOverall(uid,'gradeCom',modcomment) except: pass def lisaIsik(self,isik,tyyp,grupinimi=''): uid = str(get_text(isik.getElementsByTagName('id')[0])) roll = get_attr(isik.getElementsByTagName('role')[0],'roletype') if tyyp == 'kursus': try: self.kursus.add_student(uid) print >> self.out, "Giving user %s role 'Student'" % uid #handle grades here! interimresult, finalresult self.handleGrades(isik,uid) if roll == 'Instructor': self.kursus.set_roles(uid,('Teacher',)) print >> self.out, "Giving user %s role 'Teacher'" % uid #else: #XXX:we already set Student in add_student #self.kursus.set_roles(uid,('Student',)) except: print "------------ lisaIsik error" else: sgs = self.kursus.subgroups.getSubgroupByName(grupinimi) if not sgs: raise 'IVA import error:', 'Couldn\'t find subgroup %s' % grupinimi sgs.add_to_subgroup(uid) #self.kursus.lisa_sisegruppi(grupinimi,uid) return def restoreMembership(self,failisisu): sisegr = self.kursus.subgroups.getSubgroups() sisegrupid = [] for grupid in sisegr: sisegrupid.append(grupid.get_name()) for group in failisisu.getElementsByTagName('group'): id = get_text(group.getElementsByTagName('id')[0]) try: typevalue = get_attr(group.getElementsByTagName('typevalue')[0],'level') except: typevalue = '' if str(typevalue) == '1': try: orgname = get_text(group.getElementsByTagName('orgname')[0]) except: orgname = 'orgnameorgnameorgname' # if self.kursus: # if str(orgname) == str(self.kursus.get_name()): # pass for ship in failisisu.getElementsByTagName('membership'): try: komm = get_text(ship.getElementsByTagName('comments')[0]) except: komm = '' ship_id = get_text(ship.getElementsByTagName('id')[0]) # k_nimi = self.kursus.get_name()+' *' # if ship_id+' *' == str(self.kursus.get_name()) == id+' *' and str(komm) == 'KURSUS': kurss_name = self.kursus.get_name() if ship_id == kurss_name == id and str(komm) == 'KURSUS': for isik in ship.getElementsByTagName('member'): self.lisaIsik(isik,'kursus') else: id = get_text(group.getElementsByTagName('id')[0]) if not id in sisegrupid: try: sgr = self.kursus.subgroups.add_subgroup(id) sgr.manage_delLocalRoles((self.uname,)) except: pass for ship in failisisu.getElementsByTagName('membership'): try: komm = get_text(ship.getElementsByTagName('comments')[0]) except: komm = '' if str(komm) == 'SISEGRUPP': ship_id = get_text(ship.getElementsByTagName('id')[0]) if ship_id == id: for isik in ship.getElementsByTagName('member'): self.lisaIsik(isik,'sisegrupp',id) return def importUsers(self,element): print >> self.out, "Preparing to restore users..." um = getattr(self.iva, 'fle_users') tulem = "" us_alg = time.time() for person in element.getElementsByTagName('person'): roles = [] userid = get_text(person.getElementsByTagName('userid')[0]) password = get_attr(person.getElementsByTagName('userid')[0],'password') try: family = get_text(person.getElementsByTagName('family')[0]) except: family = '' try: given = get_text(person.getElementsByTagName('given')[0]) except: given = '' try: email = get_text(person.getElementsByTagName('email')[0]) except: email = '' try: url = get_text(person.getElementsByTagName('url')[0]) except: url = '' try: for telephones in person.getElementsByTagName('tel'): if get_attr(telephones,'teltype') == 'Voice': voice = get_text(telephones) else: mobile = get_text(telephones) except: voice = '' mobile = '' try: addr2 = get_text(person.getElementsByTagName('extadd')[0]) except: addr2 = '' try: street = get_text(person.getElementsByTagName('street')[0]) except: street = '' try: locality = get_text(person.getElementsByTagName('locality')[0]) except: locality = '' try: country = get_text(person.getElementsByTagName('country')[0]) except: country = '' try: photo_tag = person.getElementsByTagName('photo')[0] except: photo_tag = None if photo_tag: photo_type = get_attr(photo_tag,'imgtype') photo_location = get_text(photo_tag.getElementsByTagName('extref')[0]) photo = self.binarydata.getData(photo_location, self.prefix) else: photo = None try: s_r = person.getElementsByTagName('systemrole')[0] systemrole = get_attr(s_r,'systemroletype') if systemrole == 'SysAdmin': roles.append('Manager') except: systemrole = '' try: i_r = get_text(person.getElementsByTagName('institutionrole')[0]) inst_role = get_attr(i_r,'institutionroletype') except: instrole = '' try: eriala = get_text(person.getElementsByTagName('orgname')[0]) except: eriala = '' try: comm = get_text(person.getElementsByTagName('comments')[0]) except: comm = '' try: huvid = get_text(person.getElementsByTagName('short')[0]) except: huvid = '' try: amet_huvid = get_text(person.getElementsByTagName('long')[0]) except: amet_huvid = '' try: label = get_text(person.getElementsByTagName('label')[0]) except: label = '' try: if um.kasKasutajaOlemas(userid) and hasattr(um, userid): tulem = "kasutaja "+userid+" olemas" print >> self.out, "User %s already exists. Skipping. Remind me to compare uniqs here" % userid #continue except AttributeError: pass #XXX: we need to do something clever here since acl_users folder does not support some function. if not um.kasKasutajaOlemas(userid): um.acl_users._doAddUser(userid,password,[],()) print >> self.out, "Adding user %s to acl_users folder" % userid if not hasattr(um, userid): kasutaja = um.add_user_fle(userid,('Member',)) print >> self.out, "Adding user %s to fle_users folder" % userid kasutaja = getattr(um, userid) #FIXME: keel given = given email = email eriala = eriala street = street addr2 = addr2 locality = locality country = country url = url voice = voice mobile = mobile label = label comm = comm huvid = huvid amet_huvid = amet_huvid kasutaja.edit_info(given,family,email,eriala,'keel',0,photo,'','',street,addr2, locality,country,url,voice,mobile,label,comm,huvid,amet_huvid) if photo != None: kasutaja.set_photo(photo,photo_type) us_lopp = time.time() return def createCourse(self,elem,courses,teacher, userArchive): """ creates an empty course, other functions fill this with data course will be named imported_+exported course name userArchive - zip may contain user's archive. """ vigu = 0 vead = [] try: title = elem.getElementsByTagName("imsmd:title")[0] title_c = get_text(title.getElementsByTagName("imsmd:langstring")[0]) except: vigu = vigu +1 vead.append('title') try: c_organisation = get_text(elem.getElementsByTagName("ivamd:organisation")[0]) except: c_organisation = "" vigu = vigu+1 vead.append('c_org') try: c_method = get_text(elem.getElementsByTagName("ivamd:methods")[0]) except: c_method = "" vigu = vigu+1 vead.append('c_method') try: c_quote = get_text(elem.getElementsByTagName("ivamd:quote")[0]) except: c_quote = "" vigu = vigu+1 vead.append('c_quote') try: c_status = int(get_text(elem.getElementsByTagName("ivamd:status")[0])) except: c_status = 0 vigu = vigu+1 vead.append('c_stat') try: c_picture_name = get_attr(elem.getElementsByTagName("ivamd:picture")[0],"href") c_picture = self.binarydata.getData(c_picture_name,self.prefix) except: c_picture = None vigu = vigu+1 vead.append('c_pict') try: cat = int(get_text(elem.getElementsByTagName("organization_sec")[0])) except: cat = 0 vigu = vigu + 1 vead.append('cat') try: score = get_text(elem.getElementsByTagName("score")[0]) except: score = "" vigu = vigu + 1 vead.append('score') try: code = get_text(elem.getElementsByTagName("code")[0]) except: code = "" vigu = vigu + 1 vead.append('code') try: start_time = get_text(elem.getElementsByTagName("start_time")[0]) except: start_time = "" vigu = vigu + 1 vead.append('start') try: end_time = get_text(elem.getElementsByTagName("end_time")[0]) except: end_time = "" vigu = vigu + 1 vead.append('end') try: desk = elem.getElementsByTagName("imsmd:description")[0] desk_c = get_text(desk.getElementsByTagName("imsmd:langstring")[0]) # title_c = "imported_"+title_c except: desk = '' vigu = vigu+1 vead.append('desk') try: uniq_id = get_text(elem.getElementsByTagName("uniqID")[0]) except: uniq_id = None #vigu = vigu + 1 don't count vigu here vead.append('uniq_id') if vigu < 7: if not userArchive: id = courses.add_course_impl(teacher) course = courses.get_child(str(id)) course.remove_person(teacher) if not start_time: start_time = time.time() if not end_time: end_time = time.time() title_c = title_c desk_c = desk_c c_organisation = c_organisation c_method = c_method c_quote = c_quote course.update(title_c,desk_c,c_organisation,c_method,float(start_time),float(end_time),c_quote,c_picture,c_status, cCat=cat, credits=score, courseID=code) #course.kontrolliRyhmaLaud() course.add_folder('CourseFolder') return 1, course else: course = courses.get_course_by_uniqid(uniq_id) print "COURSE:", course return 1, course.getObject() else: #pass print "vigu:", vigu print "vead:", vead print "vigu tekkis liialt palju, jätan kursuse tegemise katki" #raise 'IVA Error:', 'BLAAH!' return 0, 0 def lisaSyndmus(self,resource,tiitel): from Kalender import KalendriSyndmus, KalendriSyndmusteKataloog for fail in resource.getElementsByTagName('file'): href = get_attr(fail,'href') syndmused = self.binarydata.getData(str(self.prefix+href)) synd = syndmused.split('\r\n') synd_p = [] for abc in synd: try: if abc[0] == ' ': eelmine = synd_p.pop() synd_p.append(eelmine + " "+abc) else: synd_p.append(abc) except: pass final = [] for abc in synd_p: uu = abc.split(':',1) final.append(uu) v_cal = 0 v_event = 0 v_loend = 0 for syndmus in final: # XXX:FIX: need testing summary = "" #Time 2005/05/25 10:56:36.475 GMT+3 #User Name (User Id) KristaEskla (KristaEskla) #Request URL http://iva.htk.tlu.ee/courses/4767/gf/teacher_import_handler #Exception Type UnboundLocalError #Exception Value local variable 'summary' referenced before assignment # #Traceback (innermost last): # #* Module ZPublisher.Publish, line 101, in publish #* Module ZPublisher.mapply, line 88, in mapply #* Module ZPublisher.Publish, line 39, in call_object #* Module Products.iva.Course, line 1236, in teacher_import_handler #* Module Products.iva.ImportExportIMS, line 2051, in processFile #* Module Products.iva.ImportExportIMS, line 1915, in otsiTOC #* Module Products.iva.ImportExportIMS, line 1852, in otsiTarget #* Module Products.iva.ImportExportIMS, line 1781, in leiaLahendus #* Module Products.iva.ImportExportIMS, line 2051, in processFile #* Module Products.iva.ImportExportIMS, line 1915, in otsiTOC #* Module Products.iva.ImportExportIMS, line 1852, in otsiTarget #* Module Products.iva.ImportExportIMS, line 1775, in leiaLahendus #* Module Products.iva.ImportExportIMS, line 1443, in lisaSyndmus #UnboundLocalError: local variable 'summary' referenced before assignment if syndmus[0] == 'BEGIN' and syndmus[1] == 'VCALENDAR': v_cal = 1 if not v_cal: continue if syndmus[0] == 'BEGIN' and syndmus[1] == 'VEVENT': v_event = 1 if not v_event: # no point to collect data continue # if we don't have event if syndmus[0] == 'DTSTART' or syndmus[0] == 'DTEND': temp = syndmus[1] aeg = [] aeg.append(int(temp[0:4])) aeg.append(int(temp[4:6])) aeg.append(int(temp[6:8])) aeg.append(int(temp[9:11])) aeg.append(int(temp[11:13])) aeg.append(int(temp[13:15])) aeg.append(int(0)) aeg.append(int(0)) aeg.append(int(0)) if syndmus[0] == 'DTSTART': v_loend = v_loend +1 dtstart = time.mktime(aeg) if syndmus[0] == 'DTEND': v_loend = v_loend +1 dtend = time.mktime(aeg) if syndmus[0] == 'N': v_loend = v_loend +1 n = syndmus[1] if syndmus[0] == 'DESCRIPTION': v_loend = v_loend +1 desc = syndmus[1] if syndmus[0] == 'SUMMARY': v_loend = v_loend +1 summary = syndmus[1] if syndmus[0] == 'STATUS': v_loend = v_loend +1 status = int(syndmus[1]) if syndmus[0] == 'CUTYPE': v_loend = v_loend +1 cutype = syndmus[1] if syndmus[0] == 'URL': v_loend = v_loend +1 url = syndmus[1] if syndmus[0] == 'X-PROPERTY;VALUE=NAME': v_loend = v_loend +1 name = syndmus[1] if syndmus[0] == 'RESOURCES': v_loend = v_loend +1 res = syndmus[1] if syndmus[0] == 'END' and syndmus[1] == 'VCALENDAR': break if not v_cal: continue if syndmus[0] == 'END' and syndmus[1] == 'VEVENT': if v_loend > 5: # try make an event koht = None if cutype == 'INDIVIDUAL': try: temp = getattr(self.iva.fle_users, n) koht = getattr(temp, 'syndmused') except: pass if cutype == 'GROUP': try: temp = getattr(self.iva.courses, str(self.kursus.get_id())) except AttributeError: print "XXXX: we couldn't find a course we should have made!" continue if n == 'kursus': try: koht = getattr(temp, 'syndmused') except AttributeError: print koht print temp print temp.absolute_url(), self.kursus.get_id() #koht = temp.kysiSyndmusteKataloog() else: try: temp2 = getattr(temp.subgroups, n) koht = getattr(temp2, 'syndmused') except: loodud = self.kursus.subgroups.add_subgroup(n) loodud.manage_delLocalRoles((self.uname,)) koht = getattr(loodud, 'syndmused') if not koht: break # no place to event syndmus = koht.lisaSyndmusKohe(name, desc, dtstart, dtend, url, res, status) self.set_omanik(syndmus, self.owner) v_event = 0 v_loend = 0 if not v_event: continue return def parseRSSFile(self, elem, point): for entry in elem.getElementsByTagName('item'): title = get_text(entry.getElementsByTagName('title')[0]).strip() creator = get_text(entry.getElementsByTagName('creator')[0]).strip() desc = get_text(entry.getElementsByTagName('description')[0]).strip() content = get_text(entry.getElementsByTagName('content')[0]).strip() pubDate = get_text(entry.getElementsByTagName('pubDate')[0]).strip() # handle comments here... obj = point.addEntry(None, title, desc, content, '', creator, float(pubDate)) self.set_omanik(obj, self.owner) def importUsersBlogs(self, resource, title): """ import user's blog """ if 'KURSUS' in title: print "importing course blog" if 'KASUTAJA' in title: print "importing user's blog" for org in resource.getElementsByTagName('organization'): id = get_attr(org, 'identifier') tmp = id.split('_') tmp.remove('user') tmp.remove('blog') juuser = '_'.join(tmp) self.owner = juuser for item in org.getElementsByTagName('item'): idref = get_attr(item,'identifierref') self.otsiTarget(idref, resource,title) self.owner = self.uname return def ImportUserEvents(self,resource,tiitel): for org in resource.getElementsByTagName('organization'): id = get_attr(org,'identifier') temp = id.split('_') temp.remove('user') temp.remove('events') juuser = '_'.join(temp) self.owner = juuser for item in org.getElementsByTagName('item'): idref = get_attr(item,'identifierref') tiitel = get_text(item.getElementsByTagName('title')[0]) self.otsiTarget(idref,resource,tiitel) self.owner = self.uname return def add_file_metadata(self,object,element): """ file metadata extraction and handling here only """ try: md = element.getElementsByTagName('metadata')[0] creationtime = float(get_attr(md,'creationtime')) object.set_timestamp(creationtime) except: pass for an in element.getElementsByTagName('annotation'): type = get_attr(an, 'parameters') if type != 'comment': continue person = get_text(an.getElementsByTagName('person')[0]) date = float(get_text(an.getElementsByTagName('date')[0])) desc = get_text(an.getElementsByTagName('description')[0]) status = int(get_text(an.getElementsByTagName('status')[0])) value = get_text(an.getElementsByTagName('value')[0]) object.postComment(None,'add',desc,value,status,person,date) try: p = element.getElementsByTagName('permission')[0] except: return status = int(get_text(p.getElementsByTagName('status')[0])) #permission status actually object.setObjPermission(status) position = int(get_text(p.getElementsByTagName('position')[0])) object.weight = position tl = p.getElementsByTagName('timelimits')[0] tend = float(get_attr(tl, 'end')) tstart = float(get_attr(tl, 'start')) object.setTimeLimits(tstart,tend) tfunc = int(get_text(tl)) object.setTimeFunc(tfunc) docstatus = int(get_text(p.getElementsByTagName('docstatus')[0])) object.setStatus(docstatus) commentable = int(get_text(p.getElementsByTagName('commentable')[0])) object.setCommentable(commentable) comment_view = int(get_attr(p.getElementsByTagName('commentable')[0],'view')) object.setCommentsVisible(comment_view) rateable = int(get_text(p.getElementsByTagName('rateable')[0])) object.setRateable(rateable) rate_view = int(get_attr(p.getElementsByTagName('rateable')[0],'view')) object.setRatingVisible(rate_view) return def add_folder_metadata(self,object,element): """ folder metadata extraction here ONLY! """ #try: # pres = get_attr(element,'presentation') # if pres == '1': # object.seaEsitluskaust(int(pres)) #except AttributeError: # pass try: md = element.getElementsByTagName('metadata')[0] #XXX: We don't have any metadata. Happened when exported just a wiki except IndexError: return try: creationtime = float(get_attr(md,'creationtime')) object.set_timestamp(creationtime) except: pass for an in element.getElementsByTagName('annotation'): type = get_attr(an, 'parameters') if type != 'visit': continue person = get_text(an.getElementsByTagName('person')[0]) date = float(get_text(an.getElementsByTagName('date')[0])) # TODO:XXX: viewedObject() #object.kylastus(None,person,date) try: p = element.getElementsByTagName('permission')[0] except: return status = int(get_text(p.getElementsByTagName('status')[0])) #permission status actually object.setObjPermission(status) position = int(get_text(p.getElementsByTagName('position')[0])) object.weight = position tl = p.getElementsByTagName('timelimits')[0] tend = float(get_attr(tl, 'end')) tstart = float(get_attr(tl, 'start')) object.setTimeLimits(tstart,tend) tfunc = int(get_text(tl)) object.setTimeFunc(tfunc) return def handleMetadata(self,element): try: format = get_text(element.getElementsByTagName('imsmd:format')[0]) except: format = 'application/msword' return format def set_omanik(self,obj,omanik): try: obj.manage_setLocalRoles(omanik, ('Owner',)) acl = self.iva.fle_users.leiaKasutajaKataloog(omanik) acl_user = acl.aq_inner.getUser(omanik).__of__(acl) #obj.changeOwnership(self.iva.acl_users.getUser(omanik).__of__(self.iva.acl_users)) obj.changeOwnership(acl_user) except: return 0 return 1 def handleResource(self,resource,asukoht,resour,tiitel=''): tyyp = get_attr(resource,'type') for element in resource.childNodes: # otsi ja leia failid, memod jms if element.nodeName == 'dependency': idref = get_attr(element, 'identifierref') self.otsiTarget(idref,resour,'MISC STUFF',asukoht) if element.nodeName != 'file': #võib vajada fixi. continue else: tekst = '' orgID = '' xml_real_name = '' try: href = get_attr(element,'href') except: pass try: tekst = get_text(element.getElementsByTagName('imsmd:title')[0]) except: pass try: if not tekst: tekst = get_text(element.getElementsByTagName('title')[0]) except: pass try: xml_real_name = get_attr(element.getElementsByTagName('imsmd:title')[0],'realname') except: pass try: orgID = get_attr(element.getElementsByTagName('imsmd:title')[0],'originalID') except: pass try: orgID = get_attr(element.getElementsByTagName('title')[0],'originalID') except: pass try: orgOwner = get_attr(element.getElementsByTagName('imsmd:title')[0],'originalOwner') except: orgOwner = '' try: orgOwner = get_attr(element.getElementsByTagName('title')[0],'originalOwner') except: pass if tyyp == 'file' or tyyp == 'webcontent': fail_alg = time.time() # FIXFIXFIX: faili formatt try: format = self.handleMetadata(element.getElementsByTagName('metadata')[0]) except: format = 'application/msword' href = get_attr(element,'href') data_alg = time.time() data = self.binarydata.getData(str(self.prefix+href)) data_lopp = time.time() print "Faili lugemine",data_lopp-data_alg # FIX: file type number = 1 if not tekst: tekst = tiitel number = 0 lisa_alg = time.time() if len(orgID)>3: if orgID[-4] == '.': # .doc or something realname = orgID if xml_real_name: realname = xml_real_name else: realname = '' tek = tekst fail = asukoht.add_file(tek,FakeUpload(tek,data,format),realname) lisa_lopp = time.time() print "lisa fail:",lisa_lopp-lisa_alg result = self.set_omanik(fail,orgOwner) if not result: self.set_omanik(fail,self.owner) self.add_file_metadata(fail,element) fail_lopp = time.time() print "Fail:",fail_lopp-fail_alg if tyyp == 'memo': data = self.binarydata.getData(str(self.prefix+href)) memo = asukoht.add_memo(tekst,data) # memo.set_author(self.owner) memo.manage_delLocalRoles((self.uname,)) result = self.set_omanik(memo,orgOwner) if not result: self.set_omanik(memo,self.owner) self.add_file_metadata(memo,element) if tyyp == 'URL': tek = tekst link = asukoht.add_link(tek,href) # link.set_author(self.owner) link.manage_delLocalRoles((self.uname,)) result = self.set_omanik(link,orgOwner) if not result: self.set_omanik(link,self.owner) self.add_file_metadata(link,element) #XXX:wikipages and owners if tyyp == 'wikipage': if hasattr(asukoht.aq_inner.aq_self, tiitel): # wikipage = getattr(asukoht, tiitel) asukoht._delObject(tiitel) data = self.binarydata.getData(str(self.prefix+href)) # wikipage = ZWikiPage(source_string=data, __name__=tiitel) wikipage = ZWikiPage(source_string=data, __name__=tiitel) parent = [self.wikiparent] wikipage.parents = parent #XXX: we d'nt catalog wikis. # wikipage.index_object() asukoht._setObject(tiitel,wikipage) # wikipage += 'lammas tuli külla!!' if tyyp == 'wikiimage' or tyyp == 'wikifile': if hasattr(asukoht.aq_inner.aq_self, tiitel): asukoht._delObject(tiitel) data = self.binarydata.getData(str(self.prefix+href)) if tyyp == 'wikiimage': asukoht._setObject(tiitel, OFS.Image.Image(tiitel,tiitel,'')) else: asukoht._setObject(tiitel, OFS.Image.File(tiitel,tiitel,'')) asukoht._getOb(tiitel).manage_upload(data) print "tyyp:::", tyyp return def createFolder(self,asukoht,nimi,orgID='',orgOwner=''): #XXX: we should try to handle real object id here somehow! # loo kaust # try: print ">>>>>>>>>>>> KAUSTA <<<" print asukoht.absolute_url(), orgID #print nimi, asukoht.meta_type orgID = str(orgID) if 1==1: # handle folder with id "esitlused" - it's Portfolio() now. if orgID == 'esitlused' or orgID == 'portfolio': print "tegin esitlised" kaust = asukoht.add_portfolio() else: if asukoht.meta_type == 'Portfolio' and orgID.startswith('too'): # if we're in portfolio, add AssignmentProxy. kaust = asukoht.createAssignmentProxy(orgID, self.kursus.get_id()) else: orgID = str(orgID) kaust = asukoht.add_folder(nimi,orgID) if orgOwner: self.set_omanik(kaust, orgOwner) else: self.set_omanik(kaust, self.owner) # except: return kaust def otsiWikid(self,item,tiitel,asukoht,resour,start=0,vanem=''): tit = get_text(item.getElementsByTagName('title')[0]) self.wikiparent = '' idref = get_attr(item,'identifierref') start = 0 if not start: self.wikiparent = vanem self.otsiTarget(idref,resour,tit,asukoht) for bcd in item.childNodes: if bcd.nodeType == 1 and bcd.nodeName == 'item': temp = tit self.otsiWikid(bcd,tiitel,asukoht,resour,vanem=temp) return def leiaLahendus(self,target,tiitel,asukoht='',resour='',params=''): try: xmlbase = get_attr(target,'xml:base') except: xmlbase = '' type = get_attr(target,'type') href = get_attr(target,'href') if params == 'wikifolder' and not self.testonly: wiki_resour = target.getElementsByTagName('resources')[0] # for wiki_items in target.getElementsByTagName('item'): #XXX: we try and except since something happened here what should not happen try: wiki_items = target.getElementsByTagName('item')[0] except IndexError: return self.otsiWikid(wiki_items,tiitel,asukoht,wiki_resour,start=1) return if re.search('webcontent',type) and not self.testonly: # läheb raamaturiiulile # temp = getattr(self.iva.courses, self.kursus.get_id()) # asukoht = getattr(temp, 'gf') print >> self.out, "1Preparing to add webtop item" self.handleResource(target,asukoht,resour,tiitel) if re.search('file',type) or re.search('memo',type) or re.search('URL',type) or re.search('wiki',type): if not self.testonly: print "testonly: ",self.testonly print >> self.out, "2Preparing to add webtop item" self.handleResource(target,asukoht,resour,tiitel) if re.search('Calenda',type) and not self.testonly: print >> self.out, "Preparing to add events" self.lisaSyndmus(target,tiitel) if re.search('imscp',type): if not self.kursus: self.kursus = self.coursemanager print >> self.out, "1Preparing to parse other xml file (possible course)" abc = Importer(self.uname,self.iva,xmlbase,self.kursus,self.binarydata,userwikionly=self.userwikionly,kasuta_jooksvat=self.kasuta_jooksvat,testonly=self.testonly, userArchive=self.userArchive, out=self.out) try: failisisu = abc.loadFile(self.prefix+xmlbase+href) except KeyError: return abc.processFile(self.kursus,failisisu) self.processed.append(target) if re.search('imsqti',type): print >> self.out, "2Preparing to parse other xml file (possible qti)" abc = Importer(self.uname,self.iva,self.prefix+xmlbase,self.kursus,self.binarydata,userwikionly=self.userwikionly,kasuta_jooksvat=self.kasuta_jooksvat,testonly=self.testonly, userArchive=self.userArchive, out=self.out) failisisu = abc.loadFile(self.prefix+xmlbase+href) abc.processFile(self.kursus,failisisu) self.processed.append(target) if re.search('meediapaja',type) or re.search('teadmuspaja',type) or params == 'Assignments' and not self.testonly: print >> self.out, "3Preparing to parse another xml file (possible Jamming, KB, Assignments)" abc = Importer(self.uname,self.iva,self.prefix+xmlbase,self.kursus,self.binarydata,userwikionly=self.userwikionly,kasuta_jooksvat=self.kasuta_jooksvat,testonly=self.testonly, userArchive=self.userArchive, out=self.out) failisisu = abc.loadFile(self.prefix+xmlbase+href) abc.processFile(self.kursus,failisisu) self.processed.append(target) if re.search('SYNDMUSED',tiitel) and not self.testonly: print >> self.out, "4Preparing to add user's events. uh..." self.ImportUserEvents(target,tiitel) if params == 'RAAMATURIIUL' and not self.testonly: print >> self.out, "5Found webtop content" r_res = target.getElementsByTagName('resources')[0] print "leiaLahendus", asukoht for r_org in target.getElementsByTagName('organization'): self.otsiTOC(r_org, r_res) if params == 'RYHMALAUAD' and not self.testonly: tmpasukoht = self.riiuliasukoht print >> self.out, "6Found possible subgroup content" self.riiuliasukoht = getattr(self.kursus,'subgroups') r_res = target.getElementsByTagName('resources')[0] for r_org in target.getElementsByTagName('organization'): self.otsiTOC(r_org, r_res,subgroup=1) self.riiuliasukoht = tmpasukoht if type == 'blog' and not self.testonly: good_to_go = 1 store = self.kursus.Blog if 'KASUTAJATE' in tiitel: try: usrblog = getattr(getattr(self.iva.fle_users, self.owner).webtop, 'c'+self.kursus.getId()).portfolio except AttributeError: print "*"*50 print "this guy, %s, doesn't have a course folder..." % self.owner print "*"*50 good_to_go = 0 if good_to_go: store = usrblog if good_to_go: abc = Importer(self.uname,self.iva,self.prefix+xmlbase,self.kursus,self.binarydata,userwikionly=self.userwikionly,kasuta_jooksvat=self.kasuta_jooksvat,testonly=self.testonly, userArchive=self.userArchive, out=self.out) abc.riiuliasukoht = store for fref in target.getElementsByTagName('file'): fhref = get_attr(fref, 'href') failisisu = abc.loadFile(self.prefix+xmlbase+fhref) abc.processFile(self.kursus,failisisu) self.processed.append(target) if not self.testonly and (params == 'KASUTAJATE BLOGID' or params == 'KURSUS BLOG'): print >> self.out, "7Preparing to import user's blogs" self.importUsersBlogs(target, tiitel) if params == 'SISEGRUPP' and not self.testonly: tmpasukoht = self.riiuliasukoht print >> self.out, "8Preparing to add subgroups" self.riiuliasukoht = getattr(self.kursus,'subgroups') s_res = target.getElementsByTagName('resources')[0] for s_org in target.getElementsByTagName('organization'): s_nimi = get_attr(s_org,'identifier').split(':')[1] sgs = self.riiuliasukoht.getSubgroupByName(s_nimi) if not sgs: ssg = self.riiuliasukoht.add_subgroup(s_nimi) print >> self.out, "4Added subgroup: %s" % ssg.absolute_url() ssg.manage_delLocalRoles((self.uname,)) #XXX:subgroup name mapping! self.riiuliasukoht = self.riiuliasukoht.getSubgroupByName(s_nimi) if not self.riiuliasukoht: self.riiuliasukoht = self.kursus.subgroups.add_subgroup(s_nimi) print >> self.out, "5Added subgroup: %s" % self.riiuliasukoht.absolute_url() self.riiuliasukoht.manage_delLocalRoles((self.uname,)) if not self.riiuliasukoht: print self.kursus.subgroups.objectValues() raise 'IVA import Error:', 'Subgroup creation failed.' self.otsiTOC(s_org,s_res) self.riiuliasukoht = tmpasukoht if params == 'KASUTAJATE WEBTOPID' or params == 'KASUTAJATE SAHTLID' and not self.testonly: print >> self.out, "Preparing to add user's webtop content" tmpasukoht = self.riiuliasukoht u_res = target.getElementsByTagName('resources')[0] for u_org in target.getElementsByTagName('organization'): identer = get_attr(u_org,'identifier').split('_') identer.remove('user') try: identer.remove('webtop') except ValueError: pass try: identer.remove('sahtel') except ValueError: pass #u_ident = get_attr(u_org,'identifier').split('_')[1] u_ident = '_'.join(identer) self.owner = u_ident #XXX: we didn't export admin in previous versions and we're in # trouble now ;( if not hasattr(self.kursus.fle_users, u_ident): continue if params == 'KASUTAJATE SAHTLID': tmp = getattr(self.kursus.fle_users, u_ident).webtop else: tmp = getattr(self.kursus.fle_users, u_ident).webtop if not hasattr(tmp, 'c'+str(self.kursus.id)): #cf = tmp.lisa_kursuse_kaust_ilma_requestita(self.owner,str('c'+str(self.kursus.id))) cf = tmp.add_folder( str('c'+str(self.kursus.id)), str('c'+str(self.kursus.id)) ) print >> self.out, "Creating: %s " % cf.absolute_url() cf.set_author(self.owner) self.set_omanik(cf,self.owner) self.riiuliasukoht = getattr(tmp, 'c'+str(self.kursus.id)) self.otsiTOC(u_org, u_res) self.owner = self.uname self.riiuliasukoht = tmpasukoht return def otsiTarget(self,identifier,ressursid,tiitel,asukoht='',params=''): for target in ressursid.getElementsByTagName("resource"): id = get_attr(target,'identifier') if id == identifier and not self.vaataKasTehtud(target): self.leiaLahendus(target,tiitel,asukoht,ressursid,params) self.processed.append(target) return if id == identifier and self.vaataKasTehtud(target): #print "---------------- leidsime sobiva kuid see oli juba tehtud :( jama!!!! target1" pass for target in self.root.getElementsByTagName("manifest"): id = get_attr(target,'identifier') if id == identifier and not self.vaataKasTehtud(target): self.leiaLahendus(target,tiitel,asukoht,ressursid,params=params) self.processed.append(target) return if id == identifier and self.vaataKasTehtud(target): #print "---------------- leidsime sobiva kuid see oli juba tehtud :( jama!!!! target2" pass for target in self.root.getElementsByTagName("organization"): id = get_attr(target,'identifier') if id == identifier and not self.vaataKasTehtud(target): self.leiaLahendus(target,tiitel,asukoht,ressursid,params=params) self.processed.append(target) return if id == identifier and self.vaataKasTehtud(target): pass #print "---------------- leidsime sobiva kuid see oli juba tehtud :( jama!!!!otsiTarget3" #print "identifieri " + identifier + " pole võimalik leida" return def otsiTOC(self,element,ressursid,subgroup=0): #XXX:keyword subgroup should go away, it's needed to import iva ver 0.4.2 subgroup portfolios for target in element.childNodes: tiitel = '' if not target.nodeName == 'item': continue identifier = get_attr(target,'identifier') try: identifierref = get_attr(target,'identifierref') except: identifierref = '' try: params = get_attr(target,'parameters') except: params = '' try: tiitel = get_text(target.getElementsByTagName('title')[0]) except IndexError: tiitel = 'title error' try: orgID = get_attr(target.getElementsByTagName('title')[0],'originalID') except: orgID = '' try: orgOwner = get_attr(target.getElementsByTagName('title')[0],'originalOwner') except: orgOwner = '' if self.vaataKasTehtud(target): continue else: self.processed.append(target) if identifierref: if params == 'wikifolder': tmp = self.riiuliasukoht self.riiuliasukoht = self.riiuliasukoht.add_wiki(tiitel, 1) self.add_folder_metadata(self.riiuliasukoht,target) if orgOwner: self.set_omanik(self.riiuliasukoht, orgOwner) else: self.set_omanik(self.riiuliasukoht, self.owner) print >> self.out, "1Created wiki folder @ %s " % self.riiuliasukoht.absolute_url() self.otsiTarget(identifierref, ressursid, tiitel,asukoht=self.riiuliasukoht,params=params) if params == 'wikifolder': self.riiuliasukoht = tmp else: #XXX:That's freaky stuff. if not subgroup: if not self.testonly: print "2047", self.riiuliasukoht self.riiuliasukoht = self.createFolder(self.riiuliasukoht, tiitel,orgID,orgOwner) self.add_folder_metadata(self.riiuliasukoht,target) print >> self.out, "2Created folder: %s " % self.riiuliasukoht.absolute_url() else: m = self.riiuliasukoht.objectValues() korras = 0 sgs = self.riiuliasukoht.getSubgroupByName(tiitel) if not sgs: self.kursus.subgroups.add_subgroup(tiitel) sgp = self.riiuliasukoht.getSubgroupByName(tiitel) sgp.manage_delLocalRoles((self.uname,)) print >> self.out, "3Created subgroup: %s " % sgp.absolute_url() korras = 1 if sgs: korras = 1 self.riiuliasukoht = self.kursus.subgroups.getSubgroupByName(tiitel) #for x in m: # if x.get_id() == tiitel: # self.riiuliasukoht = getattr(self.riiuliasukoht, x.get_id()) # korras = 1 # break if not korras: self.riiuliasukoht = self.kursus.subgroups.add_subgroup(tiitel) self.riiuliasukoht.manage_delLocalRoles((self.uname,)) print >> self.out, "4Created subgroup: %s " % self.riiuliasukoht.absolute_url() #self.riiuliasukoht = getattr(self.riiuliasukoht, kaust) self.otsiTOC(target, ressursid) if not self.testonly: self.riiuliasukoht = self.riiuliasukoht.aq_parent return def vaataKasTehtud(self,element): for abc in self.processed: if element.isSameNode(abc): return 1 return 0 def otsiKasKasutajaid(self,element): try: res_xmlbase = get_attr(element,'xml:base') except: res_xmlbase = '' for res in element.getElementsByTagName('resource'): if re.search('imsent',get_attr(res,'type')) and not self.vaataKasTehtud(res): try: xmlbase = get_attr(res,'xml:base') except: xmlbase = '' try: href = get_attr(res.getElementsByTagName('file')[0],'href') except: return print "hohoho" abc = Importer(self.uname,self.iva,self.prefix+xmlbase,self.kursus,self.binarydata,userwikionly=self.userwikionly,kasuta_jooksvat=self.kasuta_jooksvat,testonly=self.testonly, userArchive=self.userArchive, out=self.out) failisisu = abc.loadFile(self.prefix+xmlbase+href) abc.processFile(self.kursus,failisisu) # self.importUsers(res) self.processed.append(res) if not failisisu: failisisu = '' return failisisu for res in self.root.getElementsByTagName('resource'): if re.search('imsent',get_attr(res,'type')) and not self.vaataKasTehtud(res): # self.importUsers(res) # self.processed.append(res) abc = Importer(self.uname,self.iva,self.prefix+xmlbase,self.kursus,self.binarydata,userwikionly=self.userwikionly,kasuta_jooksvat=self.kasuta_jooksvat,testonly=self.testonly, userArchive=self.userArchive, out=self.out) failisisu = abc.loadFile(self.prefix+xmlbase+href) abc.processFile(self.kursus,failisisu) self.processed.append(res) return failisisu return def otsiAlgandmed(self,elem): userArchive = None try: metadata = elem.getElementsByTagName("metadata")[0] except: pass #raise 'IVA Error','Missing toplevel-manifest metadata element' try: resources = elem.getElementsByTagName("resources")[0] except: pass #raise 'IVA Error','Missing toplevel-manifest resources element' try: organizations = elem.getElementsByTagName("organizations")[0] except: raise 'IVA Error','Missing toplevel-manifest organizations element' try: userArchive = elem.getElementsByTagName("userArchive")[0].getAttribute('archive') except: pass return metadata, organizations, resources, userArchive def processFile(self,coursemanager,element): """ process zip and find anything usable. """ proc_alg = time.time() self.owner = self.uname self.coursemanager = coursemanager i = 0 kas_kursus = 0 kursus = None st_elem = element.firstChild self.root = element.firstChild # return success = self.kontrolliEsimeneElement(st_elem,coursemanager) if success: return # Siit edasi eeldame, et tegemist on CP-ga ok = 0 j = 0 try: st_elem = element.getElementsByTagName("manifest")[0] except IndexError: print "XXX:for now, we totally failed reading file." return self.root = st_elem metadata,orgnz, resources, userArchive = self.otsiAlgandmed(st_elem) if not self.userArchive: self.userArchive = userArchive if self.userArchive: print >> self.out, "Zip file identified as user's archive." if not self.userwikionly and not self.testonly and not self.kasuta_jooksvat: kas_kursus, kursus = self.createCourse(metadata, coursemanager, self.uname, self.userArchive) if kas_kursus: self.kursus = kursus self.riiuliasukoht = getattr(self.kursus, 'gf') if self.userArchive: print >> self.out, "Using course %s with id %s" % (self.kursus.get_name(), self.kursus.get_id()) else: self.riiuliasukoht = '' #print "sellest failist ei saanud kursust teha\nProovin kasutada jooksvat kursust" if self.userwikionly: self.riiuliasukoht = self.userwikionly if self.kasuta_jooksvat: self.riiuliasukoht = getattr(self.kursus, 'gf') failisisu = self.otsiKasKasutajaid(resources) for org in orgnz.childNodes: if org.nodeName == 'organization': self.otsiTOC(org,resources) # kursus ja sisegrupid peaksid olema nüüd loodud. paigutan kasutajad. if failisisu and not self.userwikionly and not self.testonly and not self.kasuta_jooksvat: self.restoreMembership(failisisu) proc_lopp = time.time() print "Process Zip:",proc_lopp-proc_alg def loadZip(self,file): zip = zipfile.ZipFile(file,"r",zipfile.ZIP_DEFLATED) for name in zip.namelist(): # if not re.search('imsmanifest.xml',name): self.binarydata.restoreData(name,zip.read(name)) zip.close() def loadFile(self,nimi): xmlstring = self.binarydata.getData(nimi) print "loen faili: ",nimi try: fish = xml.dom.minidom.parseString(xmlstring) except UnicodeError, ExpatError: print "XML data is not utf-8 encoded! Detecting encoding..." encoding = re.search('encoding="([-a-zA-Z0-9]*)">',xmlstring).group(1) print "Transforming from %s to utf-8..." % encoding xmlstring = unicode(xmlstring,encoding).encode("utf-8") fish = xml.dom.minidom.parseString(xmlstring) return fish def kontrolliEsimeneElement(self,element,courses): print "nodeName>",element.nodeName if self.testonly and element.nodeName!='questestinterop': try: element = element.getElementsByTagName("questestinterop")[0] except: pass if element.nodeName =='MailMessages': if self.userwikionly or self.testonly: return "kala" self.importMailMessages(element) return "MM" if element.nodeName == 'CourseCats': if self.userwikionly or self.testonly: return "kala" self.importCourseCats(element) return "CC" if element.nodeName == 'Affiliations': if self.userwikionly or self.testonly: return "kala" self.importAffliationCodes(element) return "AC" if element.nodeName == 'Settings': if self.userwikionly or self.testonly: return "kala" self.importServerSettings(element) return "IS" if element.nodeName == 'questestinterop': if self.userwikionly: return "kala" #Jump to qti import self.importQT(element,self.kursus,self.uname) return "QT" if element.nodeName == 'enterprise': if self.userwikionly or self.testonly: return "kala" #FIX: 1st element is DocumentType abc = element.nextSibling if not self.kasuta_jooksvat: self.importUsers(abc) else: print "users not imported" #Jump to user import return "EP" if element.nodeName == 'Assignments': if self.userwikionly or self.testonly: return "kala" self.importKodutood(element) return "Ass" if element.nodeName == 'Jamming': if self.userwikionly or self.testonly: return "kala" print " import Jamming" kala = Exporter(self.iva,prefix=self.prefix) kala.binarydata = self.binarydata kala.importJamming(element,self.kursus) return "MP" if element.nodeName == 'KnowledgeBuilding': if self.userwikionly or self.testonly: return "kala" print " import KnowledgeBuilding" kala = Exporter(self.iva,prefix=self.prefix) kala.binarydata = self.binarydata for ctx in element.childNodes: kala.importContext(ctx,self.kursus) return "TP" if element.nodeName == 'rss': self.parseRSSFile(element, self.riiuliasukoht) return "BI" return 0