django-orchestra-test/orchestra/contrib/systemusers/actions.py

159 lines
6.4 KiB
Python
Raw Normal View History

import os
from django.contrib import messages, admin
from django.core.exceptions import PermissionDenied
from django.template.response import TemplateResponse
2014-11-12 12:45:37 +00:00
from django.utils.translation import ungettext, ugettext_lazy as _
2015-05-11 14:05:39 +00:00
from orchestra.contrib.orchestration import Operation, helpers
2014-11-12 12:45:37 +00:00
2016-02-09 12:17:42 +00:00
from .forms import PermissionForm, LinkForm
2014-11-12 12:45:37 +00:00
2015-05-11 14:05:39 +00:00
def get_verbose_choice(choices, value):
for choice, verbose in choices:
if choice == value:
return verbose
def set_permission(modeladmin, request, queryset):
account_id = None
for user in queryset:
account_id = account_id or user.account_id
if user.account_id != account_id:
2016-02-09 12:17:42 +00:00
messages.error(request, "Users from the same account should be selected.")
return
user = queryset[0]
2015-05-11 14:05:39 +00:00
form = PermissionForm(user)
2015-05-11 14:22:36 +00:00
action_value = 'set_permission'
2016-02-09 12:17:42 +00:00
if request.POST.get('post') == 'generic_confirmation':
2015-05-11 14:05:39 +00:00
form = PermissionForm(user, request.POST)
if form.is_valid():
cleaned_data = form.cleaned_data
2015-05-11 14:05:39 +00:00
operations = []
for user in queryset:
2015-05-13 12:16:51 +00:00
base_home = cleaned_data['base_home']
extension = cleaned_data['home_extension']
action = cleaned_data['set_action']
perms = cleaned_data['permissions']
2016-02-09 12:17:42 +00:00
user.set_perm_action = action
user.set_perm_base_home = base_home
user.set_perm_home_extension = extension
user.set_perm_perms = perms
2015-05-11 14:05:39 +00:00
operations.extend(Operation.create_for_action(user, 'set_permission'))
verbose_action = get_verbose_choice(form.fields['set_action'].choices,
user.set_perm_action)
verbose_permissions = get_verbose_choice(form.fields['permissions'].choices,
user.set_perm_perms)
context = {
2015-05-11 14:05:39 +00:00
'action': verbose_action,
'perms': verbose_permissions.lower(),
2015-05-11 14:05:39 +00:00
'to': os.path.join(user.set_perm_base_home, user.set_perm_home_extension),
}
2015-05-11 14:05:39 +00:00
msg = _("%(action)s %(perms)s permission to %(to)s") % context
modeladmin.log_change(request, user, msg)
2016-02-09 12:17:42 +00:00
if not operations:
messages.error(request, _("No backend operation has been executed."))
2016-02-09 12:17:42 +00:00
else:
logs = Operation.execute(operations)
helpers.message_user(request, logs)
return
opts = modeladmin.model._meta
app_label = opts.app_label
context = {
2015-05-11 14:05:39 +00:00
'title': _("Set permission"),
'action_name': _("Set permission"),
2015-05-11 14:22:36 +00:00
'action_value': action_value,
'queryset': queryset,
'opts': opts,
'obj': user,
'app_label': app_label,
'action_checkbox_name': admin.helpers.ACTION_CHECKBOX_NAME,
2015-05-11 14:05:39 +00:00
'form': form,
}
2015-05-11 14:05:39 +00:00
return TemplateResponse(request, 'admin/systemusers/systemuser/set_permission.html',
context, current_app=modeladmin.admin_site.name)
2015-05-11 14:05:39 +00:00
set_permission.url_name = 'set-permission'
set_permission.tool_description = _("Set permission")
2016-02-09 12:17:42 +00:00
def create_link(modeladmin, request, queryset):
account_id = None
for user in queryset:
account_id = account_id or user.account_id
if user.account_id != account_id:
messages.error(request, "Users from the same account should be selected.")
return
user = queryset[0]
2016-06-17 10:00:04 +00:00
form = LinkForm(user, queryset=queryset)
2016-02-09 12:17:42 +00:00
action_value = 'create_link'
if request.POST.get('post') == 'generic_confirmation':
form = LinkForm(user, request.POST, queryset=queryset)
if form.is_valid():
cleaned_data = form.cleaned_data
operations = []
for user in queryset:
base_home = cleaned_data['base_home']
extension = cleaned_data['home_extension']
target = os.path.join(base_home, extension)
2016-02-10 12:07:55 +00:00
default_name = os.path.join(user.home, os.path.basename(target))
link_name = cleaned_data['link_name'] or default_name
2016-02-09 12:17:42 +00:00
user.create_link_target = target
user.create_link_name = link_name
operations.extend(Operation.create_for_action(user, 'create_link'))
context = {
'target': target,
'link_name': link_name,
}
msg = _("Created link from %(target)s to %(link_name)s") % context
modeladmin.log_change(request, request.user, msg)
logs = Operation.execute(operations)
if logs:
helpers.message_user(request, logs)
else:
messages.error(request, "No backend operation has been executed.")
return
opts = modeladmin.model._meta
app_label = opts.app_label
context = {
'title': _("Create link"),
'action_name': _("Create link"),
'action_value': action_value,
'queryset': queryset,
'opts': opts,
'obj': user,
'app_label': app_label,
'action_checkbox_name': admin.helpers.ACTION_CHECKBOX_NAME,
'form': form,
}
return TemplateResponse(request, 'admin/systemusers/systemuser/create_link.html',
context, current_app=modeladmin.admin_site.name)
create_link.url_name = 'create-link'
create_link.tool_description = _("Create link")
def delete_selected(modeladmin, request, queryset):
""" wrapper arround admin.actions.delete_selected to prevent main system users deletion """
opts = modeladmin.model._meta
app_label = opts.app_label
# Check that the user has delete permission for the actual model
if not modeladmin.has_delete_permission(request):
raise PermissionDenied
else:
accounts = []
for user in queryset:
if user.is_main:
accounts.append(user.username)
if accounts:
n = len(accounts)
messages.error(request, ungettext(
"You have selected one main system user (%(accounts)s), which can not be deleted.",
"You have selected some main system users which can not be deleted (%(accounts)s).",
n) % {
'accounts': ', '.join(accounts[:10]+['...'] if n > 10 else accounts)
}
)
return
return admin.actions.delete_selected(modeladmin, request, queryset)
delete_selected.short_description = _("Delete selected %(verbose_name_plural)s")