Skip to content

Commit

Permalink
feat/refact: Use of medias type and bibattribut
Browse files Browse the repository at this point in the history
-Backend
	- Remove all references of taxhub_full_lists (list in cache backend)
	- Add routes to get medias types and bibattributs
	- Add params to route "get_lists" in order to use query params list of cd_nom
	- Change docstring for "get_lists"
	- Add function "set_taxa_info_from_taxhub" to add information taxhub (taxref and medias) to newobs

- Frontend:
	- Add globalCacheService to get and set medias types and bibattributs
	- Add api route in taxhubService to get Medias and BibAttributs and loadAndCacheData to use globalCacheService based on return of api route called
	- Add interface to use typing as much as possible
	- Add call to this._taxhubService.setMediasAndAttributs in order to filter on medias (photo, photo principale and photo citizen) and to filter on attribut "nom_français"
	- Comment old "Autocomplete" code
	- Use filtered method service : this._taxhubService.filterMediasTaxhub in step of "congrats" in order to use first photo, if not medias taxhub
- Global:
	- Add comments to all references seems to be useless now

Reviewed-by: andriacap
  • Loading branch information
andriacap committed Dec 10, 2024
1 parent 2d36546 commit a92ecce
Show file tree
Hide file tree
Showing 16 changed files with 651 additions and 257 deletions.
18 changes: 1 addition & 17 deletions backend/gncitizen/core/observations/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
)

TAXREF_KEYS = ["nom_vern", "cd_nom", "cd_ref", "lb_nom"]
MEDIA_KEYS = ["id_media", "nom_type_media"]
MEDIA_KEYS = ["id_media"]

if current_app.config.get("VERIFY_OBSERVATIONS_ENABLED", False):
OBS_KEYS = OBS_KEYS + ("validation_status",)
Expand Down Expand Up @@ -183,22 +183,6 @@ def get_feature(self):
}
for p in self.medias
]
#TODO: it works only for taxhub full list with length <1000 (see if we remove this meanwhile taxhub route allows to return information by list of cd_nom)
taxon_repository = taxhub_full_lists[self.program_ref.taxonomy_list]
try:
taxon = next(
taxon
for taxon in taxon_repository
if taxon and taxon["cd_nom"] == feature["properties"]["cd_nom"]
)
feature["properties"]["taxref"] = {
key: taxon["taxref"][key] for key in TAXREF_KEYS
}
feature["properties"]["medias"] = [
{key: media[key] for key in MEDIA_KEYS} for media in taxon["medias"]
]
except StopIteration:
pass

return feature

Expand Down
39 changes: 33 additions & 6 deletions backend/gncitizen/core/observations/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from gncitizen.utils.jwt import get_id_role_if_exists, get_user_if_exists
from gncitizen.utils.mail_check import send_user_email
from gncitizen.utils.media import save_upload_files
from gncitizen.utils.taxonomy import get_specie_from_cd_nom,get_taxa_by_cd_nom
from gncitizen.utils.taxonomy import get_taxa_by_cd_nom, taxhub_rest_get_taxon_list, set_taxa_info_from_taxhub
from server import db

from .admin import ObservationView
Expand Down Expand Up @@ -223,7 +223,6 @@ def post_observation():

# If taxon name is not provided: call taxhub
if not newobs.name:
# taxon = get_specie_from_cd_nom(newobs.cd_nom)
taxon = get_taxa_by_cd_nom(newobs.cd_nom)
newobs.name = taxon.get("nom_vern", "")

Expand Down Expand Up @@ -252,8 +251,16 @@ def post_observation():
db.joinedload(ObservationModel.medias)
).get(newobs.id_observation)
features = newobs.get_feature()
#TODO: it seems to be useless now because we get medias from joinedload
# features["properties"]["images"] = file

id_taxonomy_list = newobs.program_ref.taxonomy_list
params = {'cd_nom': newobs.cd_nom}
# Appel synchrone à taxhub_rest_get_taxon_list
if id_taxonomy_list is not None:
taxon_list_data = taxhub_rest_get_taxon_list(id_taxonomy_list, params)
else:
taxon_list_data = None

features_with_taxhub_info = set_taxa_info_from_taxhub(taxon_list_data, [features])
except Exception as e:
current_app.logger.warning(
"[post_observation] ObsTax ERROR ON FILE SAVING", str(e)
Expand All @@ -263,7 +270,7 @@ def post_observation():
{
"message": "Nouvelle observation créée.",
"features": [
features,
features_with_taxhub_info[0],
],
"type": "FeatureCollection",
},
Expand Down Expand Up @@ -321,7 +328,27 @@ def get_all_observations() -> Union[FeatureCollection, Tuple[Dict, int]]:
else:
observations = query.all()
features = [obs.get_feature() for obs in observations]
feature_collection = FeatureCollection(features)


if len(observations) > 0:
id_taxonomy_list = observations[0].program_ref.taxonomy_list
cd_nom_list = ','.join(map(str, set(obs.cd_nom for obs in observations)))
else:
id_taxonomy_list = None


if len(cd_nom_list) > 0:
params = {'cd_nom': cd_nom_list}
else:
params = {}

if id_taxonomy_list is not None:
taxon_list_data = taxhub_rest_get_taxon_list(id_taxonomy_list, params)
else:
taxon_list_data = None

features_with_taxhub_info = set_taxa_info_from_taxhub(taxon_list_data, features)
feature_collection = FeatureCollection(features_with_taxhub_info)

if paginate:
feature_collection["per_page"] = query.per_page
Expand Down
Loading

0 comments on commit a92ecce

Please sign in to comment.