From b0a04b951e4b4e49194494bd68c236694288b56c Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Thu, 25 Sep 2014 17:36:42 +0200 Subject: [PATCH] [TASK] Expanding the structure, getting the Mapper mechanisms ready and in place. --- citavi_mapper/urls.py | 8 +- frontend/forms.py | 39 ++++++-- frontend/models.py | 2 +- frontend/templates/project/map-person.html | 27 ++++++ .../{project.html => project/update.html} | 0 frontend/templates/project/view-person.html | 97 +++++++++++++++++++ frontend/templates/projects.html | 5 +- frontend/views.py | 82 +++++++++++++--- service/Django.py | 4 +- service/Mapper.py | 13 ++- 10 files changed, 244 insertions(+), 33 deletions(-) create mode 100644 frontend/templates/project/map-person.html rename frontend/templates/{project.html => project/update.html} (100%) create mode 100644 frontend/templates/project/view-person.html diff --git a/citavi_mapper/urls.py b/citavi_mapper/urls.py index 2f221ed..7860a9f 100644 --- a/citavi_mapper/urls.py +++ b/citavi_mapper/urls.py @@ -6,12 +6,14 @@ from django.contrib import admin admin.autodiscover() from frontend.views import login_wrap, logout_wrap -from frontend.views import IndexView, ProjectOverView, UpdateProjectView +from frontend.views import IndexView, ProjectView, ProjectUpdateView, ProjectPersonView, ProjectMapPersonView urlpatterns = patterns('', url(r'^$', IndexView.as_view(), name='frontend-index'), - url(r'^projects/$', ProjectOverView.as_view(), name='frontend-projects'), - url(r'^projects/(?P\d+)/update$', UpdateProjectView.as_view(), name='frontend-project-update'), + url(r'^project/$', ProjectView.as_view(), name='frontend-projects'), + url(r'^project/(?P\d+)/update$', ProjectUpdateView.as_view(), name='frontend-project-update'), + url(r'^project/(?P\d+)/map/person$', ProjectPersonView.as_view(), name='frontend-project-view-person'), + url(r'^project/(?P\d+)/map/person/(?P[\w\d-]+)$', ProjectMapPersonView.as_view(), name='frontend-project-map-person'), url(r'^login/$', login_wrap, name='frontend-login'), url(r'^logout/$', logout_wrap, name='frontend-logout'), url(r'^admin/', include(admin.site.urls)), diff --git a/frontend/forms.py b/frontend/forms.py index e78680b..7bdcab4 100644 --- a/frontend/forms.py +++ b/frontend/forms.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- from django import forms -from frontend.models import Project +from frontend.models import Project, PersonGlobalIdentity from crispy_forms.helper import FormHelper from crispy_forms.layout import Submit, Layout @@ -16,18 +16,18 @@ class NewProjectForm(forms.ModelForm): self.helper.layout = Layout( 'name', 'description', - Submit('send', 'Create', css_class = 'btn-default') + Submit('send', 'Create', css_class='btn-default') ) name = forms.CharField( - label = "Name", - max_length = 255, - required = True, + label="Name", + max_length=255, + required=True, ) description = forms.CharField( - label = "Description", - required = True, + label="Description", + required=True, ) class Meta: @@ -44,7 +44,26 @@ class FileUploadForm(forms.Form): self.helper.form_method = 'post' self.helper.layout = Layout( 'file', - Submit('send', 'Upload', css_class = 'btn-default') + Submit('send', 'Upload', css_class='btn-default') ) - - file = forms.FileField() \ No newline at end of file + + file = forms.FileField() + + +class PersonMapForm(forms.Form): + def __init__(self, *args, **kwargs): + super(PersonMapForm, self).__init__(*args, **kwargs) + self.helper = FormHelper() + self.helper.form_class = 'form-horizontal' + self.helper.label_class = 'col-lg-2' + self.helper.field_class = 'col-lg-4' + self.helper.form_method = 'post' + self.helper.layout = Layout( + 'action', + 'global_identity', + Submit('continue', 'Continue', css_class='btn-default'), + Submit('save-continue', 'Save and continue', css_class='btn-default'), + ) + + action = forms.ChoiceField(choices=[('new', 'Create new global Identity'), ('existing', 'Map to existing identity')], widget=forms.RadioSelect()) + global_identity = forms.ModelChoiceField(queryset=PersonGlobalIdentity.objects.all()) \ No newline at end of file diff --git a/frontend/models.py b/frontend/models.py index abc2734..1014c15 100644 --- a/frontend/models.py +++ b/frontend/models.py @@ -26,7 +26,7 @@ class PersonGlobalIdentity(models.Model): return repr -class CitaviProjectIdentity(models.Model): +class CitaviProjectIdentity(models.Model): global_identity = models.ForeignKey(PersonGlobalIdentity, blank=True, null=True) project = models.ForeignKey(Project, blank=False, null=False) citavi_uuid = models.CharField(max_length=255, blank=False, null=False) diff --git a/frontend/templates/project/map-person.html b/frontend/templates/project/map-person.html new file mode 100644 index 0000000..0d52185 --- /dev/null +++ b/frontend/templates/project/map-person.html @@ -0,0 +1,27 @@ +{% extends "layout/base.html" %} +{% block head%} + {% load crispy_forms_tags %} +{% endblock %} +{% block navbar-header %} +
  • Back to projects
  • +{% endblock %} +{% block content %} +

    Current subject:

    + {% if not person %} +

    Oops, something went wrong. Perhaps a wrong url?

    + {% else %} +
    + ID: {{person.ID}}
    + Prefix: {{person.Prefix}}
    + Title: {{person.Title}}
    + FirstName: {{person.FirstName}}
    + MiddleName: {{person.MiddleName}}
    + LastName: {{person.LastName}}
    + Suffix: {{person.Suffix}}
    + Sex: {{person.Sex}}
    + Abv.: {{person.Abbreviation}}
    +
    + {% endif %} +

    Wizard

    +

    {% crispy form %}

    +{% endblock %} \ No newline at end of file diff --git a/frontend/templates/project.html b/frontend/templates/project/update.html similarity index 100% rename from frontend/templates/project.html rename to frontend/templates/project/update.html diff --git a/frontend/templates/project/view-person.html b/frontend/templates/project/view-person.html new file mode 100644 index 0000000..5c39c39 --- /dev/null +++ b/frontend/templates/project/view-person.html @@ -0,0 +1,97 @@ +{% extends "layout/base.html" %} +{% block head%} + {% load crispy_forms_tags %} +{% endblock %} +{% block navbar-header %} +
  • Back to projects
  • +{% endblock %} +{% block content %} +

    Name: {{project.name}} +

    Description: {{project.description}} +

    Current uploaded database filename: {{project.associated_filename}}

    + + + +

    List of unmapped citavi persons in project:

    + {% if not unmapped_persons %} +

    No unmapped persons in this project. Congratulations!

    + {% else %} + + + + + + + + + + + + + + + + + {% for person_uuid, person in unmapped_persons.items %} + + + + + + + + + + + + + {% endfor %} + +
    IDPrefixTitleFirst NameMiddle NameLast NameSuffixSexAbv.Features
    {{person.ID}}{{person.Prefix}}{{person.Title}}{{person.FirstName}}{{person.MiddleName}}{{person.LastName}}{{person.Suffix}}{{person.Sex}}{{person.Abbreviation}} + Map +
    + {% endif %} +
    +

    List of mapped citavi persons in project:

    + {% if not mapped_persons %} +

    No mapped persons in this project. Work harder!

    + {% else %} + + + + + + + + + + + + + + + + + {% for person in mapped_persons.items %} + + + + + + + + + + + + + {% endfor %} + +
    IDPrefixTitleFirst NameMiddle NameLast NameSuffixSexAbv.Features
    {{person.ID}}{{person.Prefix}}{{person.Title}}{{person.FirstName}}{{person.MiddleName}}{{person.LastName}}{{person.Suffix}}{{person.Sex}}{{person.Abbreviation}} + Map +
    + {% endif %} +{% endblock %} \ No newline at end of file diff --git a/frontend/templates/projects.html b/frontend/templates/projects.html index d36b961..f0ba006 100644 --- a/frontend/templates/projects.html +++ b/frontend/templates/projects.html @@ -26,7 +26,10 @@ {{project.name}} {{project.description}} {{project.associated_filename}} - Update project + + Update project + Persons + {% endfor %} diff --git a/frontend/views.py b/frontend/views.py index 6ce8877..377cfdb 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -13,7 +13,7 @@ from django.contrib.auth.decorators import login_required from django.utils.decorators import method_decorator from django.contrib.auth.views import login, logout -from frontend.forms import NewProjectForm, FileUploadForm +from frontend.forms import NewProjectForm, FileUploadForm, PersonMapForm from frontend.models import Project @@ -94,36 +94,36 @@ class IndexView(ProtectedTemplateView): template_name = 'index.html' page_title = 'Index' -class ProjectOverView(ProtectedFormView): +class ProjectView(ProtectedFormView): template_name = 'projects.html' page_title = 'Projects' form_class = NewProjectForm - success_url = '/projects' + success_url = '/project' def get_context_data(self, **kwargs): kwargs[u'projects'] = Project.objects.order_by('id') - return super(ProjectOverView, self).get_context_data(**kwargs) + return super(ProjectView, self).get_context_data(**kwargs) def form_valid(self, form): form.save() - return super(ProjectOverView, self).form_valid(form) + return super(ProjectView, self).form_valid(form) -class UpdateProjectView(ProtectedFormView, SingleObjectMixin): - template_name = 'project.html' +class ProjectUpdateView(ProtectedFormView, SingleObjectMixin): + template_name = 'project/update.html' page_title = 'Update project' form_class = FileUploadForm - success_url = '/projects/' + success_url = '/project/' def get(self, request, *args, **kwargs): self.project_id = kwargs[u'project_id'] self.object = Project.objects.get(pk=self.project_id) - return super(UpdateProjectView, self).get(request, *args, **kwargs) + return super(ProjectUpdateView, self).get(request, *args, **kwargs) def post(self, request, *args, **kwargs): self.project_id = kwargs[u'project_id'] self.success_url = self.success_url + self.project_id + '/update' - return super(UpdateProjectView, self).post(request, *args, **kwargs) + return super(ProjectUpdateView, self).post(request, *args, **kwargs) def form_valid(self, form, *args, **kwargs): """ This form_valid handles the file upload. """ @@ -167,5 +167,65 @@ class UpdateProjectView(ProtectedFormView, SingleObjectMixin): # citavi_project = Citavi.Project(sqlite_path) # citavi_project.open() # del citavi_project - return super(UpdateProjectView, self).form_valid(form) + return super(ProjectUpdateView, self).form_valid(form) + + +class ProjectPersonView(ProtectedFormView, SingleObjectMixin): + template_name = 'project/view-person.html' + page_title = 'Person View' + form_class = FileUploadForm + + success_url = '/projects/' + + 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) + return super(ProjectPersonView, self).get_context_data(**kwargs) + + def get(self, request, *args, **kwargs): + self.project_id = kwargs[u'project_id'] + self.object = Project.objects.get(pk=self.project_id) + return super(ProjectPersonView, self).get(request, *args, **kwargs) + + def post(self, request, *args, **kwargs): + self.project_id = kwargs[u'project_id'] + # self.success_url = self.success_url + self.project_id + '/update' + return super(ProjectPersonView, self).post(request, *args, **kwargs) + + def form_valid(self, form, *args, **kwargs): + return super(ProjectPersonView, self).form_valid(form) + + +class ProjectMapPersonView(ProtectedFormView, SingleObjectMixin): + template_name = 'project/map-person.html' + page_title = 'Person Mapping' + form_class = PersonMapForm + + success_url = '/projects/' + + 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) + return super(ProjectMapPersonView, self).get_context_data(**kwargs) + + def get(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) + 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.success_url = self.success_url + self.project_id + '/update' + return super(ProjectMapPersonView, self).post(request, *args, **kwargs) + + def form_valid(self, form, *args, **kwargs): + return super(ProjectMapPersonView, self).form_valid(form) diff --git a/service/Django.py b/service/Django.py index 52c76d0..88c1b47 100644 --- a/service/Django.py +++ b/service/Django.py @@ -20,5 +20,5 @@ class PersonIdentityManager(): def get_global_identities(self): return PersonGlobalIdentity.objects.all() - def get_mapped_identities_for_project(self, project): - return CitaviProjectIdentity.objects.filter(project=project).all() + def get_mapped_identities_for_project(self, project_instance): + return CitaviProjectIdentity.objects.filter(project=project_instance).all() diff --git a/service/Mapper.py b/service/Mapper.py index 91710bc..47e7c2b 100644 --- a/service/Mapper.py +++ b/service/Mapper.py @@ -20,14 +20,14 @@ class PersonMapper(): print "Len citavi persons: " + str(len(citavi_persons)) print "Len mapped persons: " + str(len(mapped_persons)) - if len(mapped_persons) == 0: - return citavi_persons - # Prepare citavi_persons into a uuid->person dict, then eliminate mapped ones. This is ugly and a little slow. citavi_uuid_dict = {} for person in citavi_persons: citavi_uuid_dict[person.ID] = person + if len(mapped_persons) == 0: + return citavi_uuid_dict + print len(citavi_uuid_dict) for person in mapped_persons: @@ -42,11 +42,14 @@ class PersonMapper(): def create_new_identity(self, project, person): self._person_identity_manager.create_identity(project, person.ID) + def get_person_by_uuid(self, project, uuid): # + return self._citavi_project_manager.get_person_by_uuid(project.id, uuid) + def test(): from frontend.models import Project project = Project.objects.get(id=1) p = PersonMapper() unmapped = p.get_unmapped_identities(project) print unmapped -# for person in unmapped: -# p.create_new_identity(project, person) + for person in unmapped: + p.create_new_identity(project, person)