From 9c1156af5baf01a9f919526c9ea0961a354c9620 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 29 Jul 2014 14:48:09 +0200 Subject: [PATCH] [TASK] Improve view class structure with own mixin. --- frontend/forms.py | 50 +++++++++----------------- frontend/templates/projectcontext.html | 5 ++- frontend/views.py | 50 +++++++++++++------------- 3 files changed, 44 insertions(+), 61 deletions(-) diff --git a/frontend/forms.py b/frontend/forms.py index 704eb0f..1d8d890 100644 --- a/frontend/forms.py +++ b/frontend/forms.py @@ -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'] diff --git a/frontend/templates/projectcontext.html b/frontend/templates/projectcontext.html index cf4088a..b877d91 100644 --- a/frontend/templates/projectcontext.html +++ b/frontend/templates/projectcontext.html @@ -3,7 +3,6 @@ {% load crispy_forms_tags %} {% endblock %} {% block content %} -

- {% crispy form %} -

+

Create a new Project Context

+

{% crispy form %}

{% endblock %} \ No newline at end of file diff --git a/frontend/views.py b/frontend/views.py index 86c468a..6376be4 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -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 + +class MyFormView(MyViewMixin, FormView): + pass - def get_page_data(self): - return { - u'name': self.page_name, - u'title': self.page_title - } +class MyCreateView(MyViewMixin, CreateView): + pass - def get_context_data(self, **kwargs): - kwargs[u'page'] = self.get_page_data() - return super(MyFormView, self).get_context_data(**kwargs) +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 \ No newline at end of file + return super(ProjectContextView, self).form_valid(form)