[TASK] Make skip button in person map wizard work as expected.

This commit is contained in:
Jan Philipp Timme 2014-10-06 13:57:41 +02:00
parent ed20650289
commit 8f87fa05f9
4 changed files with 43 additions and 13 deletions

View File

@ -4,6 +4,7 @@
{% endblock %}
{% block navbar-header %}
<li><a href="{% url 'frontend-projects' %}">Back to projects</a></li>
<li><a href="{% url 'frontend-project-view-person' project.id %}">Back to person list</a></li>
{% endblock %}
{% block content %}
<h3>Current subject:</h3>

View File

@ -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):

View File

@ -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

View File

@ -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