From 65b5d4fcc8dacc8e8cacfebafa7f9bb79d61d808 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Mon, 29 Sep 2014 18:17:38 +0200 Subject: [PATCH] [TASK] Make PersonMapper instance a module based singleton. --- frontend/models.py | 5 ++--- frontend/views.py | 22 +++++++++------------- service/Citavi.py | 10 ++++++---- service/Mapper.py | 1 + 4 files changed, 18 insertions(+), 20 deletions(-) diff --git a/frontend/models.py b/frontend/models.py index 09174b5..cf44497 100644 --- a/frontend/models.py +++ b/frontend/models.py @@ -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"" + from service.Mapper import person_mapper + return u"" class CitaviProjectIdentity(models.Model): diff --git a/frontend/views.py b/frontend/views.py index bf4b53d..4d741ec 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -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) diff --git a/service/Citavi.py b/service/Citavi.py index 27fcac1..00ba9bf 100644 --- a/service/Citavi.py +++ b/service/Citavi.py @@ -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() diff --git a/service/Mapper.py b/service/Mapper.py index e12f230..d293262 100644 --- a/service/Mapper.py +++ b/service/Mapper.py @@ -79,3 +79,4 @@ class PersonMapper(): return unicode(citavi_person.ID) + u" " + unicode(citavi_person.FirstName) + u" " + unicode(citavi_person.LastName) +person_mapper = PersonMapper() \ No newline at end of file