implement adding options to hosting packages

- fix unique constraints on CustomerDiskSpaceOption and
  CustomerUserDatabaseOption to allow multiple options from the same template
  for hosting packages
- fix disk space calculation in CustomerHostingPackage
- implement hostingpackages forms AddDiskspaceOptionForm, AddMailboxOptionForm,
  AddUserDatabaseOptionForm
- implement hostingpackages.views.AddHostingOption
- add new URL pattern add_hosting_option to hostingpackages.urls
- add template hostingpackages/add_hosting_option.html
- link items on hostingpackages/customerhostingpackage_option_choices.html to
  add_hosting_option
This commit is contained in:
Jan Dittberner 2015-01-25 15:15:39 +01:00
parent 9815bd1f5b
commit 0fc823a305
8 changed files with 229 additions and 14 deletions

View file

@ -5,13 +5,17 @@ This module defines views related to hosting packages.
from __future__ import absolute_import, unicode_literals
from django.conf import settings
from django.http import Http404
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.views.generic.edit import (
CreateView,
FormView,
)
from django.contrib import messages
from django.contrib.auth import get_user_model
@ -23,6 +27,9 @@ from braces.views import (
from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin
from .forms import (
AddDiskspaceOptionForm,
AddMailboxOptionForm,
AddUserDatabaseOptionForm,
CreateCustomerHostingPackageForm,
CreateHostingPackageForm,
)
@ -137,11 +144,83 @@ class HostingOptionChoices(
'customer': self.get_object().customer,
'hosting_options': (
(_('Disk space'),
DiskSpaceOption.objects.all()),
[(option, 'diskspace') for option in
DiskSpaceOption.objects.all()]),
(_('Mailboxes'),
MailboxOption.objects.all()),
[(option, 'mailboxes') for option in
MailboxOption.objects.all()]),
(_('Databases'),
UserDatabaseOption.objects.all())
[(option, 'databases') for option in
UserDatabaseOption.objects.all()]),
),
})
return context
class AddHostingOption(
LoginRequiredMixin, StaffuserRequiredMixin, FormView
):
template_name = 'hostingpackages/add_hosting_option.html'
def get_form_class(self):
optiontype = self.kwargs['type']
if optiontype == 'diskspace':
return AddDiskspaceOptionForm
elif optiontype == 'mailboxes':
return AddMailboxOptionForm
elif optiontype == 'databases':
return AddUserDatabaseOptionForm
raise Http404()
def get_hosting_package(self):
return get_object_or_404(
CustomerHostingPackage, pk=int(self.kwargs['package']))
def get_option_template(self):
optiontype = self.kwargs['type']
optionid = int(self.kwargs['optionid'])
if optiontype == 'diskspace':
return get_object_or_404(DiskSpaceOption, pk=optionid)
elif optiontype == 'mailboxes':
return get_object_or_404(MailboxOption, pk=optionid)
elif optiontype == 'databases':
return get_object_or_404(UserDatabaseOption, pk=optionid)
raise Http404()
def get_form_kwargs(self):
kwargs = super(AddHostingOption, self).get_form_kwargs()
kwargs['hostingpackage'] = self.get_hosting_package()
kwargs['option_template'] = self.get_option_template()
return kwargs
def get_initial(self):
initial = super(AddHostingOption, self).get_initial()
template = self.get_option_template()
if type(template) == DiskSpaceOption:
initial.update({
'diskspace': template.diskspace,
'diskspace_unit': template.diskspace_unit,
})
elif type(template) == MailboxOption:
initial['number'] = template.number
elif type(template) == UserDatabaseOption:
initial['number'] = template.number
else:
raise Http404()
return initial
def get_context_data(self, **kwargs):
context = super(AddHostingOption, self).get_context_data(**kwargs)
context['option_template'] = self.get_option_template()
return context
def form_valid(self, form):
option = form.save()
hosting_package = self.get_hosting_package()
messages.success(
self.request,
_("Successfully added option {option} to hosting package "
"{package}.").format(
option=option, package=hosting_package.name)
)
return redirect(hosting_package)