2015-01-20 00:51:05 +01:00
|
|
|
"""
|
|
|
|
This module defines views related to hosting packages.
|
|
|
|
|
|
|
|
"""
|
|
|
|
from __future__ import absolute_import, unicode_literals
|
|
|
|
|
2015-01-24 16:23:17 +01:00
|
|
|
from django.conf import settings
|
2015-01-25 15:15:39 +01:00
|
|
|
from django.http import Http404
|
2015-01-24 23:34:15 +01:00
|
|
|
from django.shortcuts import redirect, get_object_or_404
|
2015-01-22 00:19:30 +01:00
|
|
|
from django.utils.translation import ugettext as _
|
2015-01-24 23:34:15 +01:00
|
|
|
from django.views.generic import (
|
|
|
|
DetailView,
|
|
|
|
ListView,
|
|
|
|
)
|
2015-01-25 15:15:39 +01:00
|
|
|
from django.views.generic.edit import (
|
|
|
|
CreateView,
|
|
|
|
FormView,
|
|
|
|
)
|
2015-01-22 00:19:30 +01:00
|
|
|
from django.contrib import messages
|
2015-01-24 16:23:17 +01:00
|
|
|
from django.contrib.auth import get_user_model
|
2015-01-20 00:51:05 +01:00
|
|
|
|
|
|
|
from braces.views import (
|
|
|
|
LoginRequiredMixin,
|
|
|
|
StaffuserRequiredMixin,
|
|
|
|
)
|
|
|
|
|
2015-01-24 16:23:17 +01:00
|
|
|
from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin
|
|
|
|
|
2015-01-24 23:34:15 +01:00
|
|
|
from .forms import (
|
2015-01-25 15:15:39 +01:00
|
|
|
AddDiskspaceOptionForm,
|
|
|
|
AddMailboxOptionForm,
|
|
|
|
AddUserDatabaseOptionForm,
|
2015-01-24 23:34:15 +01:00
|
|
|
CreateCustomerHostingPackageForm,
|
|
|
|
CreateHostingPackageForm,
|
|
|
|
)
|
2015-01-25 14:04:32 +01:00
|
|
|
from .models import (
|
|
|
|
CustomerHostingPackage,
|
|
|
|
DiskSpaceOption,
|
|
|
|
MailboxOption,
|
|
|
|
UserDatabaseOption,
|
|
|
|
)
|
2015-01-20 00:51:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CreateHostingPackage(
|
|
|
|
LoginRequiredMixin, StaffuserRequiredMixin, CreateView
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Create a hosting package.
|
|
|
|
|
|
|
|
"""
|
|
|
|
model = CustomerHostingPackage
|
|
|
|
raise_exception = True
|
|
|
|
template_name_suffix = '_create'
|
|
|
|
form_class = CreateHostingPackageForm
|
|
|
|
|
2015-01-24 23:34:15 +01:00
|
|
|
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
|
|
|
|
|
2015-01-20 00:51:05 +01:00
|
|
|
def get_form_kwargs(self):
|
2015-01-24 23:34:15 +01:00
|
|
|
kwargs = super(CreateCustomerHostingPackage, self).get_form_kwargs()
|
2015-01-20 00:51:05 +01:00
|
|
|
kwargs.update(self.kwargs)
|
|
|
|
return kwargs
|
|
|
|
|
2015-01-24 16:23:17 +01:00
|
|
|
def get_customer_object(self):
|
2015-01-24 23:34:15 +01:00
|
|
|
return get_object_or_404(
|
|
|
|
get_user_model(), username=self.kwargs['user'])
|
2015-01-22 00:19:30 +01:00
|
|
|
|
2015-01-20 00:51:05 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
2015-01-24 23:34:15 +01:00
|
|
|
context = super(CreateCustomerHostingPackage, self).get_context_data(**kwargs)
|
2015-01-24 16:23:17 +01:00
|
|
|
context['customer'] = self.get_customer_object()
|
2015-01-20 00:51:05 +01:00
|
|
|
return context
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
2015-01-22 00:19:30 +01:00
|
|
|
hostingpackage = form.save(commit=False)
|
2015-01-24 16:23:17 +01:00
|
|
|
hostingpackage.customer = self.get_customer_object()
|
2015-01-22 00:19:30 +01:00
|
|
|
hostingpackage.save()
|
|
|
|
messages.success(
|
|
|
|
self.request,
|
|
|
|
_('Started setup of new hosting package {name}.').format(
|
|
|
|
name=hostingpackage.name)
|
|
|
|
)
|
2015-01-24 23:34:15 +01:00
|
|
|
return redirect(hostingpackage)
|
2015-01-24 16:23:17 +01:00
|
|
|
|
|
|
|
|
|
|
|
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):
|
2015-01-24 23:34:15 +01:00
|
|
|
return get_object_or_404(
|
|
|
|
get_user_model(), username=self.kwargs['user'])
|
2015-01-24 16:23:17 +01:00
|
|
|
|
|
|
|
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,
|
2015-01-26 12:00:26 +01:00
|
|
|
'databases': context['hostingpackage'].databases,
|
|
|
|
'osuser': context['hostingpackage'].osuser,
|
|
|
|
'hostingoptions':
|
|
|
|
context['hostingpackage'].get_hostingoptions(),
|
|
|
|
'domains': context['hostingpackage'].domains.all(),
|
|
|
|
'mailboxes': context['hostingpackage'].mailboxes,
|
2015-01-24 16:23:17 +01:00
|
|
|
})
|
2015-02-01 01:55:09 +01:00
|
|
|
context['sshkeys'] = context['osuser'].sshpublickey_set.all()
|
2015-01-24 16:23:17 +01:00
|
|
|
return context
|
2015-01-24 23:34:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AllCustomerHostingPackageList(
|
|
|
|
LoginRequiredMixin, StaffuserRequiredMixin, ListView
|
|
|
|
):
|
|
|
|
model = CustomerHostingPackage
|
|
|
|
template_name_suffix = '_admin_list'
|
2015-01-25 14:04:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
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'),
|
2015-01-25 15:15:39 +01:00
|
|
|
[(option, 'diskspace') for option in
|
|
|
|
DiskSpaceOption.objects.all()]),
|
2015-01-25 14:04:32 +01:00
|
|
|
(_('Mailboxes'),
|
2015-01-25 15:15:39 +01:00
|
|
|
[(option, 'mailboxes') for option in
|
|
|
|
MailboxOption.objects.all()]),
|
2015-01-25 14:04:32 +01:00
|
|
|
(_('Databases'),
|
2015-01-25 15:15:39 +01:00
|
|
|
[(option, 'databases') for option in
|
|
|
|
UserDatabaseOption.objects.all()]),
|
2015-01-25 14:04:32 +01:00
|
|
|
),
|
|
|
|
})
|
|
|
|
return context
|
2015-01-25 15:15:39 +01:00
|
|
|
|
|
|
|
|
|
|
|
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)
|