models.py 3.31 KB
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)