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

Commit

Permalink
structures: ajout de champs dans le fichier CSV d'import
Browse files Browse the repository at this point in the history
Ajout de nouveaux champs de structure importables depuis le fichier CSV
d'import :
- téléphone
- e-mail
  • Loading branch information
ikarius committed Jun 28, 2024
1 parent a2c72c0 commit 64b1ddd
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 48 deletions.
45 changes: 31 additions & 14 deletions dora/core/management/commands/import_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from dora.core.models import ModerationStatus
from dora.core.notify import send_moderation_notification
from dora.core.validators import validate_siret
from dora.core.validators import validate_phone_number, validate_siret
from dora.services.models import ServiceModel
from dora.services.utils import instantiate_model
from dora.sirene.models import Establishment
Expand All @@ -26,8 +26,8 @@
# Les administrateurs proposés ne seront ajoutés que s’il n’y a pas déjà
# un administrateur
#
# Format du CSV attendu:
# | nom | siret | siret_parent | courriels_administrateurs | labels | modeles |
# Format du CSV attendu (entête):
# | nom | siret | siret_parent | courriels_administrateurs | labels | modeles | telephone | courriel_structure


def to_string_array(strings_list):
Expand All @@ -44,15 +44,20 @@ class ImportSerializer(serializers.Serializer):
admins = serializers.ListField(child=serializers.EmailField(), allow_empty=True)
labels = serializers.ListField(child=serializers.CharField(), allow_empty=True)
models = serializers.ListField(child=serializers.CharField(), allow_empty=True)
phone = serializers.CharField(allow_blank=True, validators=[validate_phone_number])
email = serializers.EmailField(allow_blank=True)

def _clean_siret(self, siret: str):
return "".join([c for c in siret if c.isdigit()])
def _clean_siret_or_phone(self, siret_or_phone: str):
if not siret_or_phone:
return ""
return "".join([c for c in siret_or_phone if c.isdigit()])

def to_internal_value(self, data):
# nettoyage pré-validation
data |= {
"siret": self._clean_siret(data["siret"]),
"parent_siret": self._clean_siret(data["parent_siret"]),
"siret": self._clean_siret_or_phone(data["siret"]),
"phone": self._clean_siret_or_phone(data["phone"]),
"parent_siret": self._clean_siret_or_phone(data["parent_siret"]),
}

return super().to_internal_value(data)
Expand Down Expand Up @@ -154,6 +159,10 @@ def handle(self, *args, **options):
"admins": to_string_array(row["courriels_administrateurs"]),
"labels": to_string_array(row["labels"]),
"models": to_string_array(row["modeles"]),
# champs optionnels correspondant directement
# à un champ du modèle structure
"phone": row.get("telephone", ""),
"email": row.get("courriel_structure", ""),
}
)

Expand All @@ -169,6 +178,8 @@ def handle(self, *args, **options):
data["name"],
data["siret"],
data["parent_siret"],
phone=data.get("phone"),
email=data.get("email"),
)
self.stdout.write(f"{structure.get_frontend_url()}")
self.invite_users(structure, data["admins"])
Expand All @@ -184,14 +195,17 @@ def get_or_create_structure(
name,
siret,
parent_siret,
**kwargs,
):
if parent_siret:
parent_structure = self._get_or_create_structure_from_siret(
parent_siret, is_parent=True
parent_siret, is_parent=True, **kwargs
)
structure = self._get_or_create_branch(
name, siret, parent_structure, **kwargs
)
structure = self._get_or_create_branch(name, siret, parent_structure)
else:
structure = self._get_or_create_structure_from_siret(siret)
structure = self._get_or_create_structure_from_siret(siret, **kwargs)

return structure

Expand Down Expand Up @@ -246,7 +260,7 @@ def create_services(self, structure, models):
f"Ajout du service {service.name} ({service.get_frontend_url()})"
)

def _get_or_create_branch(self, name, siret, parent_structure):
def _get_or_create_branch(self, name, siret, parent_structure, **kwargs):
try:
if siret:
branch = Structure.objects.get(siret=siret)
Expand All @@ -260,12 +274,13 @@ def _get_or_create_branch(self, name, siret, parent_structure):
if siret:
establishment = Establishment.objects.get(siret=siret)
branch = Structure.objects.create_from_establishment(
establishment, name, parent_structure
establishment, name, parent_structure, **kwargs
)
else:
branch = Structure.objects.create(
name=name,
parent=parent_structure,
**kwargs,
)
parent_structure.post_create_branch(branch, self.bot_user, self.source)

Expand All @@ -280,15 +295,17 @@ def _get_or_create_branch(self, name, siret, parent_structure):
)
return branch

def _get_or_create_structure_from_siret(self, siret, is_parent=False):
def _get_or_create_structure_from_siret(self, siret, is_parent=False, **kwargs):
try:
structure = Structure.objects.get(siret=siret)
self.stdout.write(
f"La structure {'parente' if is_parent else ''} {structure.name} ({structure.get_frontend_url()}) existe déjà"
)
except Structure.DoesNotExist:
establishment = Establishment.objects.get(siret=siret)
structure = Structure.objects.create_from_establishment(establishment)
structure = Structure.objects.create_from_establishment(
establishment, **kwargs
)
structure.creator = self.bot_user
structure.last_editor = self.bot_user
structure.source = self.source
Expand Down
Loading

0 comments on commit 64b1ddd

Please sign in to comment.