From 672c29671e02f60bccd9a7cf338ccf8fcaf313b0 Mon Sep 17 00:00:00 2001 From: KatKatKateryna Date: Wed, 9 Oct 2024 12:16:29 +0100 Subject: [PATCH] move validation --- pygeoapi/flask_app.py | 23 ------------------- .../provider/speckle_utils/coords_utils.py | 21 ++++++++++------- pygeoapi/provider/speckle_utils/legal.py | 4 +++- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/pygeoapi/flask_app.py b/pygeoapi/flask_app.py index 99e4143..0c65bc9 100644 --- a/pygeoapi/flask_app.py +++ b/pygeoapi/flask_app.py @@ -174,7 +174,6 @@ def execute_from_flask(api_function, request: Request, *args, def handle_client(url_route: str): - from geopy.geocoders import Nominatim # if called fromm the browser, Exceptions from this function will result in infinite load agent = request.headers.get('User-Agent') @@ -194,28 +193,6 @@ def handle_client(url_route: str): if agent is not None and ("YaBrowser/" in agent or "yandex" in agent.lower()): raise ValueError("Your browser is not supported.") - # by proj location: - url_lower = request.url.lower() - if "&lat=" in url_lower and "&lon=" in url_lower: - country_code = "" - try: - geolocator = Nominatim(user_agent="specklePygeoapi") - - lat = float(url_lower.split("&lat=")[1].split("&")[0]) - lon = float(url_lower.split("&lon=")[1].split("&")[0]) - - coord = f"{lat}, {lon}" - location = geolocator.reverse(coord, exactly_one=True) - if location is not None: - address = location.raw['address'] - country_code = address.get('country_code', '') - except Exception as e: - print(f"Error validating project location: {e}") - - if country_code in COUNTRY_CODES: - print(f"Validating project location: blocked LAT LON {lat}, {lon}") - raise PermissionError("Review Speckle Terms and Conditions") - # by IP: try: url = 'https://ipinfo.io/' + ip_address + '/json' diff --git a/pygeoapi/provider/speckle_utils/coords_utils.py b/pygeoapi/provider/speckle_utils/coords_utils.py index a8af815..5e02d02 100644 --- a/pygeoapi/provider/speckle_utils/coords_utils.py +++ b/pygeoapi/provider/speckle_utils/coords_utils.py @@ -2,7 +2,7 @@ import copy import math from typing import List -from pygeoapi.provider.speckle_utils.legal import COUNTRY_CODES +from pygeoapi.provider.speckle_utils.legal import COUNTRY_CODES, STATES, POSTCODES def reproject_bulk(self, all_coords: List[List[List[float]]], all_coord_counts: List[List[None| List[int]]], geometries) -> None: @@ -14,11 +14,12 @@ def reproject_bulk(self, all_coords: List[List[List[float]]], all_coord_counts: time1 = datetime.now() flat_coords = reproject_2d_coords_list(self, all_coords) time2 = datetime.now() - validate_coords(self, flat_coords[0]) - time_operation = (time2-time1).total_seconds() self.times["time_reproject"] = time_operation - # print(f"Reproject time: {time_operation}") + + validate_coords(self, flat_coords[0]) + if len(flat_coords)>2: + validate_coords(self, flat_coords[len(flat_coords)-1]) # define type of features feat_coord_group_is_multi = [True if None in x else False for x in all_coord_counts] @@ -118,17 +119,21 @@ def offset_rotate(self, coords_in: List[list]) -> List[List[float]]: def validate_coords(self, coords): from geopy.geocoders import Nominatim country_code = "" + state = "" + postcode = "" try: - geolocator = Nominatim(user_agent="specklePygeoapi") + geolocator = Nominatim(user_agent="specklePygeoapi") coord = f"{coords[1]}, {coords[0]}" location = geolocator.reverse(coord, exactly_one=True) if location is not None: address = location.raw['address'] country_code = address.get('country_code', '') + state = address.get('state', '') + postcode = address.get('postcode', '') except Exception as e: print(f"Error validating project location: {e}") self.country_code = country_code - - if country_code in COUNTRY_CODES: - print(f"Validating project location: blocked LAT LON {coords[1]}, {coords[0]}") + + if country_code in COUNTRY_CODES or state in STATES or postcode in POSTCODES: + print(f"Validating project location: blocked LAT LON {coords[1]}, {coords[0]}, {country_code}, {state}, {postcode}") raise PermissionError("Review Speckle Terms and Conditions") diff --git a/pygeoapi/provider/speckle_utils/legal.py b/pygeoapi/provider/speckle_utils/legal.py index 2f3a38b..31481ec 100644 --- a/pygeoapi/provider/speckle_utils/legal.py +++ b/pygeoapi/provider/speckle_utils/legal.py @@ -1 +1,3 @@ -COUNTRY_CODES = ["ru"] \ No newline at end of file +COUNTRY_CODES = ["ru"] +STATES = ['Автономна Республіка Крим', 'Севастополь', 'Донецька область', 'Луганська область'] +POSTCODES = [str(i) for i in range(95000,99999)] \ No newline at end of file