2023-10-24 16:59:02 +00:00
|
|
|
from django.urls import re_path as url
|
2014-05-08 16:59:35 +00:00
|
|
|
from django.contrib import admin
|
|
|
|
from django.contrib.auth.admin import UserAdmin
|
2021-05-20 11:58:16 +00:00
|
|
|
from django.utils.html import format_html
|
|
|
|
from django.utils.safestring import mark_safe
|
2023-10-24 16:59:02 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2014-10-14 13:50:19 +00:00
|
|
|
from orchestra.admin import ExtendedModelAdmin, ChangePasswordAdminMixin
|
2015-04-04 18:10:39 +00:00
|
|
|
from orchestra.admin.utils import change_url
|
2015-07-21 10:44:32 +00:00
|
|
|
from orchestra.contrib.accounts.actions import list_accounts
|
2015-04-05 10:46:24 +00:00
|
|
|
from orchestra.contrib.accounts.admin import SelectAccountAdminMixin
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2016-03-09 11:16:28 +00:00
|
|
|
from .filters import HasUserListFilter, HasDatabaseListFilter
|
2023-10-24 16:59:02 +00:00
|
|
|
from .forms import DatabaseCreationForm, DatabaseUserChangeForm, DatabaseUserCreationForm, DatabaseForm
|
2014-10-14 13:50:19 +00:00
|
|
|
from .models import Database, DatabaseUser
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2021-01-13 15:41:36 +00:00
|
|
|
def save_selected(modeladmin, request, queryset):
|
|
|
|
for selected in queryset:
|
|
|
|
selected.save()
|
|
|
|
save_selected.short_description = "Re-save selected objects"
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
class DatabaseAdmin(SelectAccountAdminMixin, ExtendedModelAdmin):
|
2014-10-20 15:51:24 +00:00
|
|
|
list_display = ('name', 'type', 'display_users', 'account_link')
|
2016-03-09 11:16:28 +00:00
|
|
|
list_filter = ('type', HasUserListFilter)
|
2015-04-24 11:39:20 +00:00
|
|
|
search_fields = ('name', 'account__username')
|
2023-10-24 16:59:02 +00:00
|
|
|
change_readonly_fields = ('name', 'type', 'target_server')
|
2014-05-08 16:59:35 +00:00
|
|
|
extra = 1
|
|
|
|
fieldsets = (
|
|
|
|
(None, {
|
|
|
|
'classes': ('extrapretty',),
|
2023-10-24 16:59:02 +00:00
|
|
|
'fields': ('account_link', 'name', 'type', 'users', 'display_users', 'comments', 'target_server'),
|
2014-05-08 16:59:35 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
add_fieldsets = (
|
|
|
|
(None, {
|
|
|
|
'classes': ('wide',),
|
2023-10-24 16:59:02 +00:00
|
|
|
'fields': ('account_link', 'name', 'type', 'target_server')
|
2014-05-08 16:59:35 +00:00
|
|
|
}),
|
|
|
|
(_("Create new user"), {
|
|
|
|
'classes': ('wide',),
|
|
|
|
'fields': ('username', 'password1', 'password2'),
|
|
|
|
}),
|
|
|
|
(_("Use existing user"), {
|
|
|
|
'classes': ('wide',),
|
|
|
|
'fields': ('user',)
|
|
|
|
}),
|
|
|
|
)
|
2023-10-24 16:59:02 +00:00
|
|
|
form = DatabaseForm
|
2014-05-08 16:59:35 +00:00
|
|
|
add_form = DatabaseCreationForm
|
2014-10-20 15:51:24 +00:00
|
|
|
readonly_fields = ('account_link', 'display_users',)
|
|
|
|
filter_horizontal = ['users']
|
2023-10-24 16:59:02 +00:00
|
|
|
# filter_by_account_fields = ('users',)
|
|
|
|
# list_prefetch_related = ('users',)
|
2021-01-13 15:41:36 +00:00
|
|
|
actions = (list_accounts, save_selected)
|
2021-05-20 11:58:16 +00:00
|
|
|
|
|
|
|
@mark_safe
|
2014-10-20 15:51:24 +00:00
|
|
|
def display_users(self, db):
|
|
|
|
links = []
|
|
|
|
for user in db.users.all():
|
2021-05-20 11:58:16 +00:00
|
|
|
link = format_html('<a href="{}">{}</a>', change_url(user), user.username)
|
2014-10-20 15:51:24 +00:00
|
|
|
links.append(link)
|
2014-11-02 14:33:55 +00:00
|
|
|
return '<br>'.join(links)
|
2014-10-20 15:51:24 +00:00
|
|
|
display_users.short_description = _("Users")
|
|
|
|
display_users.admin_order_field = 'users__username'
|
2021-05-20 11:58:16 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def save_model(self, request, obj, form, change):
|
|
|
|
super(DatabaseAdmin, self).save_model(request, obj, form, change)
|
|
|
|
if not change:
|
|
|
|
user = form.cleaned_data['user']
|
|
|
|
if not user:
|
2014-10-09 17:04:12 +00:00
|
|
|
user = DatabaseUser(
|
2014-05-08 16:59:35 +00:00
|
|
|
username=form.cleaned_data['username'],
|
|
|
|
type=obj.type,
|
2014-10-14 13:50:19 +00:00
|
|
|
account_id=obj.account.pk,
|
2023-10-24 16:59:02 +00:00
|
|
|
target_server=form.cleaned_data['target_server'],
|
2014-05-08 16:59:35 +00:00
|
|
|
)
|
|
|
|
user.set_password(form.cleaned_data["password1"])
|
|
|
|
user.save()
|
2014-10-14 13:50:19 +00:00
|
|
|
obj.users.add(user)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
2014-10-14 13:50:19 +00:00
|
|
|
class DatabaseUserAdmin(SelectAccountAdminMixin, ChangePasswordAdminMixin, ExtendedModelAdmin):
|
2023-10-24 16:59:02 +00:00
|
|
|
list_display = ('username', 'target_server', 'type', 'display_databases', 'account_link')
|
2016-03-09 11:16:28 +00:00
|
|
|
list_filter = ('type', HasDatabaseListFilter)
|
2015-04-24 11:39:20 +00:00
|
|
|
search_fields = ('username', 'account__username')
|
2014-05-08 16:59:35 +00:00
|
|
|
form = DatabaseUserChangeForm
|
|
|
|
add_form = DatabaseUserCreationForm
|
2023-10-24 16:59:02 +00:00
|
|
|
change_readonly_fields = ('username', 'type', 'target_server')
|
2014-05-08 16:59:35 +00:00
|
|
|
fieldsets = (
|
|
|
|
(None, {
|
|
|
|
'classes': ('extrapretty',),
|
2023-10-24 16:59:02 +00:00
|
|
|
'fields': ('account_link', 'username', 'password', 'type', 'display_databases', 'target_server', 'permision')
|
2014-05-08 16:59:35 +00:00
|
|
|
}),
|
|
|
|
)
|
|
|
|
add_fieldsets = (
|
|
|
|
(None, {
|
|
|
|
'classes': ('extrapretty',),
|
2023-10-24 16:59:02 +00:00
|
|
|
'fields': ('account_link', 'username', 'password1', 'password2', 'type', 'target_server', 'permision')
|
2014-05-08 16:59:35 +00:00
|
|
|
}),
|
|
|
|
)
|
2014-10-20 15:51:24 +00:00
|
|
|
readonly_fields = ('account_link', 'display_databases',)
|
2014-11-02 14:33:55 +00:00
|
|
|
filter_by_account_fields = ('databases',)
|
2014-11-20 16:48:50 +00:00
|
|
|
list_prefetch_related = ('databases',)
|
2021-01-13 15:41:36 +00:00
|
|
|
actions = (list_accounts, save_selected)
|
2021-05-20 11:58:16 +00:00
|
|
|
|
|
|
|
@mark_safe
|
2014-10-20 15:51:24 +00:00
|
|
|
def display_databases(self, user):
|
|
|
|
links = []
|
|
|
|
for db in user.databases.all():
|
2021-05-20 11:58:16 +00:00
|
|
|
link = format_html('<a href="{}">{}</a>', change_url(db), db.name)
|
2014-10-20 15:51:24 +00:00
|
|
|
links.append(link)
|
2014-11-02 14:33:55 +00:00
|
|
|
return '<br>'.join(links)
|
2014-10-20 15:51:24 +00:00
|
|
|
display_databases.short_description = _("Databases")
|
|
|
|
display_databases.admin_order_field = 'databases__name'
|
2021-05-20 11:58:16 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
def get_urls(self):
|
|
|
|
useradmin = UserAdmin(DatabaseUser, self.admin_site)
|
2015-05-19 13:27:04 +00:00
|
|
|
return [
|
|
|
|
url(r'^(\d+)/password/$',
|
|
|
|
self.admin_site.admin_view(useradmin.user_change_password))
|
|
|
|
] + super(DatabaseUserAdmin, self).get_urls()
|
2021-05-20 11:58:16 +00:00
|
|
|
|
2014-10-15 19:29:58 +00:00
|
|
|
def save_model(self, request, obj, form, change):
|
|
|
|
""" set password """
|
|
|
|
if not change:
|
|
|
|
obj.set_password(form.cleaned_data["password1"])
|
|
|
|
super(DatabaseUserAdmin, self).save_model(request, obj, form, change)
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
admin.site.register(Database, DatabaseAdmin)
|
|
|
|
admin.site.register(DatabaseUser, DatabaseUserAdmin)
|