gva/gnuviechadmin/hostingpackages/views.py
Jan Dittberner 9815bd1f5b implement hosting package option choice view
- implement new hostingpackages.views.HostingOptionChoices
- add URL pattern 'hosting_option_choices' to hostingpackages.urls
- add template hostingpackages/customerhostingpackage_option_choices.html
- link from hostingpackages/customerhostingpackage_detail.html to
  'hosting_package_choices'
2015-01-25 14:04:32 +01:00

148 lines
4.1 KiB
Python

"""
This module defines views related to hosting packages.
"""
from __future__ import absolute_import, unicode_literals
from django.conf import settings
from django.shortcuts import redirect, get_object_or_404
from django.utils.translation import ugettext as _
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
from braces.views import (
LoginRequiredMixin,
StaffuserRequiredMixin,
)
from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin
from .forms import (
CreateCustomerHostingPackageForm,
CreateHostingPackageForm,
)
from .models import (
CustomerHostingPackage,
DiskSpaceOption,
MailboxOption,
UserDatabaseOption,
)
class CreateHostingPackage(
LoginRequiredMixin, StaffuserRequiredMixin, CreateView
):
"""
Create a hosting package.
"""
model = CustomerHostingPackage
raise_exception = True
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(CreateCustomerHostingPackage, self).get_form_kwargs()
kwargs.update(self.kwargs)
return kwargs
def get_customer_object(self):
return get_object_or_404(
get_user_model(), username=self.kwargs['user'])
def get_context_data(self, **kwargs):
context = super(CreateCustomerHostingPackage, self).get_context_data(**kwargs)
context['customer'] = self.get_customer_object()
return context
def form_valid(self, form):
hostingpackage = form.save(commit=False)
hostingpackage.customer = self.get_customer_object()
hostingpackage.save()
messages.success(
self.request,
_('Started setup of new hosting package {name}.').format(
name=hostingpackage.name)
)
return redirect(hostingpackage)
class CustomerHostingPackageDetails(StaffOrSelfLoginRequiredMixin, DetailView):
"""
This view is for showing details of a customer hosting package.
"""
model = CustomerHostingPackage
context_object_name = 'hostingpackage'
def get_customer_object(self):
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(
**kwargs)
context.update({
'customer': self.get_customer_object(),
'uploadserver': settings.OSUSER_UPLOAD_SERVER,
})
return context
class AllCustomerHostingPackageList(
LoginRequiredMixin, StaffuserRequiredMixin, ListView
):
model = CustomerHostingPackage
template_name_suffix = '_admin_list'
class HostingOptionChoices(
LoginRequiredMixin, StaffuserRequiredMixin, DetailView
):
"""
This view displays choices of hosting options for a customer hosting
package.
"""
model = CustomerHostingPackage
context_object_name = 'hostingpackage'
template_name_suffix = '_option_choices'
def get_context_data(self, **kwargs):
context = super(HostingOptionChoices, self).get_context_data(
**kwargs)
context.update({
'customer': self.get_object().customer,
'hosting_options': (
(_('Disk space'),
DiskSpaceOption.objects.all()),
(_('Mailboxes'),
MailboxOption.objects.all()),
(_('Databases'),
UserDatabaseOption.objects.all())
),
})
return context