[BUGFIX] Restructure view classes so they actually work.

This commit is contained in:
Jan Philipp Timme 2014-07-28 15:13:55 +02:00
parent 8ed554bca9
commit e965a76fb8
6 changed files with 112 additions and 10 deletions

View File

@ -6,11 +6,12 @@ from django.contrib import admin
admin.autodiscover() admin.autodiscover()
from frontend.views import login_wrap, logout_wrap from frontend.views import login_wrap, logout_wrap
from frontend.views import IndexView, RegisterView from frontend.views import IndexView, RegisterView, ProjectContextView
urlpatterns = patterns('', urlpatterns = patterns('',
url(r'^$', IndexView.as_view(), name='frontend-index'), url(r'^$', IndexView.as_view(), name='frontend-index'),
url(r'^project/$', ProjectContextView.as_view(), name='frontend-projectcontext'),
url(r'^login/$', login_wrap, name='frontend-login'), url(r'^login/$', login_wrap, name='frontend-login'),
url(r'^logout/$', logout_wrap, name='frontend-logout'), url(r'^logout/$', logout_wrap, name='frontend-logout'),
url(r'^register/', RegisterView.as_view(), name='frontend-register'), url(r'^register/', RegisterView.as_view(), name='frontend-register'),

53
frontend/forms.py Normal file
View File

@ -0,0 +1,53 @@
# -*- coding: utf-8 -*-
from django import forms
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit, Layout
class ExampleForm(forms.Form):
def __init__(self, *args, **kwargs):
super(ExampleForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-lg-2'
self.helper.field_class = 'col-lg-10'
self.helper.form_method = 'post'
self.helper.layout = Layout(
'like_website',
'favorite_food',
'favorite_color',
'favorite_number',
'notes',
Submit('send', 'Senden', css_class = 'btn-default pull-right')
)
like_website = forms.TypedChoiceField(
label = "Do you like this website?",
choices = ((1, "Yes"), (0, "No")),
coerce = lambda x: bool(int(x)),
widget = forms.RadioSelect,
initial = '1',
required = True,
)
favorite_food = forms.CharField(
label = "What is your favorite food?",
max_length = 80,
required = True,
)
favorite_color = forms.CharField(
label = "What is your favorite color?",
max_length = 80,
required = True,
)
favorite_number = forms.IntegerField(
label = "Favorite number",
required = False,
)
notes = forms.CharField(
label = "Additional notes or feedback",
required = False,
)

View File

@ -2,6 +2,7 @@
{% block content %} {% block content %}
<p> <p>
Welcome, {{user}}!<br> Welcome, {{user}}!<br>
Feel free to discover. Feel free to discover.<br>
<a href="project/">Project Contexts</a>
</p> </p>
{% endblock %} {% endblock %}

View File

@ -12,6 +12,8 @@
{% compressed_js 'bootstrap' %} {% compressed_js 'bootstrap' %}
<!-- /Bootstrap --> <!-- /Bootstrap -->
{% compressed_css 'frontend' %} {% compressed_css 'frontend' %}
<!-- Additional -->
{% block head %}{% endblock %}
</head> </head>
<body> <body>
<nav class="navbar navbar-default navbar-fixed-top" role="navigation"> <nav class="navbar navbar-default navbar-fixed-top" role="navigation">

View File

@ -0,0 +1,9 @@
{% extends "layout/base.html" %}
{% block head%}
{% load crispy_forms_tags %}
{% endblock %}
{% block content %}
<p>
{% crispy form %}
</p>
{% endblock %}

View File

@ -1,13 +1,16 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django.views.generic import TemplateView, FormView, CreateView, UpdateView
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator from django.utils.decorators import method_decorator
from django.views.generic import TemplateView
from django.contrib.auth.views import login, logout from django.contrib.auth.views import login, logout
from frontend.forms import ExampleForm
FRONTEND_PAGE_NAME = 'Citavi Mapper' FRONTEND_PAGE_NAME = 'Citavi Mapper'
# Login wrapper functions
def login_wrap(*args, **kwargs): def login_wrap(*args, **kwargs):
kwargs['extra_context'] = { kwargs['extra_context'] = {
'page': { 'page': {
@ -27,7 +30,8 @@ def logout_wrap(*args, **kwargs):
return logout(*args, **kwargs) return logout(*args, **kwargs)
class BaseView(TemplateView): # My base classes for views
class MyTemplateView(TemplateView):
template_name = 'base.html' template_name = 'base.html'
page_name = FRONTEND_PAGE_NAME page_name = FRONTEND_PAGE_NAME
page_title = 'BASE' page_title = 'BASE'
@ -37,25 +41,57 @@ class BaseView(TemplateView):
u'name': self.page_name, u'name': self.page_name,
u'title': self.page_title u'title': self.page_title
} }
def get(self, request, **kwargs):
return super(BaseView, self).get(request, **kwargs)
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
kwargs[u'page'] = self.get_page_data() kwargs[u'page'] = self.get_page_data()
return super(BaseView, self).get_context_data(**kwargs) 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): class LoggedInMixin(object):
@method_decorator(login_required) @method_decorator(login_required)
def dispatch(self, *args, **kwargs): def dispatch(self, *args, **kwargs):
return super(LoggedInMixin, self).dispatch(*args, **kwargs) return super(LoggedInMixin, self).dispatch(*args, **kwargs)
class ProtectedSiteView(LoggedInMixin, BaseView): class ProtectedTemplateView(LoggedInMixin, MyTemplateView):
pass pass
class IndexView(ProtectedSiteView): class ProtectedFormView(LoggedInMixin, MyFormView):
pass
# Actual Views
class IndexView(ProtectedTemplateView):
template_name = 'index.html' template_name = 'index.html'
page_title = 'Index' page_title = 'Index'
class RegisterView(BaseView): class RegisterView(MyTemplateView):
template_name = 'register.html' template_name = 'register.html'
page_title = 'Register' 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