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-08-11 16:01:03 +02:00
|
|
|
from frontend.forms import NewProjectForm, FileUploadForm
|
|
|
|
|
|
|
|
from frontend.models import Project
|
2014-07-28 15:13:55 +02:00
|
|
|
|
2014-07-27 23:34:23 +02:00
|
|
|
FRONTEND_PAGE_NAME = 'Citavi 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):
|
|
|
|
kwargs['extra_context'] = {
|
|
|
|
'page': {
|
|
|
|
'name': FRONTEND_PAGE_NAME,
|
|
|
|
'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):
|
|
|
|
kwargs['extra_context'] = {
|
|
|
|
'page': {
|
|
|
|
'name': FRONTEND_PAGE_NAME,
|
|
|
|
'title': 'Logout'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-07-27 23:34:23 +02:00
|
|
|
template_name = 'base.html'
|
|
|
|
page_name = FRONTEND_PAGE_NAME
|
|
|
|
page_title = 'BASE'
|
|
|
|
|
|
|
|
def get_page_data(self):
|
|
|
|
return {
|
|
|
|
u'name': self.page_name,
|
|
|
|
u'title': self.page_title
|
|
|
|
}
|
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-07-28 15:13:55 +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):
|
|
|
|
@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-07-27 23:34:23 +02:00
|
|
|
template_name = 'index.html'
|
|
|
|
page_title = 'Index'
|
|
|
|
|
2014-08-11 16:01:03 +02:00
|
|
|
class ProjectOverView(ProtectedFormView):
|
2014-08-07 14:47:59 +02:00
|
|
|
template_name = 'projects.html'
|
|
|
|
page_title = 'Projects'
|
|
|
|
form_class = NewProjectForm
|
|
|
|
success_url = '/projects'
|
2014-07-28 15:13:55 +02:00
|
|
|
|
2014-08-11 16:01:03 +02:00
|
|
|
def get_context_data(self, **kwargs):
|
2014-09-15 16:32:23 +02:00
|
|
|
kwargs[u'projects'] = Project.objects.order_by('id')
|
2014-08-11 16:01:03 +02:00
|
|
|
return super(ProjectOverView, self).get_context_data(**kwargs)
|
|
|
|
|
2014-07-28 15:13:55 +02:00
|
|
|
def form_valid(self, form):
|
2014-08-11 16:01:03 +02:00
|
|
|
form.save()
|
|
|
|
return super(ProjectOverView, self).form_valid(form)
|
|
|
|
|
2014-09-15 16:32:23 +02:00
|
|
|
class UpdateProjectView(ProtectedFormView, SingleObjectMixin):
|
2014-08-11 16:01:03 +02:00
|
|
|
template_name = 'project.html'
|
2014-09-15 16:32:23 +02:00
|
|
|
page_title = 'Update project'
|
2014-08-11 16:01:03 +02:00
|
|
|
form_class = FileUploadForm
|
|
|
|
|
2014-08-29 13:55:43 +02:00
|
|
|
success_url = '/projects/'
|
2014-08-29 15:52:09 +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)
|
|
|
|
return super(UpdateProjectView, 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']
|
|
|
|
self.success_url = self.success_url + self.project_id + '/update'
|
|
|
|
return super(UpdateProjectView, self).post(request, *args, **kwargs)
|
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']
|
|
|
|
original_filename = str(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-16 11:44:06 +02:00
|
|
|
target_filename = 'tmp/project_' + str(self.project_id) + '.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-08-29 13:55:43 +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-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-08-29 13:55:43 +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:
|
|
|
|
target_filename = 'citavi/project_' + str(self.project_id) + '.ctt4'
|
|
|
|
""" 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()
|
|
|
|
|
|
|
|
""" Refresh identities from citavi project. """
|
|
|
|
# TODO
|
2014-09-16 13:51:51 +02:00
|
|
|
#citavi_project = Citavi.Project(sqlite_path)
|
2014-09-16 11:44:06 +02:00
|
|
|
#del citavi_project
|
2014-09-15 16:32:23 +02:00
|
|
|
return super(UpdateProjectView, self).form_valid(form)
|
2014-09-16 11:34:53 +02:00
|
|
|
|