-
Notifications
You must be signed in to change notification settings - Fork 0
Feat(Popup): Use external data to find categorical labels #185
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
26fec63
Feat(Popup): Use external data to find categorical labels
arnaudfnr dc61ada
Generalize external data fetching
arnaudfnr ad3ac71
Add missing files
arnaudfnr 3cff431
Improve Fallback Renderer
arnaudfnr bf6d9ea
Fix lint
arnaudfnr 8cdc2ee
Use external data for taxon & tree labels
arnaudfnr e9b04b0
Clean code
arnaudfnr 3a176a2
Remove backend unused import
arnaudfnr 84ca748
Fix review
arnaudfnr 8636534
remove unused import
arnaudfnr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,91 @@ | ||
| from django.conf import settings | ||
| import json | ||
| import pandas as pd | ||
| from pathlib import Path | ||
|
|
||
| from rest_framework import status | ||
| from rest_framework.decorators import api_view, permission_classes | ||
| from rest_framework.permissions import IsAuthenticated | ||
| from rest_framework.decorators import api_view | ||
| from rest_framework.exceptions import NotFound, ValidationError | ||
| from rest_framework.response import Response | ||
|
|
||
| import pandas as pd | ||
| import json | ||
|
|
||
| catalog_path = settings.BASE_DIR / "catalog" | ||
|
|
||
|
|
||
| @api_view(["GET"]) | ||
| @permission_classes([IsAuthenticated]) | ||
| def catalog_view(request, layer_id, resource_name): | ||
| def resource_view(request, layer_id, resource_name): | ||
| resource_path = get_resource_path(catalog_path, layer_id, resource_name) | ||
| df = pd.read_parquet(resource_path) | ||
| return Response(json.loads(df.to_json(orient="records"))) | ||
|
|
||
|
|
||
| @api_view(["POST"]) | ||
| def resource_list_view(request, layer_id): | ||
| resource_list = parse_body(request.body) | ||
| package_path = get_package_path(catalog_path, layer_id) | ||
|
|
||
| resources_payload, results, errors = load_resources(layer_id, package_path, resource_list) | ||
| if not resources_payload: | ||
| raise NotFound({"error": "None of the resources could be loaded", "results": results}) | ||
| elif errors: | ||
| return Response({"results": results}, status=status.HTTP_207_MULTI_STATUS) | ||
|
|
||
| return Response(resources_payload) | ||
|
|
||
| def parse_body(body): | ||
| try: | ||
| payload = json.loads(body.decode("utf-8")) | ||
| except (UnicodeDecodeError, json.JSONDecodeError): | ||
| raise ValidationError("Request body must be valid JSON") | ||
|
|
||
| resource_list = payload.get("resources") | ||
| if not isinstance(resource_list, list) or len(resource_list) < 1: | ||
| raise ValidationError("Body should contain a non-empty list of resource names") | ||
|
|
||
| return resource_list | ||
|
|
||
|
|
||
| def load_resources(layer_id, package_path, resource_list): | ||
| resources_payload = {} | ||
| results = [] | ||
| errors = False | ||
| for resource_name in resource_list: | ||
| resource_path = package_path / f"{resource_name}.parquet" | ||
| if not Path(resource_path).exists(): | ||
| errors=True | ||
| results.append(not_found_result(layer_id, resource_name)) | ||
| continue | ||
| resource_df = pd.read_parquet(resource_path) | ||
| resources_payload[resource_name] = json.loads(resource_df.to_json(orient="records")) | ||
| results.append(ok_result(layer_id, resource_name)) | ||
|
|
||
| return resources_payload, results, errors | ||
|
|
||
| def get_resource_path(catalog_path, layer_id, resource_name): | ||
| package_path = get_package_path(catalog_path, layer_id) | ||
| resource_path = package_path / f"{resource_name}.parquet" | ||
|
|
||
| if not Path(resource_path).exists(): | ||
| raise NotFound(f"Resource with name '{resource_name}' was not found in the package {layer_id}") | ||
|
|
||
| return resource_path | ||
|
|
||
| def get_package_path(catalog_path, layer_id): | ||
| package_path = catalog_path / f"{layer_id}" | ||
|
|
||
| if not Path(package_path).exists(): | ||
| raise NotFound(f"Package with name '{layer_id}' was not found in the catalog") | ||
|
|
||
| return package_path | ||
|
|
||
| # authorization: ensure user has the custom add_data permission | ||
| if not request.user.has_perm("users.view_data"): | ||
| return Response(status=status.HTTP_403_FORBIDDEN) | ||
| def not_found_result(layer_id, resource_name): | ||
| return { | ||
| "uri": f"{layer_id} / {resource_name}", | ||
| "status": status.HTTP_404_NOT_FOUND, | ||
| "reason": f"Resource with name '{resource_name}' was not found in the package {layer_id}" | ||
| } | ||
|
|
||
| target_path = catalog_path / f"{layer_id}" / f"{resource_name}.parquet" | ||
| df = pd.read_parquet(target_path) | ||
| return Response(json.loads(df.to_json(orient="records"))) | ||
| def ok_result(layer_id, resource_name): | ||
| return { | ||
| "uri": f"{layer_id} / {resource_name}", | ||
| "status": status.HTTP_200_OK | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch 🤦