Skip to content

Commit

Permalink
refactor(Diagnostique): renomme les enum végé (MenuType & MenuFrequen…
Browse files Browse the repository at this point in the history
…cy) pour clarifier (#4717)
  • Loading branch information
raphodn authored Dec 9, 2024
1 parent d55a3f4 commit b64642c
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 25 deletions.
4 changes: 2 additions & 2 deletions api/templatetags/diagnostic_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def menu_frequency(key):
if not key:
return None
try:
return Diagnostic.MenuFrequency[key].label
return Diagnostic.VegetarianMenuFrequency[key].label
except Exception:
return None

Expand All @@ -18,6 +18,6 @@ def menu_type(key):
if not key:
return None
try:
return Diagnostic.MenuType[key].label
return Diagnostic.VegetarianMenuType[key].label
except Exception:
return None
18 changes: 11 additions & 7 deletions api/tests/test_canteen_statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def test_canteen_statistics(self):
"value_egalim_others_ht": None,
"has_waste_diagnostic": False,
"waste_actions": [],
"vegetarian_weekly_recurrence": Diagnostic.MenuFrequency.DAILY,
"vegetarian_weekly_recurrence": Diagnostic.VegetarianMenuFrequency.DAILY,
"plastic_tableware_substituted": False,
"communicates_on_food_quality": False,
},
Expand All @@ -64,7 +64,7 @@ def test_canteen_statistics(self):
"value_sustainable_ht": 0,
"value_externality_performance_ht": 0,
"value_egalim_others_ht": 0,
"vegetarian_weekly_recurrence": Diagnostic.MenuFrequency.DAILY,
"vegetarian_weekly_recurrence": Diagnostic.VegetarianMenuFrequency.DAILY,
},
],
},
Expand All @@ -84,7 +84,7 @@ def test_canteen_statistics(self):
"has_waste_diagnostic": True,
"waste_actions": ["action1", "action2"],
"has_donation_agreement": True,
"vegetarian_weekly_recurrence": Diagnostic.MenuFrequency.LOW,
"vegetarian_weekly_recurrence": Diagnostic.VegetarianMenuFrequency.LOW,
"cooking_plastic_substituted": True,
"serving_plastic_substituted": True,
"plastic_bottles_substituted": True,
Expand Down Expand Up @@ -409,23 +409,27 @@ def test_diversification_badge_earned(self):
high_canteen.sectors.remove(primaire)
high_canteen.sectors.remove(secondaire)
DiagnosticFactory.create(
canteen=high_canteen, vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.HIGH.value
canteen=high_canteen, vegetarian_weekly_recurrence=Diagnostic.VegetarianMenuFrequency.HIGH.value
)

low_canteen = CanteenFactory.create()
low_canteen.sectors.add(primaire)
DiagnosticFactory.create(canteen=low_canteen, vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.LOW.value)
DiagnosticFactory.create(
canteen=low_canteen, vegetarian_weekly_recurrence=Diagnostic.VegetarianMenuFrequency.LOW.value
)

# --- canteens which earn diversification badge:
daily_vege = CanteenFactory.create()
daily_vege.sectors.remove(primaire)
daily_vege.sectors.remove(secondaire)
DiagnosticFactory.create(canteen=daily_vege, vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.DAILY.value)
DiagnosticFactory.create(
canteen=daily_vege, vegetarian_weekly_recurrence=Diagnostic.VegetarianMenuFrequency.DAILY.value
)

scolaire_mid_vege = CanteenFactory.create()
scolaire_mid_vege.sectors.add(secondaire)
DiagnosticFactory.create(
canteen=scolaire_mid_vege, vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.MID.value
canteen=scolaire_mid_vege, vegetarian_weekly_recurrence=Diagnostic.VegetarianMenuFrequency.MID.value
)

badges = badges_for_queryset(Diagnostic.objects.all())
Expand Down
8 changes: 4 additions & 4 deletions api/views/canteen.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,19 +828,19 @@ def badges_for_queryset(diagnostic_year_queryset):
diversification_badge_query = diagnostic_year_queryset.exclude(vegetarian_weekly_recurrence__isnull=True)
diversification_badge_query = diversification_badge_query.exclude(vegetarian_weekly_recurrence="")
diversification_badge_query = diversification_badge_query.exclude(
vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.LOW
vegetarian_weekly_recurrence=Diagnostic.VegetarianMenuFrequency.LOW
)
scolaire_sectors = Sector.objects.filter(category="education")
if scolaire_sectors.count():
diversification_badge_query = diversification_badge_query.filter(
Q(
canteen__sectors__in=scolaire_sectors,
vegetarian_weekly_recurrence__in=[
Diagnostic.MenuFrequency.MID,
Diagnostic.MenuFrequency.HIGH,
Diagnostic.VegetarianMenuFrequency.MID,
Diagnostic.VegetarianMenuFrequency.HIGH,
],
)
| Q(vegetarian_weekly_recurrence=Diagnostic.MenuFrequency.DAILY)
| Q(vegetarian_weekly_recurrence=Diagnostic.VegetarianMenuFrequency.DAILY)
).distinct()
badge_querysets["diversification"] = diversification_badge_query

