Thursday, August 22, 2019

Custom action with security

Hi everyone

I am using a custom action in admin, it is simple but not secure. I would like some custom actions would work only for some users and i think it could be done with a different url and a differente authorization role (in Admin).

Could you give me some advice to make this issue work on my admin in a different URL?

I am using Django-rest-framework.

My implementation was quite easy (but I need to have access to this action for some specific users in Admin and Postman).

model.py:
@action(methods=['put'], detail=True, permission_classes=[IsAdminUser], url_path='aprovarArtigo', url_name='aprovar_artigo')
def approveArtcle(modeladmin, request, queryset):
queryset.update(fl_approved=True)

@action(methods=['put'], detail=True, permission_classes=[IsAdminUser], url_path='desaprovarArtigo', url_name='desaprovar_artigo')
def disapproveArtcle(modeladmin, request, queryset):
queryset.update(fl_approved=False)

I tried to put the commented line to work but this simple not work.

model.py:
class AprovarArtigoAdmin(admin.ModelAdmin):
list_display = ('id', 'nu_usuario_id', 'de_titulo', 'de_texto', 'fl_aprovado', 'dt_inclusao', 'dt_alteracao', 'nu_usuario_alteracao' )
readonly_fields = ['nu_usuario_alteracao', 'fl_aprovado']
actions = [approveArticle, disapproveArticle]
approveArticle.short_description = "Approve selected Articles"
disapproveArticle.short_description = "Disapprove selected Articles"
list_filter = ('fl_approved',)

admin.site.register(Article, ArticleAdmin)
#admin.site.register(ApproveArticleAdmin,)

I got the error:
TypeError: 'MediaDefiningClass' object is not iterable
Regards
Ezequias

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/2dfe41b9-d79d-4750-82d7-564c28af43a3%40googlegroups.com.

No comments:

Post a Comment