Skip to content

Commit c63b6e3

Browse files
Merge pull request #524 from hotosm/feature/authentication
Feature Raw Data API Authentication
2 parents a6af350 + 3a1541d commit c63b6e3

File tree

6 files changed

+1095
-649
lines changed

6 files changed

+1095
-649
lines changed

api/views.py

Lines changed: 78 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ def get_serializer_class(self):
286286
return HDXExportRegionSerializer
287287

288288
def perform_create(self, serializer):
289-
290289
serializer.save()
291290
if settings.SYNC_TO_HDX:
292291
sync_region(serializer.instance)
@@ -617,96 +616,114 @@ def request_geonames(request):
617616

618617
if geonames_url:
619618
keyword = request.GET.get("q")
620-
response = {'totalResultsCount': 0, 'geonames': []}
621-
if not (str(keyword).lower().startswith('boundary') or str(keyword).lower().startswith('osm') or str(keyword).lower().startswith('tm')):
619+
response = {"totalResultsCount": 0, "geonames": []}
620+
if not (
621+
str(keyword).lower().startswith("boundary")
622+
or str(keyword).lower().startswith("osm")
623+
or str(keyword).lower().startswith("tm")
624+
):
622625
response = requests.get(geonames_url, params=payload).json()
623626
print(response)
624627
assert isinstance(response, dict)
625628
if RAW_DATA_API_URL:
626-
if str(keyword).lower().startswith('boundary'):
627-
lst=keyword.split(" ")
628-
if len(lst)>1:
629-
keyword=lst[1]
629+
if str(keyword).lower().startswith("boundary"):
630+
lst = keyword.split(" ")
631+
if len(lst) > 1:
632+
keyword = lst[1]
630633
res = requests.get(f"{RAW_DATA_API_URL}v1/countries/?q={keyword}")
631634
if res.ok:
632635
if len(res.json()["features"]) >= 1:
633636
for feature in res.json()["features"]:
634637
geojson = {
635-
"type": "FeatureCollection",
636-
"features": [
637-
{"type": "Feature", "properties": {}, "geometry":feature["geometry"]}
638-
],
639-
}
638+
"type": "FeatureCollection",
639+
"features": [
640+
{
641+
"type": "Feature",
642+
"properties": {},
643+
"geometry": feature["geometry"],
644+
}
645+
],
646+
}
640647
add_resp = {
641648
"bbox": geojson,
642-
"adminName2": feature["properties"]["description"],
643-
"name": f'{request.GET.get("q")} -> {feature["properties"]["name"]}',
644-
"countryName": feature["properties"]["dataset_name"],
645-
"adminName1": feature["properties"]["id"],
649+
"adminName2": feature["properties"]["iso_3"],
650+
"name": f'{request.GET.get("q")} -> {feature["properties"]["description"]}',
651+
"countryName": feature["properties"][
652+
"dataset_name"
653+
],
654+
"adminName1": feature["properties"]["cid"],
646655
}
647-
656+
648657
if "geonames" in response:
649658
response["geonames"].append(add_resp)
650659

651-
if str(keyword).lower().startswith('osm'):
652-
lst=keyword.split(" ")
653-
if len(lst)>=1:
654-
keyword=lst[1]
655-
try :
656-
osm_id= int(keyword)
657-
res = requests.get(f"{RAW_DATA_API_URL}v1/osm_id/?osm_id={osm_id}")
660+
if str(keyword).lower().startswith("osm"):
661+
lst = keyword.split(" ")
662+
if len(lst) >= 1:
663+
keyword = lst[1]
664+
try:
665+
osm_id = int(keyword)
666+
res = requests.get(
667+
f"{RAW_DATA_API_URL}v1/osm_id/?osm_id={osm_id}"
668+
)
658669
if res.ok:
659670
if len(res.json()["features"]) >= 1:
660671
for feature in res.json()["features"]:
661672
geojson = {
662-
"type": "FeatureCollection",
663-
"features": [
664-
{"type": "Feature", "properties": {}, "geometry": feature["geometry"]}
665-
],
666-
}
673+
"type": "FeatureCollection",
674+
"features": [
675+
{
676+
"type": "Feature",
677+
"properties": {},
678+
"geometry": feature["geometry"],
679+
}
680+
],
681+
}
667682
add_resp = {
668683
"bbox": geojson,
669684
"adminName2": "OSM",
670685
"name": request.GET.get("q"),
671686
"countryName": osm_id,
672687
"adminName1": "Element",
673688
}
674-
689+
675690
if "geonames" in response:
676691
response["geonames"].append(add_resp)
677-
except :
692+
except:
678693
pass
679694

680-
681-
if str(keyword).lower().startswith('tm'):
682-
lst=keyword.split(" ")
683-
if len(lst)>=1:
684-
685-
keyword=lst[1]
686-
if tm_url:
687-
tm_res = requests.get(f"{tm_url}/{int(keyword)}/")
688-
if tm_res.ok:
689-
tm_res=tm_res.json()
690-
if "areaOfInterest" in tm_res:
691-
print("TM Project found")
692-
geom = tm_res["areaOfInterest"]
693-
geojson = {
694-
"type": "FeatureCollection",
695-
"features": [
696-
{"type": "Feature", "properties": {}, "geometry": geom}
697-
],
698-
}
699-
700-
add_resp = {
701-
"bbox": geojson,
702-
"adminName2": "TM",
703-
"name": request.GET.get("q"),
704-
"countryName": "Boundary",
705-
"adminName1": "Project",
706-
}
707-
# print(add_resp)
708-
if "geonames" in response:
709-
response["geonames"].append(add_resp)
695+
if str(keyword).lower().startswith("tm"):
696+
lst = keyword.split(" ")
697+
if len(lst) >= 1:
698+
keyword = lst[1]
699+
if tm_url:
700+
tm_res = requests.get(f"{tm_url}/{int(keyword)}/")
701+
if tm_res.ok:
702+
tm_res = tm_res.json()
703+
if "areaOfInterest" in tm_res:
704+
print("TM Project found")
705+
geom = tm_res["areaOfInterest"]
706+
geojson = {
707+
"type": "FeatureCollection",
708+
"features": [
709+
{
710+
"type": "Feature",
711+
"properties": {},
712+
"geometry": geom,
713+
}
714+
],
715+
}
716+
717+
add_resp = {
718+
"bbox": geojson,
719+
"adminName2": "TM",
720+
"name": request.GET.get("q"),
721+
"countryName": "Boundary",
722+
"adminName1": "Project",
723+
}
724+
# print(add_resp)
725+
if "geonames" in response:
726+
response["geonames"].append(add_resp)
710727

711728
return JsonResponse(response)
712729
else:

0 commit comments

Comments
 (0)