From acad550dd0fa6b289ee7517c6944a5dd88fd00c9 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Thu, 19 Oct 2023 15:20:06 +0200 Subject: [PATCH] add multiselect for roles in services --- idhub/admin/forms.py | 2 ++ .../0005_remove_service_rol_service_rol.py | 21 +++++++++++++++++++ idhub/models.py | 7 ++++--- idhub/templates/idhub/admin/people.html | 2 +- idhub/templates/idhub/admin/services.html | 2 +- idhub/templates/idhub/admin/user.html | 2 +- idhub/templates/idhub/admin/user_edit.html | 2 +- 7 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 idhub/migrations/0005_remove_service_rol_service_rol.py diff --git a/idhub/admin/forms.py b/idhub/admin/forms.py index 74046df..fc66f14 100644 --- a/idhub/admin/forms.py +++ b/idhub/admin/forms.py @@ -1,5 +1,6 @@ from django import forms from django.contrib.auth.models import User +from idhub.models import Rol class ProfileForm(forms.ModelForm): @@ -21,5 +22,6 @@ class RolForm(forms.ModelForm): class ServiceForm(forms.ModelForm): MANDATORY_FIELDS = ['domain', 'rol'] + class UserRolForm(forms.ModelForm): MANDATORY_FIELDS = ['service'] diff --git a/idhub/migrations/0005_remove_service_rol_service_rol.py b/idhub/migrations/0005_remove_service_rol_service_rol.py new file mode 100644 index 0000000..a383ca4 --- /dev/null +++ b/idhub/migrations/0005_remove_service_rol_service_rol.py @@ -0,0 +1,21 @@ +# Generated by Django 4.2.5 on 2023-10-19 13:01 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + dependencies = [ + ("idhub", "0004_userrol"), + ] + + operations = [ + migrations.RemoveField( + model_name="service", + name="rol", + ), + migrations.AddField( + model_name="service", + name="rol", + field=models.ManyToManyField(to="idhub.rol"), + ), + ] diff --git a/idhub/models.py b/idhub/models.py index 6c143b7..3c187b3 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -92,12 +92,13 @@ class Rol(models.Model): class Service(models.Model): domain = models.CharField(max_length=250) description = models.CharField(max_length=250) - rol = models.ForeignKey( + rol = models.ManyToManyField( Rol, - on_delete=models.CASCADE, - related_name='services', ) + def get_roles(self): + return ", ".join([x.name for x in self.rol.all()]) + def __str__(self): return "{} -> {}".format(self.domain, self.rol.name) diff --git a/idhub/templates/idhub/admin/people.html b/idhub/templates/idhub/admin/people.html index 69cd758..ff19975 100644 --- a/idhub/templates/idhub/admin/people.html +++ b/idhub/templates/idhub/admin/people.html @@ -31,7 +31,7 @@ {% for r in user.roles.all %} - {{ r.service.rol.name }} + {{ r.service.get_roles }} {% endfor %} {% trans 'View' %} diff --git a/idhub/templates/idhub/admin/services.html b/idhub/templates/idhub/admin/services.html index 11f7c0b..92fe5b1 100644 --- a/idhub/templates/idhub/admin/services.html +++ b/idhub/templates/idhub/admin/services.html @@ -24,7 +24,7 @@ {{ service.domain }} {{ service.description }} - {{ service.rol.name }} + {{ service.get_roles }} diff --git a/idhub/templates/idhub/admin/user.html b/idhub/templates/idhub/admin/user.html index 30a0041..3bee80f 100644 --- a/idhub/templates/idhub/admin/user.html +++ b/idhub/templates/idhub/admin/user.html @@ -92,7 +92,7 @@ {% for rol in object.roles.all %} - {{ rol.service.rol.name }} + {{ rol.service.get_roles }} {{ rol.service.description }} {{ rol.service.domain }} diff --git a/idhub/templates/idhub/admin/user_edit.html b/idhub/templates/idhub/admin/user_edit.html index 4752a3b..e162723 100644 --- a/idhub/templates/idhub/admin/user_edit.html +++ b/idhub/templates/idhub/admin/user_edit.html @@ -80,7 +80,7 @@ {% for rol in object.roles.all %} - {{ rol.service.rol.name }} + {{ rol.service.get_roles }} {{ rol.service.description }} {{ rol.service.domain }}