Public feed #894345

Mine

public text v1 · immutable
Raw Edit
·published 2008-02-06 21:48 UTC
rendered paste body
"""
Module to add units and people that are new in FS. Slightly more complex since they need to be created ;)
"""
from log.syslog import logmessage
from sb.connection import sbsession
from core.diff import *
from sb.model.models import *
from sb.get.people import *
from sb.get.allunits import *
from sqlalchemy.exceptions import DBAPIError, AssertionError
from util.string import washchars

#units first

logmessage('Starting addition')
logmessage('Adding %d new units' % (len(new_unit_list)))
for fs_id in new_unit_list:
        u = fsunits[fs_id]
        unit = Unit(fs_id, u[3], u[4])
        #Add directly to sbunits_add
        sbunits_add[fs_id] = unit

#Institutes

logmessage('Adding %d new insts' % (len(new_inst_list)))
for fs_id in new_inst_list:
        i = fsinsts[fs_id]
        ukey = str(i[0]) + "-0-0"
        inst = Institute(fs_id, i[3], i[4])
        if ukey in sbunits_add:
                sbunits_add[ukey].institutes.append(inst)
        else:
                #Unit does not exist... make it:
                unit = Unit(ukey, i[3], i[4])
                unit.noremove = True
                sbunits_add[ukey] = unit
                unit.institutes.append(inst)
        sbinsts_add[fs_id] = inst

#Groups

logmessage('Adding %d new groups' % (len(new_group_list)))
for fs_id in new_group_list:
        g = fsgroups[fs_id]
        group = Group(fs_id, g[3], g[4])
        ikey = str(g[0]) + "-" + str(g[1]) + "-0"
        ukey = str(g[0]) + "-0-0"
        if ikey in sbinsts_add:
                sbinsts_add[ikey].groups.append(group)
        else:
                #No institute, make it, and also check for unit to add THAT to...
                inst = Institute(ikey, g[3], g[4])
                inst.noremove = True
                if ukey in sbunits_add:
                        unit = sbunits_add[ukey]
                else:
                        sbunits_add[ukey] = Unit(ukey, g[3], g[4])
                        sbunits_add[ukey].noremove = True
                unit.institutes.append(inst)
                sbinsts_add[ikey] = inst
                inst.groups.append(group)
        sbgroups_add[fs_id] = group

#Studyprograms must be added before students

logmessage('Adding %d new studprogs' % (len(new_studprog_list)))
for acronym in new_studprog_list:
        sp = fsstudprogs[acronym]
        fs_id = str(sp[2]) + "-" + str(sp[3]) + "-" + str(sp[4])
        name = sp[1]
        #acronym = sp[0]
        studprog = StudyProgram(fs_id, acronym, name)
        sbstudprogs_add[acronym] = studprog
        if fs_id in sbgroups_add:
                sbgroups_add[fs_id].studyprograms.append(studprog)
        else:
                # We have a rogue studyprogram. We must get a group for it.
                # The FS ID will place it. First we need to check if the unit exists:
                unit_id = str(sp[2]) + '-0-0'
                inst_id = str(sp[2]) + '-' + str(sp[3]) + '-0'
                group_id = fs_id
                #Check if unit does not exist, if not, create it:
                if unit_id in sbunits_add:
                        unit = sbunits_add[unit_id]
                else:
                        unit = Unit(unit_id, acronym, name)
                        sbunits_add[unit_id] = unit
                        unit.noremove = True
                #Got unit. Check for institute.
                if inst_id in sbinsts_add:
                        inst = sbinsts_add[inst_id]
                else:
                        inst = Institute(inst_id, acronym, name)
                        unit.institutes.append(inst)
                        sbinsts_add[inst_id] = inst
                        inst.noremove = True
                #Got institute. Get group.
                if group_id in sbgroups_add:
                        group = sbgroups_add[group_id]
                else:
                        group = Group(group_id, acronym, name)
                        inst.groups.append(group)
                        sbgroups_add[group_id] = group
                        group.noremove = True
                #Ok now, append studyprogram...
                studprog.noremove = True
                group.studyprograms.append(studprog)

#Loop new units only and save them (those in sbunits_add will be commited automagically)
logmessage('Saving unit data to db')
for fs_id in new_unit_list:
        sbsession.save(sbunits_add[fs_id])

#Lets see, we now have to check subjects:

for subkey in new_subject_list:
        sub = fssubjects[subkey]
        acronym = washchars(sub[3])
        studprog = washchars(sub[7])
        subkey = acronym + '-' + studprog
        name = sub[4]
        subject = Subject(name, acronym)
        if studprog in sbstudprogs_add and not subkey in sbsubjects:
                sbstudprogs_add[studprog].subjects.append(subject)
                sbsession.save(subject)
                sbsubjects[subkey] = subje