citavi_mapper/service/Mapper.py

49 lines
2.1 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from Citavi import ProjectManager
from Django import PersonIdentityManager
class PersonMapper():
def __init__(self):
""" Initializes ProjectManager and PersonIdentityManager. """
self._citavi_project_manager = ProjectManager()
self._person_identity_manager = PersonIdentityManager()
def __del__(self):
""" Destroys ProjectManager and PersonIdentityManager. """
del self._citavi_project_manager
del self._person_identity_manager
def get_unmapped_identities(self, project):
""" Returns a uuid->person dict for all unmapped persons within a project. """
citavi_persons = self._citavi_project_manager.get_persons_from_project(project.id)
mapped_persons = self._person_identity_manager.get_mapped_identities_for_project(project)
# Prepare citavi_persons into a uuid->person dict, then eliminate mapped ones. This is ugly and a little slow.
citavi_uuid_dict = {}
for person in citavi_persons:
citavi_uuid_dict[person.ID] = person
if len(mapped_persons) == 0:
return citavi_uuid_dict
for person in mapped_persons:
if person.citavi_uuid in citavi_uuid_dict:
del citavi_uuid_dict[person.citavi_uuid]
return citavi_uuid_dict
def get_mapped_identities(self, project):
""" Returns a uuid->person dict for all mapped persons within a project. """
mapped_persons = self._person_identity_manager.get_mapped_identities_for_project(project)
mapped_uuid_dict = {}
for person in mapped_persons:
mapped_uuid_dict[person.citavi_uuid] = person
return mapped_uuid_dict
def create_new_identity(self, project, person):
""" Creates a new identity for a given person. """
return self._person_identity_manager.create_identity(project, person.ID)
def get_person_by_uuid(self, project, uuid):
""" Returns a person from a citavi project by uuid. """
return self._citavi_project_manager.get_person_by_uuid(project.id, uuid)