citavi_mapper/service/Mapper.py

79 lines
3.8 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from Citavi import ProjectManager
from Django import PersonIdentityManager
from collections import OrderedDict
class PersonMapper():
""" This little guy will help you get through your day and map citavi persons against identities. """
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_citavi_persons_from_project(self, project):
""" Returns all citavi persons from a project. """
return self._citavi_project_manager.get_persons_from_project(project.id)
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 = OrderedDict()
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 = OrderedDict()
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 map_identity_to_existing(self, global_identity, project, uuid, preferred):
""" Maps a person person to an existing global identity. """
return self._person_identity_manager.add_identity_to_global_identity(global_identity, project, uuid, preferred)
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)
2014-09-29 15:01:35 +02:00
def get_representation_for_global_identity(self, global_identity):
""" Returns a unicode representation for a global identiy. """
2014-09-29 15:01:35 +02:00
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 self._representation_for_citavi_person(citavi_person)
2014-09-29 15:01:35 +02:00
else:
return u'Not implemented yet!'
def get_representation_for_citavi_identity(self, citavi_identity):
""" Returns a unicode representation for a citavi project identiy. """
citavi_person = self._citavi_project_manager.get_person_by_uuid(citavi_identity.project_id, citavi_identity.citavi_uuid)
return self._representation_for_citavi_person(citavi_person)
def _representation_for_citavi_person(self, citavi_person):
p = citavi_person
return u"<" + unicode(p.FirstName) + u" " + unicode(p.LastName) + u">"
# Create a PersonMapper instance for public use, so ressources are not wasted.
person_mapper = PersonMapper()