citavi_mapper/frontend/views.py

97 lines
2.5 KiB
Python

# -*- coding: utf-8 -*-
from django.views.generic import TemplateView, FormView, CreateView, UpdateView
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 ExampleForm
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 MyTemplateView(TemplateView):
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(MyTemplateView, self).get_context_data(**kwargs)
class MyFormView(FormView):
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(MyFormView, self).get_context_data(**kwargs)
class LoggedInMixin(object):
@method_decorator(login_required)
def dispatch(self, *args, **kwargs):
return super(LoggedInMixin, self).dispatch(*args, **kwargs)
class ProtectedTemplateView(LoggedInMixin, MyTemplateView):
pass
class ProtectedFormView(LoggedInMixin, MyFormView):
pass
# Actual Views
class IndexView(ProtectedTemplateView):
template_name = 'index.html'
page_title = 'Index'
class RegisterView(MyTemplateView):
template_name = 'register.html'
page_title = 'Register'
class ProjectContextView(ProtectedFormView):
template_name = 'projectcontext.html'
page_title = 'Project Contexts'
form_class = ExampleForm
success_url = '/project/thanks'
def __init__(self, **kwargs):
super(ProjectContextView, self).__init__(**kwargs)
print(self.__class__.__mro__)
def form_valid(self, form):
pass