From 03caee6c8d73be69f4ead4cdf3d89681d140df93 Mon Sep 17 00:00:00 2001 From: AlexandreJunod Date: Tue, 4 Jun 2024 12:12:41 +0200 Subject: [PATCH] =?UTF-8?q?add=20in=201.1=20Entit=C3=A9s=20list=20of=20int?= =?UTF-8?q?egrators,=20pilots=20and=20validators?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- geocity/apps/accounts/admin.py | 51 +++++++++++++++++++++++++++++++++ geocity/apps/accounts/models.py | 33 +++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/geocity/apps/accounts/admin.py b/geocity/apps/accounts/admin.py index 22300ba77..405607c97 100644 --- a/geocity/apps/accounts/admin.py +++ b/geocity/apps/accounts/admin.py @@ -677,14 +677,52 @@ def get_sites_field(user): ) +class ReadOnlyTextInput(forms.TextInput): + def render(self, name, value, attrs=None, renderer=None): + return value + + class AdministrativeEntityAdminForm(forms.ModelForm): """Form class to configure an administrative entity (commune, organisation)""" + integrator_users = forms.CharField( + required=False, label="Intégrateurs", widget=forms.HiddenInput() + ) + + pilot_users = forms.CharField( + required=False, label="Pilotes", widget=forms.HiddenInput() + ) + + validator_users = forms.CharField( + required=False, label="Validateurs", widget=forms.HiddenInput() + ) + + def get_integrator_users(self): + return self.instance.get_integrator_users() + + def get_pilot_users(self): + return self.instance.get_pilot_users() + + def get_validator_users(self): + return self.instance.get_validator_users() + def __init__(self, *args, **kwargs): user = kwargs.pop("user") super().__init__(*args, **kwargs) self.fields["sites"] = get_sites_field(user) + instance = kwargs.get("instance") + if instance: + # Integrator users + self.fields["integrator_users"].widget = ReadOnlyTextInput() + self.initial["integrator_users"] = self.get_integrator_users() + # Pilot users + self.fields["pilot_users"].widget = ReadOnlyTextInput() + self.initial["pilot_users"] = self.get_pilot_users() + # Validator users + self.fields["validator_users"].widget = ReadOnlyTextInput() + self.initial["validator_users"] = self.get_validator_users() + class Meta: model = models.AdministrativeEntityForAdminSite fields = [ @@ -710,6 +748,9 @@ class Meta: "services_fees_hourly_rate", "geom", "integrator", + "integrator_users", + "pilot_users", + "validator_users", ] exclude = ["enabled_status"] widgets = { @@ -845,6 +886,16 @@ class AdministrativeEntityAdmin(IntegratorFilterMixin, admin.ModelAdmin): ), }, ), + ( + _("Utilisateurs"), + { + "fields": ( + "integrator_users", + "pilot_users", + "validator_users", + ), + }, + ), ) # Pass the user from ModelAdmin to ModelForm diff --git a/geocity/apps/accounts/models.py b/geocity/apps/accounts/models.py index bc208a22a..0f1ad8aa7 100644 --- a/geocity/apps/accounts/models.py +++ b/geocity/apps/accounts/models.py @@ -13,7 +13,9 @@ from django.db.models import BooleanField, Count, ExpressionWrapper, Q, UniqueConstraint from django.db.models.signals import post_save from django.dispatch import receiver +from django.urls import reverse from django.utils.functional import cached_property +from django.utils.safestring import mark_safe from django.utils.translation import gettext_lazy as _ from simple_history.models import HistoricalRecords from taggit.managers import TaggableManager @@ -412,6 +414,37 @@ def create_anonymous_user(self): zipcode=settings.ANONYMOUS_USER_ZIPCODE, ) + def get_user_list(self, users): + if users: + list_content = [] + for user in users: + url = reverse("admin:auth_user_change", kwargs={"object_id": user.id}) + list_content.append( + f"
  • {user.get_full_name()}
  • " + ) + list_html = "\n".join(list_content) + return mark_safe(f"") + else: + return f"" + + def get_integrator_users(self): + integrator_users = User.objects.filter(groups=self.integrator) + return self.get_user_list(integrator_users) + + def get_pilot_users(self): + pilot_users = User.objects.filter( + groups__permit_department__administrative_entity=self.pk, + groups__permit_department__is_backoffice=True, + ) + return self.get_user_list(pilot_users) + + def get_validator_users(self): + validator_users = User.objects.filter( + groups__permit_department__administrative_entity=self.pk, + groups__permit_department__is_validator=True, + ) + return self.get_user_list(validator_users) + def clean(self): from geocity.apps.forms.models import Form from geocity.apps.submissions.models import Submission