citavi_mapper/service/Mapper.py

57 lines
2.7 KiB
Python

# -*- 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)
def get_representation_for_global_identity(self, global_identity):
""" Returns a unicode string representation for a global identiy. """
if global_identity.type == 'citavi':
citavi_identity = self._person_identity_manager.get_citavi_identity_by_global_identity(global_identity)
citavi_person = self._citavi_project_manager.get_person_by_uuid(citavi_identity.project_id, citavi_identity.citavi_uuid)
return unicode(citavi_person.ID) + u" " + unicode(citavi_person.FirstName) + u" " + unicode(citavi_person.LastName)
else:
return unicode(global_identity)