[TASK] Make PersonMapper instance a module based singleton.

This commit is contained in:
Jan Philipp Timme 2014-09-29 18:17:38 +02:00
parent fe3d26f905
commit 65b5d4fcc8
4 changed files with 18 additions and 20 deletions

View File

@ -23,9 +23,8 @@ class PersonGlobalIdentity(models.Model):
# TODO: Extend this for further stuff - maybe vivo external url or something?
def __unicode__(self):
from service.Mapper import PersonMapper
pm = PersonMapper()
return u"<PersonGlobalIdentity repr='" + pm.get_representation_for_global_identity(self) + u"', ID=" + unicode(self.id) + u", type=" + unicode(self.type) + u">"
from service.Mapper import person_mapper
return u"<PersonGlobalIdentity repr='" + person_mapper.get_representation_for_global_identity(self) + u"', ID=" + unicode(self.id) + u", type=" + unicode(self.type) + u">"
class CitaviProjectIdentity(models.Model):

View File

@ -19,6 +19,9 @@ from frontend.models import Project
FRONTEND_PAGE_NAME = u'Citavi Mapper'
from service.Mapper import person_mapper
# Login wrapper functions
def login_wrap(*args, **kwargs):
""" Wrapper function for login page. """
@ -187,11 +190,8 @@ class ProjectPersonView(ProtectedFormView, SingleObjectMixin):
def get_context_data(self, **kwargs):
project = self.object
from service import Mapper
pm = Mapper.PersonMapper()
kwargs[u'unmapped_persons'] = pm.get_unmapped_identities(project)
kwargs[u'mapped_persons'] = pm.get_mapped_identities_with_representation(project)
kwargs[u'mapped_persons'] = pm.get_mapped_identities_with_representation(project)
kwargs[u'unmapped_persons'] = person_mapper.get_unmapped_identities(project)
kwargs[u'mapped_persons'] = person_mapper.get_mapped_identities_with_representation(project)
return super(ProjectPersonView, self).get_context_data(**kwargs)
def get(self, request, *args, **kwargs):
@ -218,9 +218,7 @@ class ProjectMapPersonView(ProtectedFormView, SingleObjectMixin):
def get_context_data(self, **kwargs):
project = self.object
from service import Mapper
pm = Mapper.PersonMapper()
kwargs[u'person'] = pm.get_person_by_uuid(project, self.person_uuid)
kwargs[u'person'] = person_mapper.get_person_by_uuid(project, self.person_uuid)
return super(ProjectMapPersonView, self).get_context_data(**kwargs)
def get(self, request, *args, **kwargs):
@ -237,9 +235,7 @@ class ProjectMapPersonView(ProtectedFormView, SingleObjectMixin):
return super(ProjectMapPersonView, self).post(request, *args, **kwargs)
def form_valid(self, form, *args, **kwargs):
from service import Mapper
pm = Mapper.PersonMapper()
person = pm.get_person_by_uuid(self.object, self.person_uuid)
person = person_mapper.get_person_by_uuid(self.object, self.person_uuid)
if form.data.get(u'skip', False):
""" Nothing to do here, just go on ... """
@ -247,10 +243,10 @@ class ProjectMapPersonView(ProtectedFormView, SingleObjectMixin):
elif form.data.get(u'save-continue', False):
# TODO: do mapping according to parameters, override success_url to point to next person!
if form.cleaned_data[u'action'] == u'new':
pm.create_new_identity(self.object, person)
person_mapper.create_new_identity(self.object, person)
elif form.cleaned_data[u'action'] == u'existing':
# TODO preferred = FALSE is not desired.
print form.cleaned_data
pm.map_identity_to_existing(form.cleaned_data[u'global_identity'], self.object, person.ID, form.cleaned_data[u'preferred_identity'])
person_mapper.map_identity_to_existing(form.cleaned_data[u'global_identity'], self.object, person.ID, form.cleaned_data[u'preferred_identity'])
return super(ProjectMapPersonView, self).form_valid(form)

View File

@ -2,7 +2,7 @@
from sqlalchemy import create_engine, MetaData
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy.orm import Session, scoped_session, sessionmaker
class Project():
@ -28,7 +28,8 @@ class Project():
person_class = self._sa_sqlite_autobase.classes.Person
citavi_persons = self._sa_sqlite_session.query(person_class).all()
return citavi_persons
except:
except Exception, e:
print e
self._is_error = True
# TODO: better error handling!
print str(u"An error occured within a get_persons call!")
@ -40,7 +41,8 @@ class Project():
person_class = self._sa_sqlite_autobase.classes.Person
citavi_person = self._sa_sqlite_session.query(person_class).filter(u'ID=\'' + unicode(uuid) + u'\'').all()
return citavi_person[0]
except:
except Exception, e:
print e
self._is_error = True
# TODO: better error handling!
print str(u"An error occured within a get_person_by_uuid call!")
@ -60,7 +62,7 @@ class Project():
""" Internal method to open a citavi project file. """
try:
self._sa_sqlite_engine = create_engine(u'sqlite+pysqlite:///' + self.sqlite_file)
self._sa_sqlite_session = Session(self._sa_sqlite_engine)
self._sa_sqlite_session = scoped_session(sessionmaker(bind=self._sa_sqlite_engine)) # Caution: SQLAlchemy Sessions are per thread!
self._sa_sqlite_meta = MetaData(bind=self._sa_sqlite_engine)
self._sa_sqlite_meta.reflect()
self._sa_sqlite_autobase = automap_base()

View File

@ -79,3 +79,4 @@ class PersonMapper():
return unicode(citavi_person.ID) + u" " + unicode(citavi_person.FirstName) + u" " + unicode(citavi_person.LastName)
person_mapper = PersonMapper()