From 4bffa5ec6219a3b8e245030ad8075dd78d8ecb9f Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Sat, 24 Jan 2015 23:34:15 +0100 Subject: [PATCH] add new views to hostingpackages app - rename CreateHostingPackage to CreateCustomerHostingPackage - add new CreateHostingPackage that allows to select a customer - rename CreateHostingPackageForm to CreateCustomerHostingPackageForm - add new CreateHostingPackageForm that has a customer field - add new URL pattern create_hosting_package, rename existing pattern to create_customer_hosting_package - modify template dashboard/user_dashboard to use the correct URL name --- gnuviechadmin/hostingpackages/forms.py | 34 ++++++++++-- gnuviechadmin/hostingpackages/urls.py | 10 +++- gnuviechadmin/hostingpackages/views.py | 53 ++++++++++++++----- .../templates/dashboard/user_dashboard.html | 2 +- 4 files changed, 80 insertions(+), 19 deletions(-) diff --git a/gnuviechadmin/hostingpackages/forms.py b/gnuviechadmin/hostingpackages/forms.py index bc8a8d9..727580d 100644 --- a/gnuviechadmin/hostingpackages/forms.py +++ b/gnuviechadmin/hostingpackages/forms.py @@ -17,9 +17,10 @@ from crispy_forms.layout import ( from .models import CustomerHostingPackage -class CreateHostingPackageForm(forms.ModelForm): +class CreateCustomerHostingPackageForm(forms.ModelForm): """ - This form class is used for creating new customer hosting packages. + This form class is used for creating new customer hosting packages with + a preselected customer. """ class Meta: @@ -28,13 +29,13 @@ class CreateHostingPackageForm(forms.ModelForm): def __init__(self, instance, *args, **kwargs): username = kwargs.pop('user') - super(CreateHostingPackageForm, self).__init__( + super(CreateCustomerHostingPackageForm, self).__init__( *args, **kwargs ) self.fields['description'].widget.attrs['rows'] = 2 self.helper = FormHelper() self.helper.form_action = reverse( - 'create_hosting_package', kwargs={'user': username} + 'create_customer_hosting_package', kwargs={'user': username} ) self.helper.layout = Layout( 'template', @@ -42,3 +43,28 @@ class CreateHostingPackageForm(forms.ModelForm): 'description', Submit('submit', _('Add Hosting Package')), ) + + +class CreateHostingPackageForm(forms.ModelForm): + """ + This form class is used for creating new customer hosting packages. + + """ + class Meta: + model = CustomerHostingPackage + fields = ['customer', 'template', 'name', 'description'] + + def __init__(self, instance, *args, **kwargs): + super(CreateHostingPackageForm, self).__init__( + *args, **kwargs + ) + self.fields['description'].widget.attrs['rows'] = 2 + self.helper = FormHelper() + self.helper.form_action = reverse('create_hosting_package') + self.helper.layout = Layout( + 'customer', + 'template', + 'name', + 'description', + Submit('submit', _('Add Hosting Package')), + ) diff --git a/gnuviechadmin/hostingpackages/urls.py b/gnuviechadmin/hostingpackages/urls.py index 0280eca..d8709fd 100644 --- a/gnuviechadmin/hostingpackages/urls.py +++ b/gnuviechadmin/hostingpackages/urls.py @@ -7,16 +7,24 @@ from __future__ import absolute_import, unicode_literals from django.conf.urls import patterns, url from .views import ( + AllCustomerHostingPackageList, CreateHostingPackage, + CreateCustomerHostingPackage, CustomerHostingPackageDetails, ) urlpatterns = patterns( '', - url(r'^(?P[\w0-9@.+-_]+)/create$', CreateHostingPackage.as_view(), + url(r'^create$', CreateHostingPackage.as_view(), name='create_hosting_package'), + url(r'^(?P[\w0-9@.+-_]+)/create$', + CreateCustomerHostingPackage.as_view(), + name='create_customer_hosting_package'), url(r'^(?P[\w0-9@.+-_]+)/hostingpackage/(?P\d+)/$', CustomerHostingPackageDetails.as_view(), name='hosting_package_details'), + url(r'^allpackages/', + AllCustomerHostingPackageList.as_view(), + name='all_hosting_packages'), ) diff --git a/gnuviechadmin/hostingpackages/views.py b/gnuviechadmin/hostingpackages/views.py index e368088..7917b14 100644 --- a/gnuviechadmin/hostingpackages/views.py +++ b/gnuviechadmin/hostingpackages/views.py @@ -5,10 +5,12 @@ This module defines views related to hosting packages. from __future__ import absolute_import, unicode_literals from django.conf import settings -from django.core.urlresolvers import reverse -from django.shortcuts import redirect +from django.shortcuts import redirect, get_object_or_404 from django.utils.translation import ugettext as _ -from django.views.generic import DetailView +from django.views.generic import ( + DetailView, + ListView, +) from django.views.generic.edit import CreateView from django.contrib import messages from django.contrib.auth import get_user_model @@ -20,7 +22,10 @@ from braces.views import ( from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin -from .forms import CreateHostingPackageForm +from .forms import ( + CreateCustomerHostingPackageForm, + CreateHostingPackageForm, +) from .models import CustomerHostingPackage @@ -36,23 +41,37 @@ class CreateHostingPackage( template_name_suffix = '_create' form_class = CreateHostingPackageForm + def form_valid(self, form): + hostingpackage = form.save() + messages.success( + self.request, + _('Started setup of new hosting package {name}.').format( + name=hostingpackage.name) + ) + return redirect(hostingpackage) + + +class CreateCustomerHostingPackage(CreateHostingPackage): + """ + Create a hosting package for a selected customer. + + """ + form_class = CreateCustomerHostingPackageForm + def get_form_kwargs(self): - kwargs = super(CreateHostingPackage, self).get_form_kwargs() + kwargs = super(CreateCustomerHostingPackage, self).get_form_kwargs() kwargs.update(self.kwargs) return kwargs def get_customer_object(self): - return get_user_model().objects.get(username=self.kwargs['user']) + return get_object_or_404( + get_user_model(), username=self.kwargs['user']) def get_context_data(self, **kwargs): - context = super(CreateHostingPackage, self).get_context_data(**kwargs) + context = super(CreateCustomerHostingPackage, self).get_context_data(**kwargs) context['customer'] = self.get_customer_object() return context - def get_success_url(self): - return reverse( - 'customer_dashboard', kwargs={'slug': self.kwargs['user']}) - def form_valid(self, form): hostingpackage = form.save(commit=False) hostingpackage.customer = self.get_customer_object() @@ -62,7 +81,7 @@ class CreateHostingPackage( _('Started setup of new hosting package {name}.').format( name=hostingpackage.name) ) - return redirect(self.get_success_url()) + return redirect(hostingpackage) class CustomerHostingPackageDetails(StaffOrSelfLoginRequiredMixin, DetailView): @@ -74,7 +93,8 @@ class CustomerHostingPackageDetails(StaffOrSelfLoginRequiredMixin, DetailView): context_object_name = 'hostingpackage' def get_customer_object(self): - return get_user_model().objects.get(username=self.kwargs['user']) + return get_object_or_404( + get_user_model(), username=self.kwargs['user']) def get_context_data(self, **kwargs): context = super(CustomerHostingPackageDetails, self).get_context_data( @@ -84,3 +104,10 @@ class CustomerHostingPackageDetails(StaffOrSelfLoginRequiredMixin, DetailView): 'uploadserver': settings.OSUSER_UPLOAD_SERVER, }) return context + + +class AllCustomerHostingPackageList( + LoginRequiredMixin, StaffuserRequiredMixin, ListView +): + model = CustomerHostingPackage + template_name_suffix = '_admin_list' diff --git a/gnuviechadmin/templates/dashboard/user_dashboard.html b/gnuviechadmin/templates/dashboard/user_dashboard.html index 35a20c0..7afb025 100644 --- a/gnuviechadmin/templates/dashboard/user_dashboard.html +++ b/gnuviechadmin/templates/dashboard/user_dashboard.html @@ -42,7 +42,7 @@

{% if user == object %}{% trans "You have no hosting packages yet." %}{% else %}{% trans "This user has no hosting packages assigned yet." %}{% endif %}

{% endif %} {% if user.is_staff %} - {% trans "Add hosting package" %} + {% trans "Add hosting package" %} {% endif %}