admin.py 1.58 KB
from django.contrib import admin

from .models import Organismo, Dependencia

# Register your models here.


@admin.register(Organismo)
class OrganismoAdmin(admin.ModelAdmin):
    model = Organismo
    list_display = ('id', 'short_name')
    list_filter = ('short_name',)
    search_fields = ('short_name', )

    def has_view_permission(self, request, obj=None):
        if obj is not None:
            return request.user.has_perm(f'organismo.view_{obj.short_name.lower().replace(" ", "_")}')
        return request.user.has_perm('organismo.view_organismo')

    def has_add_permission(self, request):
        return request.user.has_perm('organismo.create_organismo')

    def has_change_permission(self, request, obj=None):
        if obj is not None:
            return request.user.has_perm(f'organismo.edit_{obj.short_name.lower().replace(" ", "_")}')
        return request.user.has_perm('organismo.edit_organismo')

    def has_delete_permission(self, request, obj=None):
        if obj is not None:
            return request.user.has_perm(f'organismo.delete_{obj.short_name.lower().replace(" ", "_")}')
        return request.user.has_perm('organismo.delete_organismo')


@admin.register(Dependencia)
class DependenciaAdmin(admin.ModelAdmin):
    model = Dependencia
    list_display = ('id', 'organismo', 'short_name',)
    list_filter = ('organismo', 'short_name',)
    search_fields = ('short_name',)

    def get_queryset(self, request):
        queryset = super().get_queryset(request)

        if not request.user.is_superuser:
            user_groups = request.user.groups.all()

        return queryset