models.py
3.31 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.dispatch import receiver
# Create your models here.
class Organismo(models.Model):
class Meta:
verbose_name = 'Organismo'
verbose_name_plural = 'Organismos'
short_name = models.CharField(max_length=350, blank=False, verbose_name='Nombre del organismo')
def __str__(self):
return f'{self.short_name}'
def custom_permission_organismo_create(organismo):
content_type = ContentType.objects.get_for_model(Organismo)
action = 'view'
codename = f'{action}_{organismo.short_name.lower().replace(" ", "_")}'
if not Permission.objects.filter(codename=codename, content_type=content_type).exists():
Permission.objects.create(
codename=codename,
name=f'Can {action} {organismo.short_name}',
content_type=content_type,
)
def custom_permission_organismo_delete(organismo):
content_type = ContentType.objects.get_for_model(Organismo)
action = 'view'
codename = f'{action}_{organismo.short_name.lower().replace(" ", "_")}'
Permission.objects.filter(codename=codename, content_type=content_type).delete()
@receiver(post_save, sender=Organismo)
def manage_organismo_permissions(sender, instance, created, **kwargs):
if created:
custom_permission_organismo_create(instance)
else:
old_instance = Organismo.objects.get(pk=instance.pk)
if old_instance.short_name != instance.short_name:
custom_permission_organismo_create(old_instance)
custom_permission_organismo_delete(instance)
class Dependencia(models.Model):
objects = None
class Meta:
verbose_name = 'Dependencia'
verbose_name_plural = 'Dependencias'
organismo = models.ForeignKey('Organismo', on_delete=models.CASCADE)
short_name = models.CharField(max_length=350, blank=False, verbose_name='Nombre de la dependencia')
def __str__(self):
return f'{self.organismo} - {self.short_name}'
def custom_permission_dependencia_create(dependencia):
content_type = ContentType.objects.get_for_model(dependencia)
action = 'view'
codename = f'{action}_{dependencia.short_name.lower().replace(" ", "_")}'
if not Permission.objects.filter(codename=codename, content_type=content_type).exists():
Permission.objects.create(
codename=codename,
name=f'Can {action} {dependencia.short_name}',
content_type=content_type,
)
def custom_permissions_dependencia_delete(dependencia):
content_type = ContentType.objects.get_for_model(dependencia)
action = 'view'
codename = f'{action}_{dependencia.short_name.lower().replace(" ", "_")}'
Permission.objects.filter(codename=codename, content_type=content_type).delete()
@receiver(post_save, sender=Dependencia)
def manage_dependencia_permissions(sender, instance, created, **kwargs):
if created:
custom_permission_dependencia_create(instance)
else:
old_instance = Organismo.objects.get(pk=instance.pk)
if old_instance.short_name != instance.short_name:
custom_permissions_dependencia_delete(old_instance)
custom_permission_dependencia_create(instance)