From 8f87fa05f9daa14fa815ca28a8e3ad62e85b212a Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Mon, 6 Oct 2014 13:57:41 +0200 Subject: [PATCH] [TASK] Make skip button in person map wizard work as expected. --- frontend/templates/project/map-person.html | 1 + frontend/views.py | 44 ++++++++++++++++++---- service/Citavi.py | 5 +-- service/Mapper.py | 6 +-- 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/frontend/templates/project/map-person.html b/frontend/templates/project/map-person.html index 0d52185..b72189e 100644 --- a/frontend/templates/project/map-person.html +++ b/frontend/templates/project/map-person.html @@ -4,6 +4,7 @@ {% endblock %} {% block navbar-header %}
  • Back to projects
  • +
  • Back to person list
  • {% endblock %} {% block content %}

    Current subject:

    diff --git a/frontend/views.py b/frontend/views.py index f109e05..db4eea4 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -190,7 +190,6 @@ class ProjectPersonView(ProtectedFormView, SingleObjectMixin): def get_context_data(self, **kwargs): project = self.object - kwargs[u'unmapped_persons'] = person_mapper.get_unmapped_identities(project) kwargs[u'mapped_persons'] = person_mapper.get_mapped_identities(project) return super(ProjectPersonView, self).get_context_data(**kwargs) @@ -217,18 +216,44 @@ class ProjectMapPersonView(ProtectedFormView, SingleObjectMixin): success_url = u'/project' + _unmapped_persons = None + + def _refresh_unmapped(self): + """ Refresh data about unmapped persons. """ + self._unmapped_persons = person_mapper.get_unmapped_identities(self.object) + + def _is_unmapped(self, person_uuid): + """ Returns True if the given person is not mapped yet. """ + if not self._unmapped_persons: + self._refresh_unmapped() + return person_uuid in self._unmapped_persons + def get_context_data(self, **kwargs): project = self.object kwargs[u'person'] = person_mapper.get_person_by_uuid(project, self.person_uuid) return super(ProjectMapPersonView, self).get_context_data(**kwargs) def get_success_url(self): - unmapped_persons = person_mapper.get_unmapped_identities(self.object) - project_id = self.object.id - person = unmapped_persons.itervalues().next() + """ TODO: Make this work! - Update success uri to next unmapped person. """ + last_uuid = unicode(self.person_uuid) + self._refresh_unmapped() + unmapped = self._unmapped_persons.items() + first_person_uuid = unmapped[0][0] + next_uuid = first_person_uuid + unmapped.reverse() + + while True: + try: + current_person = unmapped.pop() + if current_person[1].ID == last_uuid: + next_uuid = unmapped.pop()[0] + break + except IndexError: + break + kwargs = { - u"project_id": project_id, - u"person_uuid": person.ID + u"project_id": self.object.id, + u"person_uuid": next_uuid } return reverse('frontend-project-map-person', kwargs=kwargs) @@ -236,13 +261,18 @@ class ProjectMapPersonView(ProtectedFormView, SingleObjectMixin): self.project_id = kwargs[u'project_id'] self.person_uuid = kwargs[u'person_uuid'] self.object = Project.objects.get(pk=self.project_id) + if self._is_unmapped(self.person_uuid) == False: + raise Exception("Sorry, this person was already mapped. Try deleting the existing mapping and move on. TODO: Make this more beautiful.") + self._refresh_unmapped() return super(ProjectMapPersonView, self).get(request, *args, **kwargs) def post(self, request, *args, **kwargs): self.project_id = kwargs[u'project_id'] self.person_uuid = kwargs[u'person_uuid'] self.object = Project.objects.get(pk=self.project_id) - # self.success_url = self.success_url + self.project_id + '/update' + if self._is_unmapped(self.person_uuid) == False: + raise Exception("Sorry, this person was already mapped. Try deleting the existing mapping and move on. TODO: Make this more beautiful.") + self._refresh_unmapped() return super(ProjectMapPersonView, self).post(request, *args, **kwargs) def form_valid(self, form, *args, **kwargs): diff --git a/service/Citavi.py b/service/Citavi.py index 00ba9bf..ac73b42 100644 --- a/service/Citavi.py +++ b/service/Citavi.py @@ -1,10 +1,9 @@ # -*- coding: utf-8 -*- -from sqlalchemy import create_engine, MetaData +from sqlalchemy import create_engine, MetaData, asc, desc from sqlalchemy.ext.automap import automap_base from sqlalchemy.orm import Session, scoped_session, sessionmaker - class Project(): """ A helper class representing a Citavi project. """ def __init__(self, sqlite_file): @@ -26,7 +25,7 @@ class Project(): """ Returns all persons from the Citavi project. """ try: person_class = self._sa_sqlite_autobase.classes.Person - citavi_persons = self._sa_sqlite_session.query(person_class).all() + citavi_persons = self._sa_sqlite_session.query(person_class).order_by(asc('ID')).all() return citavi_persons except Exception, e: print e diff --git a/service/Mapper.py b/service/Mapper.py index d6bbcb6..82a16b6 100644 --- a/service/Mapper.py +++ b/service/Mapper.py @@ -2,7 +2,7 @@ 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. """ @@ -25,7 +25,7 @@ class PersonMapper(): 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 = {} + citavi_uuid_dict = OrderedDict() for person in citavi_persons: citavi_uuid_dict[person.ID] = person if len(mapped_persons) == 0: @@ -38,7 +38,7 @@ class PersonMapper(): 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 = {} + mapped_uuid_dict = OrderedDict() for person in mapped_persons: mapped_uuid_dict[person.citavi_uuid] = person return mapped_uuid_dict