[TASK] Improve view class structure with own mixin.
This commit is contained in:
parent
daf535a5f3
commit
9c1156af5b
|
@ -1,53 +1,35 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from django import forms
|
from django import forms
|
||||||
|
from frontend.models import ProjectContext
|
||||||
from crispy_forms.helper import FormHelper
|
from crispy_forms.helper import FormHelper
|
||||||
from crispy_forms.layout import Submit, Layout
|
from crispy_forms.layout import Submit, Layout
|
||||||
|
|
||||||
class ExampleForm(forms.Form):
|
class NewProjectContextForm(forms.ModelForm):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(ExampleForm, self).__init__(*args, **kwargs)
|
super(NewProjectContextForm, self).__init__(*args, **kwargs)
|
||||||
self.helper = FormHelper()
|
self.helper = FormHelper()
|
||||||
self.helper.form_class = 'form-horizontal'
|
self.helper.form_class = 'form-horizontal'
|
||||||
self.helper.label_class = 'col-lg-2'
|
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.form_method = 'post'
|
||||||
self.helper.layout = Layout(
|
self.helper.layout = Layout(
|
||||||
'like_website',
|
'name',
|
||||||
'favorite_food',
|
'description',
|
||||||
'favorite_color',
|
Submit('send', 'Senden', css_class = 'btn-default')
|
||||||
'favorite_number',
|
)
|
||||||
'notes',
|
|
||||||
Submit('send', 'Senden', css_class = 'btn-default pull-right')
|
|
||||||
)
|
|
||||||
|
|
||||||
like_website = forms.TypedChoiceField(
|
name = forms.CharField(
|
||||||
label = "Do you like this website?",
|
label = "Name",
|
||||||
choices = ((1, "Yes"), (0, "No")),
|
max_length = 255,
|
||||||
coerce = lambda x: bool(int(x)),
|
|
||||||
widget = forms.RadioSelect,
|
|
||||||
initial = '1',
|
|
||||||
required = True,
|
required = True,
|
||||||
)
|
)
|
||||||
|
|
||||||
favorite_food = forms.CharField(
|
description = forms.CharField(
|
||||||
label = "What is your favorite food?",
|
label = "Description",
|
||||||
max_length = 80,
|
|
||||||
required = True,
|
required = True,
|
||||||
)
|
)
|
||||||
|
|
||||||
favorite_color = forms.CharField(
|
class Meta:
|
||||||
label = "What is your favorite color?",
|
model = ProjectContext
|
||||||
max_length = 80,
|
fields = ['name', 'description']
|
||||||
required = True,
|
|
||||||
)
|
|
||||||
|
|
||||||
favorite_number = forms.IntegerField(
|
|
||||||
label = "Favorite number",
|
|
||||||
required = False,
|
|
||||||
)
|
|
||||||
|
|
||||||
notes = forms.CharField(
|
|
||||||
label = "Additional notes or feedback",
|
|
||||||
required = False,
|
|
||||||
)
|
|
||||||
|
|
|
@ -3,7 +3,6 @@
|
||||||
{% load crispy_forms_tags %}
|
{% load crispy_forms_tags %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<p>
|
<h3>Create a new Project Context</h3>
|
||||||
{% crispy form %}
|
<p>{% crispy form %}</p>
|
||||||
</p>
|
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -6,7 +6,7 @@ from django.contrib.auth.decorators import login_required
|
||||||
from django.utils.decorators import method_decorator
|
from django.utils.decorators import method_decorator
|
||||||
from django.contrib.auth.views import login, logout
|
from django.contrib.auth.views import login, logout
|
||||||
|
|
||||||
from frontend.forms import ExampleForm
|
from frontend.forms import NewProjectContextForm
|
||||||
|
|
||||||
FRONTEND_PAGE_NAME = 'Citavi Mapper'
|
FRONTEND_PAGE_NAME = 'Citavi Mapper'
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ def logout_wrap(*args, **kwargs):
|
||||||
|
|
||||||
|
|
||||||
# My base classes for views
|
# My base classes for views
|
||||||
class MyTemplateView(TemplateView):
|
class MyViewMixin(object):
|
||||||
template_name = 'base.html'
|
template_name = 'base.html'
|
||||||
page_name = FRONTEND_PAGE_NAME
|
page_name = FRONTEND_PAGE_NAME
|
||||||
page_title = 'BASE'
|
page_title = 'BASE'
|
||||||
|
@ -44,36 +44,43 @@ class MyTemplateView(TemplateView):
|
||||||
|
|
||||||
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(MyTemplateView, self).get_context_data(**kwargs)
|
return super(MyViewMixin, self).get_context_data(**kwargs)
|
||||||
|
|
||||||
class MyFormView(FormView):
|
class MyTemplateView(MyViewMixin, TemplateView):
|
||||||
template_name = 'base.html'
|
pass
|
||||||
page_name = FRONTEND_PAGE_NAME
|
|
||||||
page_title = 'BASE'
|
class MyFormView(MyViewMixin, FormView):
|
||||||
|
pass
|
||||||
|
|
||||||
def get_page_data(self):
|
class MyCreateView(MyViewMixin, CreateView):
|
||||||
return {
|
pass
|
||||||
u'name': self.page_name,
|
|
||||||
u'title': self.page_title
|
|
||||||
}
|
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
class MyUpdateView(MyViewMixin, UpdateView):
|
||||||
kwargs[u'page'] = self.get_page_data()
|
pass
|
||||||
return super(MyFormView, self).get_context_data(**kwargs)
|
|
||||||
|
|
||||||
|
|
||||||
|
# Mixin to protect pages
|
||||||
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)
|
||||||
|
|
||||||
|
|
||||||
|
# My protected view classes
|
||||||
class ProtectedTemplateView(LoggedInMixin, MyTemplateView):
|
class ProtectedTemplateView(LoggedInMixin, MyTemplateView):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
class ProtectedFormView(LoggedInMixin, MyFormView):
|
class ProtectedFormView(LoggedInMixin, MyFormView):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Actual Views
|
class ProtectedCreateView(LoggedInMixin, MyCreateView):
|
||||||
|
pass
|
||||||
|
|
||||||
|
class ProtectedUpdateView(LoggedInMixin, MyUpdateView):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# Actual Views
|
||||||
class IndexView(ProtectedTemplateView):
|
class IndexView(ProtectedTemplateView):
|
||||||
template_name = 'index.html'
|
template_name = 'index.html'
|
||||||
page_title = 'Index'
|
page_title = 'Index'
|
||||||
|
@ -85,13 +92,8 @@ class RegisterView(MyTemplateView):
|
||||||
class ProjectContextView(ProtectedFormView):
|
class ProjectContextView(ProtectedFormView):
|
||||||
template_name = 'projectcontext.html'
|
template_name = 'projectcontext.html'
|
||||||
page_title = 'Project Contexts'
|
page_title = 'Project Contexts'
|
||||||
form_class = ExampleForm
|
form_class = NewProjectContextForm
|
||||||
success_url = '/project/thanks'
|
success_url = '/project'
|
||||||
|
|
||||||
def __init__(self, **kwargs):
|
|
||||||
super(ProjectContextView, self).__init__(**kwargs)
|
|
||||||
print(self.__class__.__mro__)
|
|
||||||
|
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
pass
|
return super(ProjectContextView, self).form_valid(form)
|
||||||
|
|
Loading…
Reference in New Issue