Expand Down
4 changes: 2 additions & 2 deletions api/views/teledeclaration.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,12 @@ def _get_teledeclaration_override_data(teledeclaration_data):
for x in (teledeclaration_data.get("diversification_plan_actions") or [])
],
"vegetarian_weekly_recurrence": (
Diagnostic.MenuFrequency(teledeclaration_data["vegetarian_weekly_recurrence"]).label
Diagnostic.VegetarianMenuFrequency(teledeclaration_data["vegetarian_weekly_recurrence"]).label
if teledeclaration_data.get("vegetarian_weekly_recurrence")
else None
),
"vegetarian_menu_type": (
Diagnostic.MenuType(teledeclaration_data["vegetarian_menu_type"]).label
Diagnostic.VegetarianMenuType(teledeclaration_data["vegetarian_menu_type"]).label
if teledeclaration_data.get("vegetarian_menu_type")
else None
),
Expand Down
8 changes: 4 additions & 4 deletions data/factories/diagnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class Meta:
has_donation_agreement = factory.Faker("boolean")

has_diversification_plan = factory.Faker("boolean")
vegetarian_weekly_recurrence = fuzzy.FuzzyChoice(list(Diagnostic.MenuFrequency))
vegetarian_menu_type = fuzzy.FuzzyChoice(list(Diagnostic.MenuType))
vegetarian_weekly_recurrence = fuzzy.FuzzyChoice(list(Diagnostic.VegetarianMenuFrequency))
vegetarian_menu_type = fuzzy.FuzzyChoice(list(Diagnostic.VegetarianMenuType))

cooking_plastic_substituted = factory.Faker("boolean")
serving_plastic_substituted = factory.Faker("boolean")
Expand Down Expand Up @@ -72,8 +72,8 @@ class Meta:
has_donation_agreement = factory.Faker("boolean")

has_diversification_plan = factory.Faker("boolean")
vegetarian_weekly_recurrence = fuzzy.FuzzyChoice(list(Diagnostic.MenuFrequency))
vegetarian_menu_type = fuzzy.FuzzyChoice(list(Diagnostic.MenuType))
vegetarian_weekly_recurrence = fuzzy.FuzzyChoice(list(Diagnostic.VegetarianMenuFrequency))
vegetarian_menu_type = fuzzy.FuzzyChoice(list(Diagnostic.VegetarianMenuType))

cooking_plastic_substituted = factory.Faker("boolean")
serving_plastic_substituted = factory.Faker("boolean")
Expand Down
15 changes: 9 additions & 6 deletions data/models/diagnostic.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ class DiagnosticType(models.TextChoices):
class CreationSource(models.TextChoices):
TUNNEL = "TUNNEL", "Tunnel"

class MenuFrequency(models.TextChoices):
class VegetarianMenuFrequency(models.TextChoices):
NEVER = "NEVER", "Jamais"
LOW = "LOW", "Moins d'une fois par semaine"
MID = "MID", "Une fois par semaine"
HIGH = "HIGH", "Plus d'une fois par semaine"
DAILY = "DAILY", "De façon quotidienne"

class MenuType(models.TextChoices):
class VegetarianMenuType(models.TextChoices):
UNIQUE = "UNIQUE", "Un menu végétarien en plat unique, sans choix"
SEVERAL = (
"SEVERAL",
Expand Down Expand Up @@ -435,14 +435,14 @@ class PublicationStatus(models.TextChoices):
)
vegetarian_weekly_recurrence = models.CharField(
max_length=255,
choices=MenuFrequency.choices,
choices=VegetarianMenuFrequency.choices,
null=True,
blank=True,
verbose_name="Menus végétariens par semaine",
)
vegetarian_menu_type = models.CharField(
max_length=255,
choices=MenuType.choices,
choices=VegetarianMenuType.choices,
blank=True,
null=True,
verbose_name="Menu végétarien proposé",
Expand Down Expand Up @@ -1740,9 +1740,12 @@ def waste_badge(self) -> bool | None:

@property
def diversification_badge(self) -> bool | None:
if self.vegetarian_weekly_recurrence == Diagnostic.MenuFrequency.DAILY:
if self.vegetarian_weekly_recurrence == Diagnostic.VegetarianMenuFrequency.DAILY:
return True
elif self.vegetarian_weekly_recurrence in [Diagnostic.MenuFrequency.MID, Diagnostic.MenuFrequency.HIGH]:
elif self.vegetarian_weekly_recurrence in [
Diagnostic.VegetarianMenuFrequency.MID,
Diagnostic.VegetarianMenuFrequency.HIGH,
]:
# if the canteen is in the education sector, it can have a lower recurrence
if self.canteen.in_education:
return True
Expand Down

0 comments on commit b64642c

Please sign in to comment.