Skip to content
This repository has been archived by the owner on Jun 24, 2024. It is now read-only.

Commit

Permalink
add in 1.1 Entités list of integrators, pilots and validators
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandreJunod committed Jun 4, 2024
1 parent ec4d04d commit 03caee6
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 0 deletions.
51 changes: 51 additions & 0 deletions geocity/apps/accounts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -710,6 +748,9 @@ class Meta:
"services_fees_hourly_rate",
"geom",
"integrator",
"integrator_users",
"pilot_users",
"validator_users",
]
exclude = ["enabled_status"]
widgets = {
Expand Down Expand Up @@ -845,6 +886,16 @@ class AdministrativeEntityAdmin(IntegratorFilterMixin, admin.ModelAdmin):
),
},
),
(
_("Utilisateurs"),
{
"fields": (
"integrator_users",
"pilot_users",
"validator_users",
),
},
),
)

# Pass the user from ModelAdmin to ModelForm
Expand Down
33 changes: 33 additions & 0 deletions geocity/apps/accounts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"<li><a href='{url}'>{user.get_full_name()}</a></li>"
)
list_html = "\n".join(list_content)
return mark_safe(f"<ul>{list_html}</ul>")
else:
return f"<ul><li>{_('Aucun utilisateur trouvé')}</li></ul>"

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
Expand Down

0 comments on commit 03caee6

Please sign in to comment.