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
This commit is contained in:
Jan Dittberner 2015-01-24 23:34:15 +01:00
parent 7991d2bf4f
commit 4bffa5ec62
4 changed files with 80 additions and 19 deletions

View file

@ -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')),
)

View file

@ -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<user>[\w0-9@.+-_]+)/create$', CreateHostingPackage.as_view(),
url(r'^create$', CreateHostingPackage.as_view(),
name='create_hosting_package'),
url(r'^(?P<user>[\w0-9@.+-_]+)/create$',
CreateCustomerHostingPackage.as_view(),
name='create_customer_hosting_package'),
url(r'^(?P<user>[\w0-9@.+-_]+)/hostingpackage/(?P<pk>\d+)/$',
CustomerHostingPackageDetails.as_view(),
name='hosting_package_details'),
url(r'^allpackages/',
AllCustomerHostingPackageList.as_view(),
name='all_hosting_packages'),
)

View file

@ -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'

View file

@ -42,7 +42,7 @@
<p class="text-info">{% if user == object %}{% trans "You have no hosting packages yet." %}{% else %}{% trans "This user has no hosting packages assigned yet." %}{% endif %}</p>
{% endif %}
{% if user.is_staff %}
<a href="{% url "create_hosting_package" user=object.username %}" class="btn btn-primary">{% trans "Add hosting package" %}</a>
<a href="{% url "create_customer_hosting_package" user=object.username %}" class="btn btn-primary">{% trans "Add hosting package" %}</a>
{% endif %}
</div>
</div>