2015-01-04 20:22:47 +01:00
|
|
|
"""
|
|
|
|
Admin functionality for the :py:mod:`userdbs.models` models.
|
|
|
|
|
|
|
|
"""
|
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2015-01-04 22:27:43 +01:00
|
|
|
from django import forms
|
2015-01-04 17:57:51 +01:00
|
|
|
from django.contrib import admin
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2015-01-04 17:57:51 +01:00
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
from .models import DatabaseUser, UserDatabase
|
2015-01-04 20:22:47 +01:00
|
|
|
|
|
|
|
|
2015-01-04 22:27:43 +01:00
|
|
|
class DatabaseUserCreationForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
A form for creating :py:class:`database users
|
|
|
|
<userdbs.models.DatabaseUser>`
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = DatabaseUser
|
2023-02-18 22:46:48 +01:00
|
|
|
fields = ["osuser", "db_type"]
|
2015-01-04 22:27:43 +01:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
"""
|
|
|
|
Save the database user.
|
|
|
|
|
|
|
|
:param boolean commit: whether to save the created database user
|
|
|
|
:return: database user instance
|
|
|
|
:rtype: :py:class:`userdbs.models.DatabaseUser`
|
|
|
|
|
|
|
|
"""
|
|
|
|
dbuser = DatabaseUser.objects.create_database_user(
|
2023-02-18 22:46:48 +01:00
|
|
|
osuser=self.cleaned_data["osuser"],
|
|
|
|
db_type=self.cleaned_data["db_type"],
|
|
|
|
commit=commit,
|
|
|
|
)
|
2015-01-04 22:27:43 +01:00
|
|
|
return dbuser
|
|
|
|
|
|
|
|
def save_m2m(self):
|
|
|
|
"""
|
|
|
|
Noop.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class UserDatabaseCreationForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
A form for creating :py:class:`user databases
|
|
|
|
<userdbs.models.UserDatabase>`
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = UserDatabase
|
2023-02-18 22:46:48 +01:00
|
|
|
fields = ["db_user"]
|
2015-01-04 22:27:43 +01:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
"""
|
|
|
|
Save the user database.
|
|
|
|
|
|
|
|
:param boolean commit: whether to save the created user database
|
|
|
|
:return: user database instance
|
|
|
|
:rtype: :py:class:`userdbs.models.UserDatabase`
|
|
|
|
|
|
|
|
"""
|
|
|
|
database = UserDatabase.objects.create_userdatabase(
|
2023-02-18 22:46:48 +01:00
|
|
|
db_user=self.cleaned_data["db_user"], commit=commit
|
|
|
|
)
|
2015-01-04 22:27:43 +01:00
|
|
|
return database
|
|
|
|
|
|
|
|
def save_m2m(self):
|
|
|
|
"""
|
|
|
|
Noop.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class DatabaseUserAdmin(admin.ModelAdmin):
|
|
|
|
"""
|
|
|
|
Admin class for working with :py:class:`database users
|
|
|
|
<userdbs.models.DatabaseUser>`
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
|
|
|
actions = ["perform_delete_selected"]
|
2015-01-04 22:27:43 +01:00
|
|
|
add_form = DatabaseUserCreationForm
|
|
|
|
|
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
|
|
|
"""
|
|
|
|
Use special form for database user creation.
|
|
|
|
|
|
|
|
:param request: the current HTTP request
|
|
|
|
:param obj: either a :py:class:`Database user
|
|
|
|
<userdbs.models.DatabaseUser>` instance or None for a new database
|
|
|
|
user
|
|
|
|
:param kwargs: keyword arguments to be passed to
|
|
|
|
:py:meth:`django.contrib.admin.ModelAdmin.get_form`
|
|
|
|
:return: form instance
|
|
|
|
|
|
|
|
"""
|
|
|
|
defaults = {}
|
|
|
|
if obj is None:
|
2023-02-18 22:46:48 +01:00
|
|
|
defaults.update(
|
|
|
|
{
|
|
|
|
"form": self.add_form,
|
|
|
|
}
|
|
|
|
)
|
2015-01-04 22:27:43 +01:00
|
|
|
defaults.update(kwargs)
|
2023-02-18 22:46:48 +01:00
|
|
|
return super(DatabaseUserAdmin, self).get_form(request, obj, **defaults)
|
2015-01-04 22:27:43 +01:00
|
|
|
|
|
|
|
def get_readonly_fields(self, request, obj=None):
|
|
|
|
"""
|
|
|
|
Make sure that osuser, name and db_type are not editable for existing
|
|
|
|
database users.
|
|
|
|
|
|
|
|
:param request: the current HTTP request
|
|
|
|
:param obj: either a :py:class:`Database user
|
|
|
|
<userdbs.models.DatabaseUser>` instance or None for a new database
|
|
|
|
user
|
|
|
|
:return: a list of fields
|
|
|
|
:rtype: list
|
|
|
|
|
|
|
|
"""
|
|
|
|
if obj:
|
2023-02-18 22:46:48 +01:00
|
|
|
return ["osuser", "name", "db_type"]
|
2015-01-04 22:27:43 +01:00
|
|
|
return []
|
|
|
|
|
|
|
|
def save_model(self, request, obj, form, change):
|
|
|
|
"""
|
|
|
|
Make sure that the user is created in the target database.
|
|
|
|
|
|
|
|
:param request: the current HTTP request
|
|
|
|
:param obj: a :py:class:`Database user <userdbs.models.DatabaseUser>`
|
|
|
|
instance
|
|
|
|
:param form: the form instance
|
|
|
|
:param boolean change: whether this is a change operation or not
|
|
|
|
|
|
|
|
"""
|
|
|
|
if not change:
|
|
|
|
obj.create_in_database()
|
|
|
|
super(DatabaseUserAdmin, self).save_model(request, obj, form, change)
|
|
|
|
|
|
|
|
def perform_delete_selected(self, request, queryset):
|
|
|
|
"""
|
|
|
|
Action to delete a list of selected database users.
|
|
|
|
|
|
|
|
This action calls the delete method of each selected database user in
|
|
|
|
contrast to the default `delete_selected`
|
|
|
|
|
|
|
|
:param request: the current HTTP request
|
|
|
|
:param queryset: Django ORM queryset representing the selected database
|
|
|
|
users
|
|
|
|
|
|
|
|
"""
|
|
|
|
for dbuser in queryset.all():
|
|
|
|
dbuser.delete()
|
2023-02-18 22:46:48 +01:00
|
|
|
|
|
|
|
perform_delete_selected.short_description = _("Delete selected database users")
|
2015-01-04 22:27:43 +01:00
|
|
|
|
|
|
|
def get_actions(self, request):
|
|
|
|
"""
|
|
|
|
Get the available actions for database users.
|
|
|
|
|
|
|
|
This overrides the default behavior to remove the default
|
|
|
|
`delete_selected` action.
|
|
|
|
|
|
|
|
:param request: the current HTTP request
|
|
|
|
:return: list of actions
|
|
|
|
:rtype: list
|
|
|
|
|
|
|
|
"""
|
|
|
|
actions = super(DatabaseUserAdmin, self).get_actions(request)
|
2023-02-18 22:46:48 +01:00
|
|
|
if "delete_selected" in actions: # pragma: no cover
|
|
|
|
del actions["delete_selected"]
|
2015-01-04 22:27:43 +01:00
|
|
|
return actions
|
|
|
|
|
|
|
|
|
|
|
|
class UserDatabaseAdmin(admin.ModelAdmin):
|
|
|
|
"""
|
|
|
|
Admin class for working with :py:class:`user databases
|
|
|
|
<userdbs.models.UserDatabase>`
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
|
|
|
actions = ["perform_delete_selected"]
|
2015-01-04 22:27:43 +01:00
|
|
|
add_form = UserDatabaseCreationForm
|
|
|
|
|
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
|
|
|
"""
|
|
|
|
Use special form for user database creation.
|
|
|
|
|
|
|
|
:param request: the current HTTP request
|
|
|
|
:param obj: either a :py:class:`User database
|
|
|
|
<userdbs.models.UserDatabase>` instance or None for a new user
|
|
|
|
database
|
|
|
|
:param kwargs: keyword arguments to be passed to
|
|
|
|
:py:meth:`django.contrib.admin.ModelAdmin.get_form`
|
|
|
|
:return: form instance
|
|
|
|
|
|
|
|
"""
|
|
|
|
defaults = {}
|
|
|
|
if obj is None:
|
2023-02-18 22:46:48 +01:00
|
|
|
defaults.update(
|
|
|
|
{
|
|
|
|
"form": self.add_form,
|
|
|
|
}
|
|
|
|
)
|
2015-01-04 22:27:43 +01:00
|
|
|
defaults.update(kwargs)
|
2023-02-18 22:46:48 +01:00
|
|
|
return super(UserDatabaseAdmin, self).get_form(request, obj, **defaults)
|
2015-01-04 22:27:43 +01:00
|
|
|
|
|
|
|
def get_readonly_fields(self, request, obj=None):
|
|
|
|
"""
|
|
|
|
Make sure that db_name and db_user are not editable for existing user
|
|
|
|
databases.
|
|
|
|
|
|
|
|
:param request: the current HTTP request
|
|
|
|
:param obj: either a :py:class:`User database
|
|
|
|
<userdbs.models.UserDatabase>` instance or None for a new user
|
|
|
|
database
|
|
|
|
:return: a list of fields
|
|
|
|
:rtype: list
|
|
|
|
|
|
|
|
"""
|
|
|
|
if obj:
|
2023-02-18 22:46:48 +01:00
|
|
|
return ["db_name", "db_user"]
|
2015-01-04 22:27:43 +01:00
|
|
|
return []
|
|
|
|
|
|
|
|
def save_model(self, request, obj, form, change):
|
|
|
|
"""
|
|
|
|
Make sure that the database is created in the target database server.
|
|
|
|
|
|
|
|
:param request: the current HTTP request
|
|
|
|
:param obj: a :py:class:`Database user <userdbs.models.DatabaseUser>`
|
|
|
|
instance
|
|
|
|
:param form: the form instance
|
|
|
|
:param boolean change: whether this is a change operation or not
|
|
|
|
|
|
|
|
"""
|
|
|
|
if not change:
|
|
|
|
obj.create_in_database()
|
|
|
|
super(UserDatabaseAdmin, self).save_model(request, obj, form, change)
|
|
|
|
|
|
|
|
def perform_delete_selected(self, request, queryset):
|
|
|
|
"""
|
|
|
|
Action to delete a list of selected user databases.
|
|
|
|
|
|
|
|
This action calls the delete method of each selected user database in
|
|
|
|
contrast to the default `delete_selected`
|
|
|
|
|
|
|
|
:param request: the current HTTP request
|
|
|
|
:param queryset: Django ORM queryset representing the selected user
|
|
|
|
databases
|
|
|
|
|
|
|
|
"""
|
|
|
|
for database in queryset.all():
|
|
|
|
database.delete()
|
2023-02-18 22:46:48 +01:00
|
|
|
|
|
|
|
perform_delete_selected.short_description = _("Delete selected user databases")
|
2015-01-04 22:27:43 +01:00
|
|
|
|
|
|
|
def get_actions(self, request):
|
|
|
|
"""
|
|
|
|
Get the available actions for user databases.
|
|
|
|
|
|
|
|
This overrides the default behavior to remove the default
|
|
|
|
`delete_selected` action.
|
|
|
|
|
|
|
|
:param request: the current HTTP request
|
|
|
|
:return: list of actions
|
|
|
|
:rtype: list
|
|
|
|
|
|
|
|
"""
|
|
|
|
actions = super(UserDatabaseAdmin, self).get_actions(request)
|
2023-02-18 22:46:48 +01:00
|
|
|
if "delete_selected" in actions: # pragma: no cover
|
|
|
|
del actions["delete_selected"]
|
2015-01-04 22:27:43 +01:00
|
|
|
return actions
|
|
|
|
|
|
|
|
|
|
|
|
admin.site.register(DatabaseUser, DatabaseUserAdmin)
|
|
|
|
admin.site.register(UserDatabase, UserDatabaseAdmin)
|