Skip to content
Snippets Groups Projects

Cristin64

Merged Petr.Kalashnikov requested to merge cristin64 into master
2 unresolved threads
Files
7
+ 72
48
@@ -62,9 +62,10 @@ def add_organization(
root: ET.Element,
orgmap: typing.Dict[int, Orgenhet],
context: CristinMsContext,
) -> None:
) -> typing.List:
"""Add organization to the Element"""
organisasjon = ET.SubElement(root, 'organisasjon')
stedskoder = []
for org in orgmap.values():
parent = get_org_parent(orgmap, org)
if parent is None:
@@ -114,15 +115,62 @@ def add_organization(
for k, v in values_none_okay.items():
if v is not None:
set_text(enhet, k, v)
stedskoder.append(str(avd) + str(undavd) + str(gruppe))
return stedskoder
def add_persons(root: ET.Element,
persons: typing.List[dict],
organizations: typing.Dict[int, Orgenhet],
context: CristinMsContext) -> None:
context: CristinMsContext,
stedskoder: typing.List = []) -> None:
"""Add persons to the Element"""
personer = ET.SubElement(root, 'personer')
for person_data in persons:
all_ansettelser = []
if '_stillinger' in person_data and len(person_data['_stillinger']) > 0:
for stilling in person_data['_stillinger']:
# Verify employment info good enough for export
stil_model = Stilling(**stilling)
if not stilling_validate_fields(stil_model):
logger.warning(
"Employee %s has insufficient employment info in db."
" Skipping employment info.",
person_data["id"])
continue
org_enhet_nr = int(stil_model.organisasjon_id)
if org_enhet_nr not in organizations:
logger.warning("No orgenhet found for stilling %r",
stilling)
continue
org = organizations[org_enhet_nr]
avd, undavd, gruppe = get_stedkode(context, org.id)
if not avd or not undavd or not gruppe:
continue
stedskode = str(avd) + str(undavd) + str(gruppe)
if not stedskode in stedskoder:
continue
# Don't create the ansettelser element until we know we can add at least
# one element to it
all_ansettelser.append({'institusjonsnr': str(
context.config.source_info.institusjonsnr),
'avd': avd,
'undavd': undavd,
'gruppe': gruppe,
'stillingskode': str(
stil_model.stillingskode)[-4:],
'datoFra': stil_model.innehaver[
0].innehaver_startdato,
'datoTil': stil_model.innehaver[
0].innehaver_sluttdato,
'stillingsbetegnelse': stil_model.stillingsnavn
})
if not all_ansettelser:
continue
# Validate and adjust values if possible, otherwise skip person
values = {
"etternavn": person_data['etternavn'],
@@ -155,51 +203,27 @@ def add_persons(root: ET.Element,
for k, v in values_none_okay.items():
if v is not None:
set_text(person, k, v)
if '_stillinger' in person_data and len(person_data['_stillinger']) > 0:
ansettelser_made = False
for stilling in person_data['_stillinger']:
# Verify employment info good enough for export
stil_model = Stilling(**stilling)
if not stilling_validate_fields(stil_model):
logger.warning(
"Employee %s has insufficient employment info in db."
" Skipping employment info.",
person_data["id"])
continue
org_enhet_nr = int(stil_model.organisasjon_id)
if org_enhet_nr not in organizations:
logger.warning("No orgenhet found for stilling %r",
stilling)
continue
org = organizations[org_enhet_nr]
avd, undavd, gruppe = get_stedkode(context, org.id)
if not avd or not undavd or not gruppe:
continue
# Don't create the ansettelser element until we know we can add at least
# one element to it
if not ansettelser_made:
ansettelser = ET.SubElement(person, 'ansettelser')
ansettelser_made = True
# Create the entry
ansettelse = ET.SubElement(ansettelser, 'ansettelse')
set_text(ansettelse, 'institusjonsnr',
str(context.config.source_info.institusjonsnr))
set_text(ansettelse, 'avdnr', avd)
set_text(ansettelse, 'undavdnr', undavd)
set_text(ansettelse, 'gruppenr', gruppe)
# Use last 4 digits of stillingskode. API typically gives 20001234
# but we only want 1234
set_text(ansettelse, 'stillingskode',
str(stil_model.stillingskode)[-4:])
set_text(ansettelse, 'datoFra',
stil_model.innehaver[0].innehaver_startdato)
set_text(ansettelse, 'datoTil',
stil_model.innehaver[0].innehaver_sluttdato)
ansettelser = ET.SubElement(person, 'ansettelser')
for ans_dict in all_ansettelser:
ansettelse = ET.SubElement(ansettelser, 'ansettelse')
set_text(ansettelse, 'institusjonsnr',
ans_dict['institusjonsnr'])
set_text(ansettelse, 'avdnr', ans_dict['avd'])
set_text(ansettelse, 'undavdnr', ans_dict['undavd'])
set_text(ansettelse, 'gruppenr', ans_dict['gruppe'])
# Use last 4 digits of stillingskode. API typically gives 20001234
# but we only want 1234
set_text(ansettelse, 'stillingskode',
ans_dict['stillingskode'])
set_text(ansettelse, 'datoFra',
ans_dict['datoFra'])
set_text(ansettelse, 'datoTil',
ans_dict['datoTil'])
if ans_dict['stillingsbetegnelse']:
set_text(ansettelse, 'stillingsbetegnelse',
stil_model.stillingsnavn)
# set_text(ansettelse, 'stillingsandel', ???)
ans_dict['stillingsbetegnelse'])
# set_text(ansettelse, 'stillingsandel', ???)
def make_root_element() -> ET.Element:
@@ -232,12 +256,12 @@ def generate_tree(
"""Generate the whole tree"""
root = make_root_element()
add_source_info(root, context.config.source_info)
add_organization(root, organizations, context)
stedskoder = add_organization(root, organizations, context)
# Exit if no OUs added
check_org_okay(root)
add_persons(root, persons, organizations, context)
add_persons(root, persons, organizations, context, stedskoder)
return root
Loading