Skip to content
Snippets Groups Projects

Cristin64

Merged Petr.Kalashnikov requested to merge cristin64 into master
2 unresolved threads
Files
7
+ 70
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,20 +115,65 @@ 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:
    • Suggested change
      132 if '_stillinger' in person_data and len(person_data['_stillinger']) > 0:
      132 if '_stillinger' in person_data and person_data['_stillinger']:
Please register or sign in to reply
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 stedskode not 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':
    • Suggested change
      157 all_ansettelser.append({'institusjonsnr':
      158 str(context.config.source_info.institusjonsnr),
      159 'avd': avd,
      160 'undavd': undavd,
      161 'gruppe': gruppe,
      162 'stillingskode': str(
      163 stil_model.stillingskode)[-4:],
      164 'datoFra': stil_model.innehaver[
      165 0].innehaver_startdato,
      166 'datoTil': stil_model.innehaver[
      167 0].innehaver_sluttdato,
      168 'stillingsbetegnelse': stil_model.stillingsnavn
      169 })
      157 all_ansettelser.append(
      158 {
      159 "institusjonsnr": str(
      160 context.config.source_info.institusjonsnr
      161 ),
      162 "avd": avd,
      163 "undavd": undavd,
      164 "gruppe": gruppe,
      165 "stillingskode": str(stil_model.stillingskode)[-4:],
      166 "datoFra": stil_model.innehaver[0].innehaver_startdato,
      167 "datoTil": stil_model.innehaver[0].innehaver_sluttdato,
      168 "stillingsbetegnelse": stil_model.stillingsnavn,
      169 }
      170 )
Please register or sign in to reply
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'],
"fornavn": person_data['fornavn'],
}
values_none_okay = {
"brukernavn": person_data.get('username', None),
@@ -156,50 +202,26 @@ def add_persons(root: ET.Element,
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 +254,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