2014-11-12 12:45:37 +00:00
|
|
|
from functools import partial
|
|
|
|
|
|
|
|
from django import forms
|
2015-03-26 16:00:30 +00:00
|
|
|
from django.contrib import messages, admin
|
|
|
|
from django.core.exceptions import PermissionDenied
|
2014-11-12 12:45:37 +00:00
|
|
|
from django.db import transaction
|
|
|
|
from django.shortcuts import render
|
|
|
|
from django.utils.safestring import mark_safe
|
|
|
|
from django.utils.text import capfirst
|
|
|
|
from django.utils.translation import ungettext, ugettext_lazy as _
|
|
|
|
|
|
|
|
from orchestra.admin.decorators import action_with_confirmation
|
|
|
|
from orchestra.admin.utils import change_url
|
2014-11-14 16:52:54 +00:00
|
|
|
from orchestra.apps.orchestration.models import BackendOperation as Operation
|
2014-11-12 12:45:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GrantPermissionForm(forms.Form):
|
|
|
|
base_path = forms.ChoiceField(label=_("Grant access to"), choices=(('hola', 'hola'),),
|
|
|
|
help_text=_("User will be granted access to this directory."))
|
|
|
|
path_extension = forms.CharField(label='', required=False)
|
|
|
|
read_only = forms.BooleanField(label=_("Read only"), initial=False, required=False,
|
|
|
|
help_text=_("Designates whether the permissions granted will be read-only or read/write."))
|
|
|
|
|
|
|
|
|
|
|
|
@action_with_confirmation(extra_context=dict(form=GrantPermissionForm()))
|
|
|
|
def grant_permission(modeladmin, request, queryset):
|
2014-11-14 16:52:54 +00:00
|
|
|
user = queryset.get()
|
|
|
|
log = Operation.execute_action(user, 'grant_permission')
|
2014-11-12 12:45:37 +00:00
|
|
|
# TODO
|
|
|
|
grant_permission.url_name = 'grant-permission'
|
|
|
|
grant_permission.verbose_name = _("Grant 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")
|
|
|
|
|