2014-07-26 13:00:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-09-01 15:30:57 +02:00
|
|
|
# File upload related imports
|
|
|
|
import os
|
|
|
|
from django.core.files.storage import default_storage
|
|
|
|
from django.core.files.base import ContentFile
|
|
|
|
from django.conf import settings
|
|
|
|
|
2014-08-29 13:55:43 +02:00
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2014-07-28 15:13:55 +02:00
|
|
|
from django.views.generic import TemplateView, FormView, CreateView, UpdateView
|
2014-08-11 16:01:03 +02:00
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
2014-07-27 23:34:23 +02:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
from django.utils.decorators import method_decorator
|
|
|
|
from django.contrib.auth.views import login, logout
|
2014-07-26 13:00:21 +02:00
|
|
|
|
2014-09-25 17:36:42 +02:00
|
|
|
from frontend.forms import NewProjectForm, FileUploadForm, PersonMapForm
|
2014-08-11 16:01:03 +02:00
|
|
|
|
|
|
|
from frontend.models import Project
|
2014-07-28 15:13:55 +02:00
|
|
|
|
2014-09-29 15:01:35 +02:00
|
|
|
FRONTEND_PAGE_NAME = u'Citavi Mapper'
|
2014-07-27 23:34:23 +02:00
|
|
|
|
2014-09-29 18:17:38 +02:00
|
|
|
from service.Mapper import person_mapper
|
|
|
|
|
|
|
|
|
2014-07-28 15:13:55 +02:00
|
|
|
# Login wrapper functions
|
2014-07-27 23:34:23 +02:00
|
|
|
def login_wrap(*args, **kwargs):
|
2014-09-29 13:40:07 +02:00
|
|
|
""" Wrapper function for login page. """
|
2014-09-29 15:01:35 +02:00
|
|
|
kwargs[u'extra_context'] = {
|
|
|
|
u'page': {
|
|
|
|
u'name': FRONTEND_PAGE_NAME,
|
|
|
|
u'title': 'Login'
|
2014-08-29 13:55:43 +02:00
|
|
|
}
|
2014-07-27 23:34:23 +02:00
|
|
|
}
|
|
|
|
return login(*args, **kwargs)
|
|
|
|
|
|
|
|
def logout_wrap(*args, **kwargs):
|
2014-09-29 13:40:07 +02:00
|
|
|
""" Wrapper function for logout page. """
|
2014-09-29 15:01:35 +02:00
|
|
|
kwargs[u'extra_context'] = {
|
|
|
|
u'page': {
|
|
|
|
u'name': FRONTEND_PAGE_NAME,
|
|
|
|
u'title': 'Logout'
|
2014-09-19 11:05:14 +02:00
|
|
|
}
|
2014-07-27 23:34:23 +02:00
|
|
|
}
|
|
|
|
return logout(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2014-07-28 15:13:55 +02:00
|
|
|
# My base classes for views
|
2014-07-29 14:48:09 +02:00
|
|
|
class MyViewMixin(object):
|
2014-09-29 13:40:07 +02:00
|
|
|
""" Basic view mixin to add global variables to all templates. """
|
2014-09-29 15:01:35 +02:00
|
|
|
template_name = u'base.html'
|
2014-07-27 23:34:23 +02:00
|
|
|
page_name = FRONTEND_PAGE_NAME
|
2014-09-29 15:01:35 +02:00
|
|
|
page_title = u'BASE'
|
2014-09-19 11:05:14 +02:00
|
|
|
|
2014-07-27 23:34:23 +02:00
|
|
|
def get_page_data(self):
|
|
|
|
return {
|
|
|
|
u'name': self.page_name,
|
|
|
|
u'title': self.page_title
|
|
|
|
}
|
2014-09-19 11:05:14 +02:00
|
|
|
|
2014-07-26 13:00:21 +02:00
|
|
|
def get_context_data(self, **kwargs):
|
2014-07-27 23:34:23 +02:00
|
|
|
kwargs[u'page'] = self.get_page_data()
|
2014-07-29 14:48:09 +02:00
|
|
|
return super(MyViewMixin, self).get_context_data(**kwargs)
|
2014-07-28 15:13:55 +02:00
|
|
|
|
2014-07-29 14:48:09 +02:00
|
|
|
class MyTemplateView(MyViewMixin, TemplateView):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class MyFormView(MyViewMixin, FormView):
|
|
|
|
pass
|
2014-09-19 11:05:14 +02:00
|
|
|
|
2014-07-29 14:48:09 +02:00
|
|
|
class MyCreateView(MyViewMixin, CreateView):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class MyUpdateView(MyViewMixin, UpdateView):
|
|
|
|
pass
|
2014-07-28 15:13:55 +02:00
|
|
|
|
2014-07-27 23:34:23 +02:00
|
|
|
|
2014-07-29 14:48:09 +02:00
|
|
|
# Mixin to protect pages
|
2014-07-27 23:34:23 +02:00
|
|
|
class LoggedInMixin(object):
|
2014-09-29 13:40:07 +02:00
|
|
|
""" Mixin to force a valid login for protected pages. """
|
2014-07-27 23:34:23 +02:00
|
|
|
@method_decorator(login_required)
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
return super(LoggedInMixin, self).dispatch(*args, **kwargs)
|
|
|
|
|
2014-07-29 14:48:09 +02:00
|
|
|
|
|
|
|
# My protected view classes
|
2014-07-28 15:13:55 +02:00
|
|
|
class ProtectedTemplateView(LoggedInMixin, MyTemplateView):
|
|
|
|
pass
|
|
|
|
|
|
|
|
class ProtectedFormView(LoggedInMixin, MyFormView):
|
2014-07-27 23:34:23 +02:00
|
|
|
pass
|
|
|
|
|
2014-07-29 14:48:09 +02:00
|
|
|
class ProtectedCreateView(LoggedInMixin, MyCreateView):
|
|
|
|
pass
|
2014-07-28 15:13:55 +02:00
|
|
|
|
2014-07-29 14:48:09 +02:00
|
|
|
class ProtectedUpdateView(LoggedInMixin, MyUpdateView):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
# Actual Views
|
2014-07-28 15:13:55 +02:00
|
|
|
class IndexView(ProtectedTemplateView):
|
2014-09-29 15:01:35 +02:00
|
|
|
template_name = u'index.html'
|
|
|
|
page_title = u'Index'
|
2014-07-27 23:34:23 +02:00
|
|
|
|
2014-09-29 13:40:07 +02:00
|
|
|
|
2014-09-25 17:36:42 +02:00
|
|
|
class ProjectView(ProtectedFormView):
|
2014-09-29 15:01:35 +02:00
|
|
|
template_name = u'projects.html'
|
|
|
|
page_title = u'Projects'
|
2014-08-07 14:47:59 +02:00
|
|
|
form_class = NewProjectForm
|
2014-09-29 15:01:35 +02:00
|
|
|
success_url = u'/project'
|
2014-09-19 11:05:14 +02:00
|
|
|
|
2014-08-11 16:01:03 +02:00
|
|
|
def get_context_data(self, **kwargs):
|
2014-09-29 15:01:35 +02:00
|
|
|
kwargs[u'projects'] = Project.objects.order_by(u'id')
|
2014-09-25 17:36:42 +02:00
|
|
|
return super(ProjectView, self).get_context_data(**kwargs)
|
2014-09-19 11:05:14 +02:00
|
|
|
|
2014-07-28 15:13:55 +02:00
|
|
|
def form_valid(self, form):
|
2014-08-11 16:01:03 +02:00
|
|
|
form.save()
|
2014-09-25 17:36:42 +02:00
|
|
|
return super(ProjectView, self).form_valid(form)
|
2014-09-19 11:05:14 +02:00
|
|
|
|
2014-09-29 13:40:07 +02:00
|
|
|
|
2014-09-25 17:36:42 +02:00
|
|
|
class ProjectUpdateView(ProtectedFormView, SingleObjectMixin):
|
2014-09-29 15:01:35 +02:00
|
|
|
template_name = u'project/update.html'
|
|
|
|
page_title = u'Update project'
|
2014-08-11 16:01:03 +02:00
|
|
|
form_class = FileUploadForm
|
|
|
|
|
2014-09-29 15:01:35 +02:00
|
|
|
success_url = u'/project/'
|
2014-09-19 11:05:14 +02:00
|
|
|
|
2014-08-29 13:55:43 +02:00
|
|
|
def get(self, request, *args, **kwargs):
|
2014-09-15 16:32:23 +02:00
|
|
|
self.project_id = kwargs[u'project_id']
|
|
|
|
self.object = Project.objects.get(pk=self.project_id)
|
2014-09-25 17:36:42 +02:00
|
|
|
return super(ProjectUpdateView, self).get(request, *args, **kwargs)
|
2014-08-29 13:55:43 +02:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
2014-09-15 16:32:23 +02:00
|
|
|
self.project_id = kwargs[u'project_id']
|
2014-09-29 15:01:35 +02:00
|
|
|
self.object = Project.objects.get(pk=self.project_id)
|
|
|
|
self.success_url = self.success_url + self.project_id + u'/update'
|
2014-09-25 17:36:42 +02:00
|
|
|
return super(ProjectUpdateView, self).post(request, *args, **kwargs)
|
2014-09-19 11:05:14 +02:00
|
|
|
|
2014-08-29 13:55:43 +02:00
|
|
|
def form_valid(self, form, *args, **kwargs):
|
2014-09-16 11:34:53 +02:00
|
|
|
""" This form_valid handles the file upload. """
|
2014-09-15 16:32:23 +02:00
|
|
|
original_file = form.files[u'file']
|
2014-09-29 15:01:35 +02:00
|
|
|
original_filename = unicode(original_file)
|
2014-09-16 12:52:11 +02:00
|
|
|
original_contentfile = ContentFile(original_file.read())
|
2014-09-16 11:34:53 +02:00
|
|
|
""" Put file into temporary folder for analysis """
|
2014-09-29 15:01:35 +02:00
|
|
|
target_filename = u'tmp/project_' + unicode(self.project_id) + u'.ctt4'
|
2014-09-16 12:52:11 +02:00
|
|
|
relative_path = default_storage.save(target_filename, original_contentfile)
|
2014-09-16 11:34:53 +02:00
|
|
|
temp_sqlite = os.path.join(settings.MEDIA_ROOT, relative_path)
|
2014-09-19 11:05:14 +02:00
|
|
|
|
2014-09-16 11:34:53 +02:00
|
|
|
""" Test if SQLite is a valid citavi project. """
|
|
|
|
from service import Citavi
|
2014-09-16 13:51:51 +02:00
|
|
|
citavi_project = Citavi.Project(temp_sqlite)
|
2014-09-19 11:05:14 +02:00
|
|
|
citavi_project.open()
|
2014-09-16 11:34:53 +02:00
|
|
|
citavi_project_valid = citavi_project.is_valid()
|
|
|
|
""" Free temporary ressources. """
|
|
|
|
del citavi_project
|
|
|
|
default_storage.delete(temp_sqlite)
|
2014-09-19 11:05:14 +02:00
|
|
|
|
2014-09-16 11:34:53 +02:00
|
|
|
if citavi_project_valid == False:
|
|
|
|
""" TODO: Put up an error message or something. """
|
|
|
|
pass
|
2014-09-16 11:44:06 +02:00
|
|
|
else:
|
2014-09-29 15:01:35 +02:00
|
|
|
target_filename = u'citavi/project_' + unicode(self.project_id) + u'.ctt4'
|
2014-09-16 11:44:06 +02:00
|
|
|
""" Remove eventually pre-existing citavi file. """
|
|
|
|
if default_storage.exists(target_filename):
|
|
|
|
default_storage.delete(target_filename)
|
|
|
|
|
2014-09-16 11:34:53 +02:00
|
|
|
""" Actually store file in citavi folder """
|
2014-09-16 12:52:11 +02:00
|
|
|
relative_path = default_storage.save(target_filename, original_contentfile)
|
2014-09-16 11:34:53 +02:00
|
|
|
sqlite_path = os.path.join(settings.MEDIA_ROOT, relative_path)
|
|
|
|
|
|
|
|
""" Store new original filename in Project """
|
|
|
|
project = Project.objects.get(id=self.project_id)
|
|
|
|
project.associated_filename = original_filename
|
|
|
|
project.save()
|
2014-09-19 11:05:14 +02:00
|
|
|
|
2014-09-16 11:34:53 +02:00
|
|
|
""" Refresh identities from citavi project. """
|
|
|
|
# TODO
|
2014-09-19 11:05:14 +02:00
|
|
|
# citavi_project = Citavi.Project(sqlite_path)
|
|
|
|
# citavi_project.open()
|
|
|
|
# del citavi_project
|
2014-09-25 17:36:42 +02:00
|
|
|
return super(ProjectUpdateView, self).form_valid(form)
|
|
|
|
|
|
|
|
|
|
|
|
class ProjectPersonView(ProtectedFormView, SingleObjectMixin):
|
2014-09-29 15:01:35 +02:00
|
|
|
template_name = u'project/view-person.html'
|
|
|
|
page_title = u'Person List View'
|
2014-09-25 17:36:42 +02:00
|
|
|
form_class = FileUploadForm
|
|
|
|
|
2014-09-29 15:01:35 +02:00
|
|
|
success_url = u'/project/'
|
2014-09-25 17:36:42 +02:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
project = self.object
|
|
|
|
|
2014-09-29 18:17:38 +02:00
|
|
|
kwargs[u'unmapped_persons'] = person_mapper.get_unmapped_identities(project)
|
2014-09-30 14:20:30 +02:00
|
|
|
kwargs[u'mapped_persons'] = person_mapper.get_mapped_identities(project)
|
2014-09-25 17:36:42 +02:00
|
|
|
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):
|
2014-09-29 15:01:35 +02:00
|
|
|
template_name = u'project/map-person.html'
|
|
|
|
page_title = u'Person Mapping'
|
2014-09-25 17:36:42 +02:00
|
|
|
form_class = PersonMapForm
|
|
|
|
|
2014-09-29 15:01:35 +02:00
|
|
|
success_url = u'/project'
|
2014-09-25 17:36:42 +02:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
project = self.object
|
|
|
|
|
2014-09-29 18:17:38 +02:00
|
|
|
kwargs[u'person'] = person_mapper.get_person_by_uuid(project, self.person_uuid)
|
2014-09-25 17:36:42 +02:00
|
|
|
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']
|
2014-09-29 13:40:07 +02:00
|
|
|
self.object = Project.objects.get(pk=self.project_id)
|
2014-09-25 17:36:42 +02:00
|
|
|
# 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):
|
2014-09-29 18:17:38 +02:00
|
|
|
person = person_mapper.get_person_by_uuid(self.object, self.person_uuid)
|
2014-09-29 13:40:07 +02:00
|
|
|
|
2014-09-29 16:51:50 +02:00
|
|
|
if form.data.get(u'skip', False):
|
|
|
|
""" Nothing to do here, just go on ... """
|
2014-09-29 13:40:07 +02:00
|
|
|
pass
|
2014-09-29 16:51:50 +02:00
|
|
|
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':
|
2014-09-29 18:17:38 +02:00
|
|
|
person_mapper.create_new_identity(self.object, person)
|
2014-09-29 16:51:50 +02:00
|
|
|
elif form.cleaned_data[u'action'] == u'existing':
|
|
|
|
# TODO preferred = FALSE is not desired.
|
|
|
|
print form.cleaned_data
|
2014-09-29 18:17:38 +02:00
|
|
|
person_mapper.map_identity_to_existing(form.cleaned_data[u'global_identity'], self.object, person.ID, form.cleaned_data[u'preferred_identity'])
|
2014-09-25 17:36:42 +02:00
|
|
|
return super(ProjectMapPersonView, self).form_valid(form)
|
2014-09-16 11:34:53 +02:00
|
|
|
|