citavi_mapper/service/Django.py

36 lines
1.7 KiB
Python

# -*- coding: utf-8 -*-
from frontend.models import PersonGlobalIdentity, CitaviProjectIdentity
class PersonIdentityManager():
""" Class wrapping the django model layer to manage identities. """
def create_identity(self, project, uuid):
""" Creates a new identity and connects it to the given citavi project uuid. """
pgi = PersonGlobalIdentity(type='citavi')
pgi.save()
cpi = CitaviProjectIdentity(global_identity=pgi, project=project, citavi_uuid=uuid, preferred=True)
cpi.save()
return cpi
def add_identity_to_global_identity(self, global_identity, project, uuid, preferred):
""" Maps given citavi project identity to existing global identity. """
# First, fetch all involved CitaviProjectIdentities and set them to preferred=False
CitaviProjectIdentity.objects.filter(global_identity=global_identity).update(preferred=False)
# Then add the new primary ProjectIdentity.
cpi = CitaviProjectIdentity(global_identity=global_identity, project=project, citavi_uuid=uuid, preferred=preferred)
cpi.save()
return cpi
def get_global_identities(self):
""" Returns all global identities. """
return PersonGlobalIdentity.objects.all()
def get_mapped_identities_for_project(self, project_instance):
""" Returns all existing (mapped) identies for a given project. """
return CitaviProjectIdentity.objects.filter(project=project_instance).all()
def get_citavi_identity_by_global_identity(self, global_identity):
""" Returns the preferred citavi identity based on their global identity. """
return CitaviProjectIdentity.objects.filter(global_identity=global_identity, preferred=True).all()[0]