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
|
|
|
|
2014-09-01 15:30:57 +02:00
|
|
|
""" This form_valid handles the file uploads and triggers a citavi schema import and things. """
|
2014-08-29 13:55:43 +02:00
|
|
|
def form_valid(self, form, *args, **kwargs):
|
2014-09-15 16:32:23 +02:00
|
|
|
original_file = form.files[u'file']
|
|
|
|
original_filename = str(original_file)
|
|
|
|
relative_path = default_storage.save('tmp/project_' + self.project_id + '.ctt4', ContentFile(original_file.read()))
|
2014-09-01 15:30:57 +02:00
|
|
|
path = os.path.join(settings.MEDIA_ROOT, relative_path)
|
|
|
|
|
2014-09-15 16:32:23 +02:00
|
|
|
#TODO: Actually validate that we get an SQLite3 file!
|
2014-08-29 13:55:43 +02:00
|
|
|
|
2014-09-15 16:32:23 +02:00
|
|
|
# Store this in the project model
|
|
|
|
project = Project.objects.get(id=self.project_id)
|
|
|
|
project.associated_filename = original_filename
|
|
|
|
project.save()
|
2014-08-29 13:55:43 +02:00
|
|
|
|
2014-09-15 16:32:23 +02:00
|
|
|
return super(UpdateProjectView, self).form_valid(form)
|
2014-08-29 13:55:43 +02:00
|
|
|
|
|
|
|
|