UPDATE
This commit is contained in:
+102
@@ -1,3 +1,8 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
from functools import cached_property
|
||||
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
@@ -5,3 +10,100 @@ class PlantConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "plant"
|
||||
verbose_name = "Plant"
|
||||
|
||||
@cached_property
|
||||
def plant_aliases(self) -> dict[str, str]:
|
||||
return {
|
||||
"tomato": "گوجهفرنگی",
|
||||
"cucumber": "خیار",
|
||||
"pepper": "فلفل دلمهای",
|
||||
"bell pepper": "فلفل دلمهای",
|
||||
"carrot": "هویج",
|
||||
"lettuce": "کاهو",
|
||||
"potato": "سیبزمینی",
|
||||
"onion": "پیاز",
|
||||
}
|
||||
|
||||
@cached_property
|
||||
def growth_stage_aliases(self) -> dict[str, str]:
|
||||
return {
|
||||
"initial": "initial",
|
||||
"seedling": "initial",
|
||||
"establishment": "initial",
|
||||
"جوانه زنی": "initial",
|
||||
"جوانهزنی": "initial",
|
||||
"نشا": "initial",
|
||||
"استقرار": "initial",
|
||||
"vegetative": "vegetative",
|
||||
"growth": "vegetative",
|
||||
"رویشی": "vegetative",
|
||||
"رشد رویشی": "vegetative",
|
||||
"flowering": "flowering",
|
||||
"anthesis": "flowering",
|
||||
"گلدهی": "flowering",
|
||||
"گل دهی": "flowering",
|
||||
"fruiting": "fruiting",
|
||||
"harvest": "fruiting",
|
||||
"ripening": "fruiting",
|
||||
"میوه دهی": "fruiting",
|
||||
"میوهدهی": "fruiting",
|
||||
"برداشت": "fruiting",
|
||||
"maturity": "maturity",
|
||||
"رسیدگی": "maturity",
|
||||
"بلوغ": "maturity",
|
||||
}
|
||||
|
||||
def _normalize_lookup_value(self, value: str | None) -> str:
|
||||
text = (value or "").strip().lower()
|
||||
if not text:
|
||||
return ""
|
||||
|
||||
translation_table = str.maketrans(
|
||||
{
|
||||
"ي": "ی",
|
||||
"ك": "ک",
|
||||
"ة": "ه",
|
||||
"أ": "ا",
|
||||
"إ": "ا",
|
||||
"ؤ": "و",
|
||||
"ۀ": "ه",
|
||||
"": " ",
|
||||
"-": " ",
|
||||
"_": " ",
|
||||
}
|
||||
)
|
||||
text = text.translate(translation_table)
|
||||
text = re.sub(r"\s+", " ", text)
|
||||
return text.strip()
|
||||
|
||||
def resolve_growth_stage(self, growth_stage: str | None) -> str | None:
|
||||
value = (growth_stage or "").strip()
|
||||
if not value:
|
||||
return value
|
||||
|
||||
normalized = self._normalize_lookup_value(value)
|
||||
return self.growth_stage_aliases.get(normalized, value)
|
||||
|
||||
def resolve_plant_name(self, plant_name: str | None) -> str | None:
|
||||
from .models import Plant
|
||||
|
||||
value = (plant_name or "").strip()
|
||||
if not value:
|
||||
return value
|
||||
|
||||
plant = Plant.objects.filter(name=value).first() or Plant.objects.filter(name__iexact=value).first()
|
||||
if plant is not None:
|
||||
return plant.name
|
||||
|
||||
normalized = self._normalize_lookup_value(value)
|
||||
alias_target = self.plant_aliases.get(normalized)
|
||||
if alias_target:
|
||||
aliased_plant = Plant.objects.filter(name=alias_target).first()
|
||||
if aliased_plant is not None:
|
||||
return aliased_plant.name
|
||||
|
||||
for plant in Plant.objects.only("name").iterator():
|
||||
if self._normalize_lookup_value(plant.name) == normalized:
|
||||
return plant.name
|
||||
|
||||
return value
|
||||
|
||||
Reference in New Issue
Block a user