[TASK] Improve view class structure with own mixin.
This commit is contained in:
parent
daf535a5f3
commit
9c1156af5b
|
@ -1,53 +1,35 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
|
||||
from django import forms
|
||||
from frontend.models import ProjectContext
|
||||
from crispy_forms.helper import FormHelper
|
||||
from crispy_forms.layout import Submit, Layout
|
||||
|
||||
class ExampleForm(forms.Form):
|
||||
class NewProjectContextForm(forms.ModelForm):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(ExampleForm, self).__init__(*args, **kwargs)
|
||||
super(NewProjectContextForm, 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.field_class = 'col-lg-4'
|
||||
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')
|
||||
)
|
||||
'name',
|
||||
'description',
|
||||
Submit('send', 'Senden', css_class = 'btn-default')
|
||||
)
|
||||
|
||||
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',
|
||||
name = forms.CharField(
|
||||
label = "Name",
|
||||
max_length = 255,
|
||||
required = True,
|
||||
)
|
||||
|
||||
favorite_food = forms.CharField(
|
||||
label = "What is your favorite food?",
|
||||
max_length = 80,
|
||||
description = forms.CharField(
|
||||
label = "Description",
|
||||
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,
|
||||
)
|
||||
class Meta:
|
||||
model = ProjectContext
|
||||
fields = ['name', 'description']
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
{% load crispy_forms_tags %}
|
||||
{% endblock %}
|
||||
{% block content %}
|
||||
<p>
|
||||
{% crispy form %}
|
||||
</p>
|
||||
<h3>Create a new Project Context</h3>
|
||||
<p>{% crispy form %}</p>
|
||||
{% endblock %}
|
|
@ -6,7 +6,7 @@ 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
|
||||
from frontend.forms import NewProjectContextForm
|
||||
|
||||
FRONTEND_PAGE_NAME = 'Citavi Mapper'
|
||||
|
||||
|
@ -31,7 +31,7 @@ def logout_wrap(*args, **kwargs):
|
|||
|
||||
|
||||
# My base classes for views
|
||||
class MyTemplateView(TemplateView):
|
||||
class MyViewMixin(object):
|
||||
template_name = 'base.html'
|
||||
page_name = FRONTEND_PAGE_NAME
|
||||
page_title = 'BASE'
|
||||
|
@ -44,36 +44,43 @@ class MyTemplateView(TemplateView):
|
|||
|
||||
def get_context_data(self, **kwargs):
|
||||
kwargs[u'page'] = self.get_page_data()
|
||||
return super(MyTemplateView, self).get_context_data(**kwargs)
|
||||
return super(MyViewMixin, self).get_context_data(**kwargs)
|
||||
|
||||
class MyFormView(FormView):
|
||||
template_name = 'base.html'
|
||||
page_name = FRONTEND_PAGE_NAME
|
||||
page_title = 'BASE'
|
||||
class MyTemplateView(MyViewMixin, TemplateView):
|
||||
pass
|
||||
|
||||
def get_page_data(self):
|
||||
return {
|
||||
u'name': self.page_name,
|
||||
u'title': self.page_title
|
||||
}
|
||||
class MyFormView(MyViewMixin, FormView):
|
||||
pass
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
kwargs[u'page'] = self.get_page_data()
|
||||
return super(MyFormView, self).get_context_data(**kwargs)
|
||||
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
|
||||
|
||||
# Actual Views
|
||||
class ProtectedCreateView(LoggedInMixin, MyCreateView):
|
||||
pass
|
||||
|
||||
class ProtectedUpdateView(LoggedInMixin, MyUpdateView):
|
||||
pass
|
||||
|
||||
|
||||
# Actual Views
|
||||
class IndexView(ProtectedTemplateView):
|
||||
template_name = 'index.html'
|
||||
page_title = 'Index'
|
||||
|
@ -85,13 +92,8 @@ class RegisterView(MyTemplateView):
|
|||
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__)
|
||||
|
||||
form_class = NewProjectContextForm
|
||||
success_url = '/project'
|
||||
|
||||
def form_valid(self, form):
|
||||
pass
|
||||
return super(ProjectContextView, self).form_valid(form)
|
||||
|
|
Loading…
Reference in New Issue