Skip to content

Commit

Permalink
Merge pull request #38 from IT-CEREBRUM/CRB-3975-uit-greg-mapper
Browse files Browse the repository at this point in the history
CRB-3975: Greg-refactoring for UiT
  • Loading branch information
fredrikhl authored and GitHub Enterprise committed Jan 25, 2023
2 parents bade777 + dc426b2 commit cf1ed1f
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 129 deletions.
4 changes: 2 additions & 2 deletions Cerebrum/modules/greg/datasource.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright 2021 University of Oslo, Norway
# Copyright 2021-2023 University of Oslo, Norway
#
# This file is part of Cerebrum.
#
Expand Down Expand Up @@ -183,7 +183,7 @@ def _parse_person_id(d):
return {
'id': normalize_id(d['id']),
'person': normalize_id(d['person']),
# 'source': normalize_text(d['source']),
'source': normalize_text(d['source'], allow_empty=True),
'type': normalize_text(d['type']),
'value': normalize_text(d['value'], allow_empty=True),
'verified': normalize_text(d['verified'], allow_empty=True),
Expand Down
31 changes: 20 additions & 11 deletions Cerebrum/modules/greg/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ def get_import_class(cereconf=cereconf):
return cls


def _is_deceased(person_obj, _today=None):
""" helper - check if a Person object is deceased. """
today = _today or datetime.date.today()
return (
person_obj
and person_obj.deceased_date
and date_compat.get_date(person_obj.deceased_date) < today)


class GregImporter(object):

REQUIRED_PERSON_ID = (
Expand Down Expand Up @@ -112,10 +121,11 @@ def get_person(self, greg_person):
raise ValueError('invalid person: no external_ids')
return search(self.db, criterias, required=False)

def get_ou(self, greg_orgunit):
def get_ou(self, orgunit_ids):
""" Find matching ou from a Greg orgunit dict. """
search = OuMatcher()
criterias = tuple(self.mapper.get_orgunit_ids(greg_orgunit))
criterias = tuple((id_type, id_value)
for id_type, id_value in orgunit_ids)
if not criterias:
raise ValueError('invalid orgunit: no external_ids')
return search(self.db, criterias, required=True)
Expand Down Expand Up @@ -143,11 +153,7 @@ def handle_object(self, greg_person, person_obj):
"""
greg_id = greg_person['id']

is_deceased = (
person_obj
and person_obj.deceased_date
and (date_compat.get_date(person_obj.deceased_date)
< datetime.date.today()))
is_deceased = _is_deceased(person_obj)
if is_deceased:
logger.warning('person_id=%s is marked as deceased',
person_obj.entity_id)
Expand Down Expand Up @@ -215,20 +221,23 @@ def remove(self, greg_person, person_obj):
self._sync_affs(person_obj, ())
self._sync_consents(person_obj, ())

def update(self, greg_person, person_obj):
def update(self, greg_person, person_obj, _today=None):
""" Update the Person object using employee_data. """
if not greg_person:
raise ValueError('update() called without greg person data!')
if person_obj is None or not person_obj.entity_id:
raise ValueError('update() called without cerebrum person!')

today = _today or datetime.date.today()

self._sync_name(person_obj, self.mapper.get_names(greg_person))
self._sync_ids(person_obj, self.mapper.get_person_ids(greg_person))
self._sync_cinfo(person_obj, self.mapper.get_contact_info(greg_person))
affs = (
(aff_status, self.get_ou(ou_data).entity_id)
for aff_status, ou_data
in self.mapper.get_affiliations(greg_person)
(aff_status, self.get_ou(org_ids).entity_id)
for aff_status, org_ids, start_date, end_date
in self.mapper.get_affiliations(greg_person,
filter_active_at=today)
)
self._sync_affs(person_obj, affs)
self._sync_consents(person_obj, self.mapper.get_consents(greg_person))
Loading

0 comments on commit cf1ed1f

Please sign in to comment.