def _cache_ou2sko(ou): """Fill up cache with ou_id -> sko mappings.""" ou = Stedkode.Stedkode(cerebrum_db) for row in ou.get_stedkoder(): sko = make_sko(row["fakultet"], row["institutt"], row["avdeling"]) _ou2sko_cache[int(row["ou_id"])] = sko
def get_ans_fak(fak_list, ent2uname): fak_res = {} person = Factory.get('Person')(db) stdk = Stedkode.Stedkode(db) for fak in fak_list: ans_list = [] # Get all stedkoder in one faculty for ou in stdk.get_stedkoder(fakultet=int(fak)): # get persons in the stedkode for pers in person.list_affiliations( source_system=const.system_sap, affiliation=const.affiliation_ansatt, ou_id=int(ou['ou_id'])): person.clear() try: person.find(int(pers['person_id'])) acc_id = person.get_primary_account() except Errors.NotFoundError: logger.debug("Person pers_id: %d , no valid account!" % \ person.entity_id) break if acc_id and ent2uname.has_key(acc_id): uname = ent2uname[acc_id]['NAME'] if uname not in ans_list: ans_list.append(uname) else: logger.debug("Person pers_id: %d have no account!" % \ person.entity_id) fak_res[int(fak)] = ans_list return fak_res
def output_all_OUs(): """Output all information about OUs.""" ou = Stedkode.Stedkode(cerebrum_db) # Fill up the cache for later usage. _cache_ou2sko(ou) xmlwriter.startElement("organization") output_elem("orgid", str(cereconf.DEFAULT_INSTITUSJONSNR), {"orgidtype": "institusjonsnummer"}) output_elem("orgname", "Høyskolen i Agder", { "lang": "no", "orgnametype": "name" }) output_elem("realm", "hia.no") # Now, output all OUs for r in ou.get_stedkoder(): sko = ou_id2sko(r["ou_id"]) assert sko, "No sko?" try: ou.clear() ou.find(r["ou_id"]) except Errors.NotFoundError: logger.warn("OU id %s does not exist, but its sko (%s) does", r["ou_id"], sko) continue # yrt xmlwriter.startElement("ou") output_elem("ouid", sko, {"ouidtype": "sko"}) output_elem("ouname", ou.name, {"ounametype": "name", "lang": "no"}) # TBD: Do we need parent_id? # parent_id = ou_id2parent_sko(ou) # if parent_id: # output_elem("parentid", parent_id, { "ouidtype" : "sko" }) # # fi xmlwriter.endElement("ou") # od xmlwriter.endElement("organization")
def output_all_ous(orgname): """Output all OUs in target organization.""" logger.debug("outputting all OUs") u = text_decoder(cerebrum_db.encoding) ou = Stedkode.Stedkode(cerebrum_db) _cache_ou2sko(ou) xmlwriter.startElement("organization") out("orgid", str(cereconf.DEFAULT_INSTITUSJONSNR), {"orgidtype": "institusjonsnummer"}) out("orgname", orgname, {"lang": "no", "orgnametype": "name"}) out("realm", cereconf.INSTITUTION_DOMAIN_NAME) # for each OU, output name and ID for row in ou.get_stedkoder(): ou_id = row["ou_id"] sko = ou_id2sko(ou_id) assert sko try: ou.clear() ou.find(ou_id) except Errors.NotFoundError: logger.warn("OU ID %s does not exist, but its sko (%s) does", ou_id, sko) xmlwriter.startElement("ou") out("ouid", sko, {"ouidtype": "sko"}) # FIXME: Is there any guarantee that lang==no holds? out( "ouname", u( ou.get_name_with_language(constants.ou_name_display, constants.language_nb)), { "ounametype": "name", "lang": "no" }) xmlwriter.endElement("ou") xmlwriter.endElement("organization")
import cerebrum_path import cereconf from Cerebrum import Errors from Cerebrum import OU from Cerebrum.Utils import Factory from Cerebrum.modules.no import Stedkode from Cerebrum.Utils import XMLHelper from Cerebrum.utils.atomicfile import MinimumSizeWriter db = Factory.get('Database')() const = Factory.get('CLConstants')(db) co = Factory.get('Constants')(db) ou = OU.OU(db) person = Factory.get('Person')(db) stedkode = Stedkode.Stedkode(db) account = Factory.get('Account')(db) xml = XMLHelper() f = None KiB = 1024 MiB = KiB**2 def get_person_oudisp(person): affs = person.get_affiliations() for aff in affs: stedkode.clear() stedkode.find(aff['ou_id']) return stedkode.name
def main(): # Håndter upper- og lowercasing av strenger som inneholder norske # tegn. locale.setlocale(locale.LC_CTYPE, ('en_US', 'iso88591')) init_globals() fxml.start_xml_head() # Finn `account_id` -> account-data for alle brukere. acc2names = load_acc2name() # Spytt ut PERSON-elementene. for user in acc2names.itervalues(): fxml.user_to_XML( user['NAME'], # Som påpekt av HiA i en e-post til cerebrum-hia # (<messsage-id:[email protected]), skal # vi bruke STATUS_ADD (autentiseringsrutinene har # endret seg og nå *skal* man levere dumpen med # recstatus=1). fronter_lib.Fronter.STATUS_ADD, user) # Registrer en del semi-statiske strukturnoder. root_node_id = "STRUCTURE:ClassFronter structure root node" register_group('Universitetet i Agder', root_node_id, root_node_id) manuell_node_id = 'STRUCTURE:%s:manuell' % \ cereconf.INSTITUTION_DOMAIN_NAME_LMS register_group('Manuell', manuell_node_id, root_node_id, allow_room=True) auto_node_id = "STRUCTURE:%s:automatisk" % \ cereconf.INSTITUTION_DOMAIN_NAME_LMS register_group("Automatisk", auto_node_id, root_node_id) emner_id = 'STRUCTURE:%s:fs:emner' % cereconf.INSTITUTION_DOMAIN_NAME_LMS register_group('Emner', emner_id, auto_node_id) this_sem, next_sem = get_semester() emner_this_sem_id = emner_id + ':%s:%s' % tuple(this_sem) emner_next_sem_id = emner_id + ':%s:%s' % tuple(next_sem) register_group('Emner %s %s' % (this_sem[1].upper(), this_sem[0]), emner_this_sem_id, emner_id) register_group('Emner %s %s' % (next_sem[1].upper(), next_sem[0]), emner_next_sem_id, emner_id) emnerom_this_sem_id = emner_this_sem_id + ':emnerom' emnerom_next_sem_id = emner_next_sem_id + ':emnerom' register_group('Emnerom %s %s' % (this_sem[1].upper(), this_sem[0]), emnerom_this_sem_id, emner_this_sem_id) register_group('Emnerom %s %s' % (next_sem[1].upper(), next_sem[0]), emnerom_next_sem_id, emner_next_sem_id) for sem, sem_node_id in ((this_sem, emner_this_sem_id), (next_sem, emner_next_sem_id)): for suffix, title in (('student', 'Studenter %s %s' % (sem[1].upper(), sem[0])), ('foreleser', 'Forelesere %s %s' % (sem[1].upper(), sem[0])), ('studieleder', 'Studieledere %s %s' % (sem[1].upper(), sem[0]))): node_id = sem_node_id + ':' + suffix register_group(title, node_id, sem_node_id) brukere_id = 'STRUCTURE:%s:fs:brukere' % cereconf.INSTITUTION_DOMAIN_NAME_LMS register_group('Brukere', brukere_id, auto_node_id) fellesrom_id = 'STRUCTURE:%s:fs:fellesrom' % \ cereconf.INSTITUTION_DOMAIN_NAME_LMS register_group('Fellesrom', fellesrom_id, auto_node_id) # Registrer statiske EVU-strukturnoder. # Ting blir litt enklere, hvis vi drar med oss institusjonsnummeret evu_node_id = 'STRUCTURE:%s:fs:%s:evu' % ( cereconf.INSTITUTION_DOMAIN_NAME_LMS, cereconf.DEFAULT_INSTITUSJONSNR) register_group('EVU', evu_node_id, auto_node_id) for (suffix, title, allow_room) in (("kursrom", "EVU kursrom", True), ("kursdeltaker", "EVU kursdeltaker", False), ("foreleser", "EVU foreleser", False)): node_id = evu_node_id + ":" + suffix register_group(title, node_id, evu_node_id, allow_room) # od # Populer dicter for "emnekode -> emnenavn", "fakultet -> # [emnekode ...]" og "<evukurs> -> evukursnavn". emne_info = {} fakulteter = [] def finn_emne_info(element, attrs): if element != 'undenhet': return emnekode = attrs['emnekode'].lower() faknr = int(attrs['faknr_kontroll']) emne_info[emnekode] = {'navn': attrs['emnenavn_bokmal'], 'fak': faknr} if faknr not in fakulteter: fakulteter.append(faknr) access_FS.underv_enhet_xml_parser(os.path.join(fs_dir, 'underv_enhet.xml'), finn_emne_info) stprog_info = {} def finn_stprog_info(element, attrs): if element != 'studprog': return stprog = attrs['studieprogramkode'].lower() faknr = int(attrs['faknr_studieansv']) stprog_info[stprog] = {'fak': faknr} if faknr not in fakulteter: fakulteter.append(faknr) access_FS.studieprog_xml_parser(os.path.join(fs_dir, 'studieprog.xml'), finn_stprog_info) evukurs_info = {} def finn_evukurs_info(element, attrs): if element != "evukurs": return name = "%s (%s)" % (attrs.get("etterutdkursnavnkort", ""), ", ".join( filter( None, (attrs.get("etterutdkurskode"), attrs.get("kurstidsangivelsekode"), attrs.get("emnekode"))))) eukk, ktak = (attrs["etterutdkurskode"].lower(), attrs["kurstidsangivelsekode"].lower()) evukurs_info[eukk, ktak] = name # end finn_evukurs_info access_FS.evukurs_xml_parser(os.path.join(fs_dir, 'evu_kursinfo.xml'), finn_evukurs_info) # Henter ut ansatte per fakultet ans_dict = get_ans_fak(fakulteter, acc2names) # Opprett de forskjellige stedkode-korridorene. ou = Stedkode.Stedkode(db) for faknr in fakulteter: fak_sko = "%02d0000" % faknr ou.clear() try: ou.find_stedkode(faknr, 0, 0, institusjon=cereconf.DEFAULT_INSTITUSJONSNR) except Errors.NotFoundError: logger.error("Finner ikke stedkode for fakultet %d", faknr) faknavn = '*Ikke registrert som fakultet i FS*' else: acronym = ou.get_name_with_language( name_variant=const.ou_name_acronym, name_language=const.language_nb, default="") short_name = ou.get_name_with_language( name_variant=const.ou_name_short, name_language=const.language_nb, default="") if acronym: faknavn = acronym else: faknavn = short_name fak_ans_id = "%s:sap:gruppe:%s:%s:ansatte" % \ (cereconf.INSTITUTION_DOMAIN_NAME_LMS, cereconf.DEFAULT_INSTITUSJONSNR, fak_sko) ans_title = "Ansatte ved %s" % faknavn register_group(ans_title, fak_ans_id, brukere_id, allow_contact=True) ans_memb = ans_dict[int(faknr)] register_members(fak_ans_id, ans_memb, const.entity_account) for id_prefix, parent_id in ((emner_this_sem_id, emnerom_this_sem_id), (emner_next_sem_id, emnerom_next_sem_id)): fak_node_id = id_prefix + \ ":%s:%s" % (cereconf.DEFAULT_INSTITUSJONSNR, fak_sko) register_group(faknavn, fak_node_id, parent_id, allow_room=True) brukere_sted_id = brukere_id + \ ":%s:%s" % (cereconf.DEFAULT_INSTITUSJONSNR, fak_sko) register_group(faknavn, brukere_sted_id, brukere_id) brukere_studenter_id = brukere_sted_id + ':student' register_group('Studenter ved %s' % faknavn, brukere_studenter_id, brukere_sted_id) fellesrom_sted_id = fellesrom_id + ":%s:%s" % ( cereconf.DEFAULT_INSTITUSJONSNR, fak_sko) register_group(faknavn, fellesrom_sted_id, fellesrom_id, allow_room=True) register_spread_groups(emne_info, stprog_info, evukurs_info) output_group_xml() for room, data in new_rooms.iteritems(): fxml.room_to_XML(data['CFid'], fronter_lib.Fronter.STATUS_ADD, data) for node, data in new_acl.iteritems(): fxml.acl_to_XML(node, fronter_lib.Fronter.STATUS_ADD, data) for gname, members in new_groupmembers.iteritems(): person_members = members.get(int(const.entity_account), ()) group_members = members.get(int(const.entity_group), ()) fxml.personmembers_to_XML(gname, fronter_lib.Fronter.STATUS_ADD, person_members) if group_members: # IVR 2008-01-29 Just to be sure... assert gname.split(':')[-1] == "student" fxml.groupmembers_to_XML(gname, fronter_lib.Fronter.STATUS_ADD, group_members) fxml.end()