admin.py
1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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