2014-07-26 13:00:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2014-07-27 23:34:23 +02:00
|
|
|
|
2014-07-28 15:13:55 +02:00
|
|
|
from django.views.generic import TemplateView, FormView, CreateView, UpdateView
|
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-07 14:47:59 +02:00
|
|
|
from frontend.forms import NewProjectForm
|
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'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-07 14:47:59 +02:00
|
|
|
class ProjectView(ProtectedFormView):
|
|
|
|
template_name = 'projects.html'
|
|
|
|
page_title = 'Projects'
|
|
|
|
form_class = NewProjectForm
|
|
|
|
success_url = '/projects'
|
2014-07-28 15:13:55 +02:00
|
|
|
|
|
|
|
def form_valid(self, form):
|
2014-08-07 14:47:59 +02:00
|
|
|
return super(ProjectView, self).form_valid(form)
|