def get_conf_dict(slug: str) -> dict: """ Takes a slug value for a DataProvider and returns a mapproxy configuration as a dict. :param slug: A string matching the slug of a DataProvider :return: a dict. """ from eventkit_cloud.jobs.models import DataProvider # Circular reference try: provider = cast(DataProvider, get_cached_model(model=DataProvider, prop="slug", value=slug)) except Exception: raise Exception(f"Unable to find provider for slug {slug}") # Load and "clean" mapproxy config for displaying a map. try: conf_dict = yaml.safe_load(provider.config) # Pop layers out so that the default layer configuration above is used. conf_dict.pop("layers", "") ssl_verify = getattr(settings, "SSL_VERIFICATION", True) if isinstance(ssl_verify, bool): if not ssl_verify: conf_dict["globals"] = {"http": {"ssl_no_cert_checks": ssl_verify}} else: conf_dict["globals"] = {"http": {"ssl_ca_certs": ssl_verify}} conf_dict.update( {"globals": {"cache": {"lock_dir": "./locks", "tile_lock_dir": "./locks"}, "tiles": {"expires_hours": 0}}} ) except Exception as e: logger.error(e) raise Exception(f"Unable to load a mapproxy configuration for slug {slug}") return conf_dict
def test_get_cached_model(self, mocked_cache: MagicMock): export_provider = MagicMock() expected_name = "SomeProvider" expected_prop = "slug" expected_val = "osm" export_provider.__name__ = expected_name mocked_cache.get_or_set.return_value = export_provider get_model = get_model_by_params(export_provider) return_value = get_cached_model(export_provider, expected_prop, expected_val) self.assertEquals(export_provider, return_value) expected_call_value = f"{expected_name}-{expected_prop}-{expected_val}" mocked_cache.get_or_set.assert_called_once_with( expected_call_value, get_model, 360)
def add_restricted_regions_to_config( base_config: dict, config: dict, slug: str, user: User = None ) -> Tuple[dict, dict]: from eventkit_cloud.jobs.models import DataProvider, RegionalPolicy config["sources"]["default"]["coverage"] = { "clip": True, "difference": [{"bbox": [-180, -90, 180, 90], "srs": "EPSG:4326"}], } providers = [get_cached_model(model=DataProvider, prop="slug", value=slug)] for policy in RegionalPolicy.objects.filter(providers__in=providers).prefetch_related("justifications"): # If no user no need to set up regional policy. if user and not get_valid_regional_justification(policy, user): config["sources"]["default"]["coverage"]["difference"].append( {"bbox": GEOSGeometry(policy.region.the_geom).extent, "srs": "EPSG:4326"} ) for current_cache in base_config.get("caches", {}): base_config["caches"][current_cache]["disable_storage"] = True return base_config, config
def map(request: AuthenticatedHttpRequest, slug: str, path: str) -> HttpResponse: """ Makes a proxy request to mapproxy used to get map tiles. :param request: The httprequest. :param slug: A string matching the slug of a DataProvider. :param path: The rest of the url context (i.e. path to the tile some_service/0/0/0.png). :return: The HttpResponse. """ mapproxy_app = create_mapproxy_app(slug, request.user) params = parse_qs(request.META["QUERY_STRING"]) script_name = f"/map/{slug}" mp_response = mapproxy_app.get(path, params, request.headers, extra_environ=dict(SCRIPT_NAME=script_name), expect_errors=True) response = HttpResponse(mp_response.body, status=mp_response.status_int) for header, value in mp_response.headers.items(): response[header] = value if params.get("REQUEST") == ["GetFeatureInfo"]: provider = cast(DataProvider, get_cached_model(DataProvider, "slug", slug)) if response.status_code in [200, 202]: try: map_query = get_map_query(provider.metadata.get("type")) response = map_query().get_geojson(response) except Exception as e: logger.error(e) response.status_code = 500 response.content = "No data available." else: if provider.metadata: response.content = "The service was unable to provide data for this location." else: response.content = "No data is available for this service." response["Content-length"] = len(response.content) return response