2015-05-08 14:05:57 +00:00
|
|
|
import os
|
|
|
|
|
2015-03-26 16:00:30 +00:00
|
|
|
from django.contrib import messages, admin
|
|
|
|
from django.core.exceptions import PermissionDenied
|
2015-05-08 14:05:57 +00:00
|
|
|
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
|
|
|
|
2015-05-11 14:05:39 +00:00
|
|
|
from .forms import PermissionForm
|
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):
|
2015-05-08 14:05:57 +00:00
|
|
|
account_id = None
|
|
|
|
for user in queryset:
|
|
|
|
account_id = account_id or user.account_id
|
|
|
|
if user.account_id != account_id:
|
|
|
|
messages.error("Users from the same account should be selected.")
|
|
|
|
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'
|
|
|
|
if request.POST.get('action') == action_value:
|
2015-05-11 14:05:39 +00:00
|
|
|
form = PermissionForm(user, request.POST)
|
2015-05-08 14:05:57 +00:00
|
|
|
if form.is_valid():
|
|
|
|
cleaned_data = form.cleaned_data
|
2015-05-11 14:05:39 +00:00
|
|
|
operations = []
|
2015-05-08 14:05:57 +00:00
|
|
|
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']
|
|
|
|
user.set_permission(base_home, extension, action=action, 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)
|
2015-05-08 14:05:57 +00:00
|
|
|
context = {
|
2015-05-11 14:05:39 +00:00
|
|
|
'action': verbose_action,
|
|
|
|
'perms': verbose_permissions,
|
|
|
|
'to': os.path.join(user.set_perm_base_home, user.set_perm_home_extension),
|
2015-05-08 14:05:57 +00:00
|
|
|
}
|
2015-05-11 14:05:39 +00:00
|
|
|
msg = _("%(action)s %(perms)s permission to %(to)s") % context
|
2015-05-08 14:05:57 +00:00
|
|
|
modeladmin.log_change(request, user, msg)
|
2015-05-11 14:05:39 +00:00
|
|
|
logs = Operation.execute(operations)
|
|
|
|
helpers.message_user(request, logs)
|
2015-05-08 14:05:57 +00:00
|
|
|
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,
|
2015-05-08 14:05:57 +00:00
|
|
|
'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-08 14:05:57 +00:00
|
|
|
}
|
2015-05-11 14:05:39 +00:00
|
|
|
return TemplateResponse(request, 'admin/systemusers/systemuser/set_permission.html',
|
2015-05-08 14:05:57 +00:00
|
|
|
context, current_app=modeladmin.admin_site.name)
|
2015-05-11 14:05:39 +00:00
|
|
|
set_permission.url_name = 'set-permission'
|
2015-07-20 12:51:30 +00:00
|
|
|
set_permission.tool_description = _("Set permission")
|
2015-03-26 16:00:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
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")
|