citavi_mapper/frontend/views.py

165 lines
5.0 KiB
Python

# -*- coding: utf-8 -*-
# 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
from django.http import HttpResponse, HttpResponseRedirect
from django.views.generic import TemplateView, FormView, CreateView, UpdateView
from django.views.generic.detail import SingleObjectMixin
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.models import Project
FRONTEND_PAGE_NAME = 'Citavi Mapper'
# Login wrapper functions
def login_wrap(*args, **kwargs):
kwargs['extra_context'] = {
'page': {
'name': FRONTEND_PAGE_NAME,
'title': 'Login'
}
}
return login(*args, **kwargs)
def logout_wrap(*args, **kwargs):
kwargs['extra_context'] = {
'page': {
'name': FRONTEND_PAGE_NAME,
'title': 'Logout'
}
}
return logout(*args, **kwargs)
# My base classes for views
class MyViewMixin(object):
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
}
def get_context_data(self, **kwargs):
kwargs[u'page'] = self.get_page_data()
return super(MyViewMixin, self).get_context_data(**kwargs)
class MyTemplateView(MyViewMixin, TemplateView):
pass
class MyFormView(MyViewMixin, FormView):
pass
class MyCreateView(MyViewMixin, CreateView):
pass
class MyUpdateView(MyViewMixin, UpdateView):
pass
# Mixin to protect pages
class LoggedInMixin(object):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(LoggedInMixin, self).dispatch(*args, **kwargs)
# My protected view classes
class ProtectedTemplateView(LoggedInMixin, MyTemplateView):
pass
class ProtectedFormView(LoggedInMixin, MyFormView):
pass
class ProtectedCreateView(LoggedInMixin, MyCreateView):
pass
class ProtectedUpdateView(LoggedInMixin, MyUpdateView):
pass
# Helper view methods
def enterProject(request, project_id=None):
try:
if request.session['project_id'] != project_id:
return HttpResponse("<enterProject> Please leave your current project - Project ID " + request.session['project_id'] + " is still active.")
except KeyError:
pass
request.session['project_id'] = project_id
return HttpResponseRedirect('/projects/' + project_id + '/')
def leaveProject(request, project_id=None):
try:
if request.session['project_id'] == project_id:
del request.session['project_id']
else:
return HttpResponse("<leaveProject> You tried to leave project with ID " + project_id + ", but your active project id is " + request.session['project_id'] + ".")
except KeyError:
pass
return HttpResponseRedirect('/projects/')
# Actual Views
class IndexView(ProtectedTemplateView):
template_name = 'index.html'
page_title = 'Index'
class ProjectOverView(ProtectedFormView):
template_name = 'projects.html'
page_title = 'Projects'
form_class = NewProjectForm
success_url = '/projects'
def get_context_data(self, **kwargs):
kwargs[u'projects'] = Project.objects.all()
return super(ProjectOverView, self).get_context_data(**kwargs)
def form_valid(self, form):
form.save()
return super(ProjectOverView, self).form_valid(form)
class ProjectView(ProtectedFormView, SingleObjectMixin):
template_name = 'project.html'
page_title = 'Project View'
form_class = FileUploadForm
success_url = '/projects/'
def get(self, request, *args, **kwargs):
project_id = kwargs[u'project_id']
self.object = Project.objects.get(pk=project_id)
return super(ProjectView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
project_id = kwargs[u'project_id']
self.success_url = self.success_url + project_id
return super(ProjectView, self).post(request, *args, **kwargs)
""" This form_valid handles the file uploads and triggers a citavi schema import and things. """
def form_valid(self, form, *args, **kwargs):
project_id = self.request.session[u'project_id']
file = form.files[u'file']
relative_path = default_storage.save('tmp/project_import_' + project_id + '.ctt4', ContentFile(file.read()))
path = os.path.join(settings.MEDIA_ROOT, relative_path)
print "Path: " + path
#TODO: Use file to do things here.
from service.sqlitehelper import import_sqlite
import_sqlite(project_id=project_id, sqlite_file=path)
default_storage.delete(relative_path)
return super(ProjectView, self).form_valid(form)