2014-09-19 11:52:56 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from Citavi import ProjectManager
|
|
|
|
from Django import PersonIdentityManager
|
|
|
|
|
2014-09-29 13:40:07 +02:00
|
|
|
|
2014-09-19 11:52:56 +02:00
|
|
|
class PersonMapper():
|
2014-09-30 14:20:30 +02:00
|
|
|
""" This little guy will help you get through your day and map citavi persons against identities. """
|
2014-09-19 11:52:56 +02:00
|
|
|
def __init__(self):
|
2014-09-29 13:40:07 +02:00
|
|
|
""" Initializes ProjectManager and PersonIdentityManager. """
|
2014-09-19 11:52:56 +02:00
|
|
|
self._citavi_project_manager = ProjectManager()
|
|
|
|
self._person_identity_manager = PersonIdentityManager()
|
|
|
|
|
|
|
|
def __del__(self):
|
2014-09-29 13:40:07 +02:00
|
|
|
""" Destroys ProjectManager and PersonIdentityManager. """
|
2014-09-19 11:52:56 +02:00
|
|
|
del self._citavi_project_manager
|
|
|
|
del self._person_identity_manager
|
|
|
|
|
2014-09-29 16:51:50 +02:00
|
|
|
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)
|
|
|
|
|
2014-09-19 11:52:56 +02:00
|
|
|
def get_unmapped_identities(self, project):
|
2014-09-29 13:40:07 +02:00
|
|
|
""" Returns a uuid->person dict for all unmapped persons within a project. """
|
2014-09-19 11:52:56 +02:00
|
|
|
citavi_persons = self._citavi_project_manager.get_persons_from_project(project.id)
|
|
|
|
mapped_persons = self._person_identity_manager.get_mapped_identities_for_project(project)
|
2014-09-19 16:22:11 +02:00
|
|
|
# Prepare citavi_persons into a uuid->person dict, then eliminate mapped ones. This is ugly and a little slow.
|
|
|
|
citavi_uuid_dict = {}
|
2014-09-19 11:52:56 +02:00
|
|
|
for person in citavi_persons:
|
2014-09-19 16:22:11 +02:00
|
|
|
citavi_uuid_dict[person.ID] = person
|
2014-09-25 17:36:42 +02:00
|
|
|
if len(mapped_persons) == 0:
|
|
|
|
return citavi_uuid_dict
|
2014-09-19 11:52:56 +02:00
|
|
|
for person in mapped_persons:
|
2014-09-19 16:22:11 +02:00
|
|
|
if person.citavi_uuid in citavi_uuid_dict:
|
|
|
|
del citavi_uuid_dict[person.citavi_uuid]
|
|
|
|
return citavi_uuid_dict
|
|
|
|
|
2014-09-29 13:40:07 +02:00
|
|
|
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
|
|
|
|
|
2014-09-19 16:22:11 +02:00
|
|
|
def create_new_identity(self, project, person):
|
2014-09-29 13:40:07 +02:00
|
|
|
""" Creates a new identity for a given person. """
|
|
|
|
return self._person_identity_manager.create_identity(project, person.ID)
|
2014-09-19 11:52:56 +02:00
|
|
|
|
2014-09-29 16:51:50 +02:00
|
|
|
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)
|
|
|
|
|
2014-09-29 13:40:07 +02:00
|
|
|
def get_person_by_uuid(self, project, uuid):
|
|
|
|
""" Returns a person from a citavi project by uuid. """
|
2014-09-25 17:36:42 +02:00
|
|
|
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):
|
2014-09-29 16:51:50 +02:00
|
|
|
""" 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)
|
2014-10-01 11:26:11 +02:00
|
|
|
return self._representation_for_citavi_person(citavi_person)
|
2014-09-29 15:01:35 +02:00
|
|
|
else:
|
2014-09-29 16:51:50 +02:00
|
|
|
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)
|
2014-10-01 11:26:11 +02:00
|
|
|
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">"
|
2014-09-29 16:51:50 +02:00
|
|
|
|
|
|
|
|
2014-09-30 14:20:30 +02:00
|
|
|
# Create a PersonMapper instance for public use, so ressources are not wasted.
|
|
|
|
person_mapper = PersonMapper()
|