2015-01-26 12:06:03 +01:00
|
|
|
"""
|
|
|
|
This module defines views for user database handling.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
from __future__ import absolute_import
|
2015-01-26 12:06:03 +01:00
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.contrib import messages
|
2016-01-28 16:17:50 +01:00
|
|
|
from django.core.exceptions import SuspiciousOperation
|
2015-01-26 12:06:03 +01:00
|
|
|
from django.shortcuts import redirect
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
from django.views.generic.edit import CreateView, DeleteView, UpdateView
|
2015-01-26 12:06:03 +01:00
|
|
|
from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2015-01-26 12:06:03 +01:00
|
|
|
from gvawebcore.views import HostingPackageAndCustomerMixin
|
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
from .forms import AddUserDatabaseForm, ChangeDatabaseUserPasswordForm
|
|
|
|
from .models import DB_TYPES, DatabaseUser, UserDatabase
|
2015-01-26 12:06:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AddUserDatabase(
|
|
|
|
HostingPackageAndCustomerMixin, StaffOrSelfLoginRequiredMixin, CreateView
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
This view is used to setup new user databases.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2015-01-26 12:06:03 +01:00
|
|
|
model = UserDatabase
|
2023-02-18 22:46:48 +01:00
|
|
|
context_object_name = "database"
|
|
|
|
template_name_suffix = "_create"
|
2015-01-26 12:06:03 +01:00
|
|
|
form_class = AddUserDatabaseForm
|
|
|
|
|
|
|
|
def _get_dbtypes(self, hostingpackage):
|
|
|
|
retval = []
|
|
|
|
db_options = hostingpackage.get_databases()
|
|
|
|
for opt in db_options:
|
|
|
|
dbs_of_type = UserDatabase.objects.filter(
|
2023-02-18 22:46:48 +01:00
|
|
|
db_user__osuser=hostingpackage.osuser, db_user__db_type=opt["db_type"]
|
|
|
|
).count()
|
|
|
|
if dbs_of_type < opt["number"]:
|
|
|
|
retval.append((opt["db_type"], DB_TYPES[opt["db_type"]]))
|
2016-01-28 16:17:50 +01:00
|
|
|
if len(retval) < 1:
|
|
|
|
raise SuspiciousOperation(
|
2023-02-18 22:46:48 +01:00
|
|
|
_("The hosting package has no database products assigned.")
|
|
|
|
)
|
2015-01-26 12:06:03 +01:00
|
|
|
return retval
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(AddUserDatabase, self).get_form_kwargs()
|
2023-02-18 22:46:48 +01:00
|
|
|
kwargs["hostingpackage"] = self.get_hosting_package()
|
|
|
|
kwargs["dbtypes"] = self._get_dbtypes(kwargs["hostingpackage"])
|
2015-01-26 12:06:03 +01:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
userdatabase = form.save()
|
|
|
|
messages.success(
|
|
|
|
self.request,
|
2023-02-18 22:46:48 +01:00
|
|
|
_(
|
|
|
|
"Successfully create new {type} database {dbname} for user " "{dbuser}."
|
|
|
|
).format(
|
|
|
|
type=userdatabase.db_user.db_type,
|
|
|
|
dbname=userdatabase.db_name,
|
|
|
|
dbuser=userdatabase.db_user,
|
|
|
|
),
|
2015-01-26 12:06:03 +01:00
|
|
|
)
|
|
|
|
return redirect(self.get_hosting_package())
|
2015-01-26 12:39:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ChangeDatabaseUserPassword(
|
|
|
|
HostingPackageAndCustomerMixin, StaffOrSelfLoginRequiredMixin, UpdateView
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
This view is used to change a database user's password.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2015-01-26 12:39:42 +01:00
|
|
|
model = DatabaseUser
|
2023-02-18 22:46:48 +01:00
|
|
|
slug_field = "name"
|
|
|
|
context_object_name = "dbuser"
|
|
|
|
template_name_suffix = "_setpassword"
|
2015-01-26 12:39:42 +01:00
|
|
|
form_class = ChangeDatabaseUserPasswordForm
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(ChangeDatabaseUserPassword, self).get_form_kwargs()
|
2023-02-18 22:46:48 +01:00
|
|
|
kwargs["hostingpackage"] = self.get_hosting_package()
|
2015-01-26 12:39:42 +01:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2023-02-18 22:46:48 +01:00
|
|
|
context = super(ChangeDatabaseUserPassword, self).get_context_data(**kwargs)
|
|
|
|
context["hostingpackage"] = self.get_hosting_package()
|
|
|
|
context["customer"] = self.get_customer_object()
|
2015-01-26 12:39:42 +01:00
|
|
|
return context
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
db_user = form.save()
|
|
|
|
messages.success(
|
|
|
|
self.request,
|
2023-02-18 22:46:48 +01:00
|
|
|
_("Successfully changed password of database user {dbuser}.").format(
|
|
|
|
dbuser=db_user.name
|
|
|
|
),
|
2015-01-26 12:39:42 +01:00
|
|
|
)
|
|
|
|
return redirect(self.get_hosting_package())
|
2015-01-26 13:39:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteUserDatabase(
|
|
|
|
HostingPackageAndCustomerMixin, StaffOrSelfLoginRequiredMixin, DeleteView
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
This view is used to delete user databases and databases users if they have
|
|
|
|
no more databases assigned.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2015-01-26 13:39:35 +01:00
|
|
|
model = UserDatabase
|
2023-02-18 22:46:48 +01:00
|
|
|
slug_field = "db_name"
|
|
|
|
context_object_name = "database"
|
2015-01-26 13:39:35 +01:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(DeleteUserDatabase, self).get_context_data(**kwargs)
|
2023-02-18 22:46:48 +01:00
|
|
|
context.update(
|
|
|
|
{
|
|
|
|
"hostingpackage": self.get_hosting_package(),
|
|
|
|
"customer": self.get_customer_object(),
|
|
|
|
}
|
|
|
|
)
|
2015-01-26 13:39:35 +01:00
|
|
|
return context
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
messages.success(
|
|
|
|
self.request,
|
2023-02-18 22:46:48 +01:00
|
|
|
_("Database deleted."),
|
2015-01-26 13:39:35 +01:00
|
|
|
)
|
|
|
|
return self.get_hosting_package().get_absolute_url()
|