Skip to content

Commit

Permalink
New presta_type_display utils
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Jun 18, 2024
1 parent f697a9b commit 376906c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
8 changes: 2 additions & 6 deletions lemarche/siaes/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from django.dispatch import receiver
from django.urls import reverse
from django.utils import timezone
from django.utils.encoding import force_str
from django.utils.functional import cached_property
from django.utils.text import slugify
from phonenumber_field.modelfields import PhoneNumberField
Expand All @@ -25,7 +24,7 @@
from lemarche.stats.models import Tracker
from lemarche.users.models import User
from lemarche.utils.constants import DEPARTMENTS_PRETTY, RECALCULATED_FIELD_HELP_TEXT, REGIONS_PRETTY
from lemarche.utils.data import phone_number_display, round_by_base
from lemarche.utils.data import choice_array_to_string, phone_number_display, round_by_base
from lemarche.utils.fields import ChoiceArrayField
from lemarche.utils.urls import get_object_admin_url
from lemarche.utils.validators import validate_naf, validate_post_code, validate_siret
Expand Down Expand Up @@ -1014,10 +1013,7 @@ def presta_type_display(self) -> str:
if self.kind == siae_constants.KIND_AI:
return "Mise à disposition du personnel"
if self.presta_type:
presta_type_values = [
force_str(dict(siae_constants.PRESTA_CHOICES).get(key, "")) for key in self.presta_type
]
return ", ".join(filter(None, presta_type_values))
return choice_array_to_string(siae_constants.PRESTA_CHOICES, self.presta_type)
return ""

@property
Expand Down
11 changes: 11 additions & 0 deletions lemarche/utils/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import phonenumbers
from django.core.management import call_command
from django.db import connection
from django.utils.encoding import force_str


def reset_app_sql_sequences(app_name):
Expand Down Expand Up @@ -33,6 +34,16 @@ def get_choice(choices, key):
return None


def choice_array_to_string(choices, array=[]):
"""
choices = [(1, 'One'), (2, 'Two'), (3, 'Three')]
array = [1, 3]
choice_array_to_string(choices, array) --> 'One, Three'
"""
choice_array_values = [force_str(dict(choices).get(key, "")) for key in array]
return ", ".join(filter(None, choice_array_values))


def round_by_base(x, base=5):
return base * round(x / base)

Expand Down
5 changes: 2 additions & 3 deletions lemarche/utils/templatetags/array_choices_display.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django import template
from django.utils.encoding import force_str
from django.utils.html import mark_safe

from lemarche.siaes import constants as siae_constants
from lemarche.utils.data import choice_array_to_string


register = template.Library()
Expand All @@ -22,8 +22,7 @@ def array_choices_display(obj, field, output_format="string"):
except: # noqa
keys = getattr(obj, field, [])

values = [force_str(choices_dict.get(key, "")) for key in (keys or [])]
values = filter(None, values)
values = choice_array_to_string(choices_dict, keys)

# output format
if output_format == "list":
Expand Down

0 comments on commit 376906c

Please sign in to comment.