2015-01-26 12:06:03 +01:00
|
|
|
"""
|
|
|
|
This module defines form classes for user database editing.
|
|
|
|
|
|
|
|
"""
|
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 crispy_forms.helper import FormHelper
|
|
|
|
from crispy_forms.layout import Submit
|
2015-01-26 12:06:03 +01:00
|
|
|
from django import forms
|
2018-11-19 23:28:40 +01:00
|
|
|
from django.urls import reverse
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2015-01-26 12:06:03 +01:00
|
|
|
|
|
|
|
from gvawebcore.forms import PasswordModelFormMixin
|
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
from .models import DB_TYPES, DatabaseUser, UserDatabase
|
|
|
|
|
2015-01-26 12:06:03 +01:00
|
|
|
|
|
|
|
class AddUserDatabaseForm(forms.ModelForm, PasswordModelFormMixin):
|
|
|
|
"""
|
|
|
|
This form is used to create new user database instances.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2015-01-26 12:06:03 +01:00
|
|
|
db_type = forms.TypedChoiceField(
|
2023-02-18 22:46:48 +01:00
|
|
|
label=_("Database type"),
|
2015-01-26 12:06:03 +01:00
|
|
|
choices=DB_TYPES,
|
|
|
|
widget=forms.RadioSelect,
|
|
|
|
coerce=int,
|
|
|
|
)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = UserDatabase
|
|
|
|
fields = []
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2023-02-18 22:46:48 +01:00
|
|
|
self.hosting_package = kwargs.pop("hostingpackage")
|
|
|
|
self.available_dbtypes = kwargs.pop("dbtypes")
|
2015-01-26 12:06:03 +01:00
|
|
|
super(AddUserDatabaseForm, self).__init__(*args, **kwargs)
|
2023-02-18 22:46:48 +01:00
|
|
|
self.fields["db_type"].choices = self.available_dbtypes
|
2015-01-26 12:06:03 +01:00
|
|
|
if len(self.available_dbtypes) == 1:
|
2023-02-18 22:46:48 +01:00
|
|
|
self.fields["db_type"].initial = self.available_dbtypes[0][0]
|
|
|
|
self.fields["db_type"].widget = forms.HiddenInput()
|
2015-01-26 12:06:03 +01:00
|
|
|
self.helper = FormHelper()
|
|
|
|
self.helper.form_action = reverse(
|
2023-02-18 22:46:48 +01:00
|
|
|
"add_userdatabase", kwargs={"package": self.hosting_package.id}
|
|
|
|
)
|
|
|
|
self.helper.add_input(Submit("submit", _("Create database")))
|
2015-01-26 12:06:03 +01:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
"""
|
|
|
|
Setup a new database with a new database user with the specified
|
|
|
|
password.
|
|
|
|
|
|
|
|
:param boolean commit: whether to save the created database
|
|
|
|
:return: database instance
|
|
|
|
:rtype: :py:class:`userdbs.models.UserDatabase`
|
|
|
|
|
|
|
|
"""
|
|
|
|
data = self.cleaned_data
|
|
|
|
self.instance = UserDatabase.objects.create_userdatabase_with_user(
|
2023-02-18 22:46:48 +01:00
|
|
|
data["db_type"],
|
|
|
|
self.hosting_package.osuser,
|
|
|
|
password=data["password1"],
|
|
|
|
commit=commit,
|
|
|
|
)
|
2015-01-26 12:06:03 +01:00
|
|
|
return super(AddUserDatabaseForm, self).save(commit)
|
2015-01-26 12:39:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ChangeDatabaseUserPasswordForm(forms.ModelForm, PasswordModelFormMixin):
|
|
|
|
"""
|
|
|
|
This form is used to change the password of a database user.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2015-01-26 12:39:42 +01:00
|
|
|
class Meta:
|
|
|
|
model = DatabaseUser
|
|
|
|
fields = []
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2023-02-18 22:46:48 +01:00
|
|
|
self.hosting_package = kwargs.pop("hostingpackage")
|
2015-01-26 12:39:42 +01:00
|
|
|
super(ChangeDatabaseUserPasswordForm, self).__init__(*args, **kwargs)
|
|
|
|
self.helper = FormHelper()
|
|
|
|
self.helper.form_action = reverse(
|
2023-02-18 22:46:48 +01:00
|
|
|
"change_dbuser_password",
|
|
|
|
kwargs={
|
|
|
|
"slug": self.instance.name,
|
|
|
|
"package": self.hosting_package.id,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.helper.add_input(Submit("submit", _("Set password")))
|
2015-01-26 12:39:42 +01:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
2023-02-18 22:46:48 +01:00
|
|
|
self.instance.set_password(self.cleaned_data["password1"])
|
2015-01-26 12:39:42 +01:00
|
|
|
return super(ChangeDatabaseUserPasswordForm, self).save()
|