move validation
This commit is contained in:
@@ -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'
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
COUNTRY_CODES = ["ru"]
|
||||
COUNTRY_CODES = ["ru"]
|
||||
STATES = ['Автономна Республіка Крим', 'Севастополь', 'Донецька область', 'Луганська область']
|
||||
POSTCODES = [str(i) for i in range(95000,99999)]
|
||||
Reference in New Issue
Block a user