def cleanup_db(): """ Cleanup the database """ import transaction import c2cgeoportal_geoportal.lib from c2cgeoportal_commons.models import DBSession from c2cgeoportal_commons.models.main import OGCServer, TreeItem, Role, RestrictionArea, \ Interface, Functionality, FullTextSearch from c2cgeoportal_commons.models.static import Shorturl, User transaction.commit() for ra in DBSession.query(RestrictionArea).all(): ra.roles = [] DBSession.delete(ra) for ti in DBSession.query(TreeItem).all(): DBSession.delete(ti) DBSession.query(OGCServer).delete() DBSession.query(Interface).delete() for r in DBSession.query(Role).all(): r.functionnalities = [] DBSession.delete(r) DBSession.query(User).delete() DBSession.query(Functionality).delete() DBSession.query(FullTextSearch).delete() DBSession.query(Shorturl).delete() transaction.commit() c2cgeoportal_geoportal.lib.ogc_server_wms_url_ids = None c2cgeoportal_geoportal.lib.ogc_server_wfs_url_ids = None caching.init_region({ "backend": "dogpile.cache.null", }) caching.invalidate_region()
def handle(event: InvalidateCacheEvent) -> None: del event caching.invalidate_region() if caching.MEMORY_CACHE_DICT: caching.get_region("std").delete_multi( list(caching.MEMORY_CACHE_DICT.keys())) caching.MEMORY_CACHE_DICT.clear()
def handle(event: InvalidateCacheEvent): # pylint: disable=unused-variable del event caching.invalidate_region() if caching.MEMORY_CACHE_DICT: caching.get_region("std").delete_multi( caching.MEMORY_CACHE_DICT.keys()) caching.MEMORY_CACHE_DICT.clear()
def fill_tech_user_functionality(name, functionalities): from c2cgeoportal_commons.models import DBSession from c2cgeoportal_commons.models.main import Functionality, Role role = DBSession.query(Role).filter_by(name=name).one() role.functionalities = [Functionality(name, value) for name, value in functionalities] DBSession.add(role) transaction.commit() caching.invalidate_region()
def create_default_ogcserver(): from c2cgeoportal_commons.models import DBSession from c2cgeoportal_commons.models.main import OGCServer transaction.commit() ogcserver = OGCServer(name="__test_ogc_server") ogcserver.url = mapserv_url DBSession.add(ogcserver) transaction.commit() caching.invalidate_region() return ogcserver
def test_no_layers(self): # mapfile error from c2cgeoportal_geoportal.views.theme import Theme request = self._create_request_obj() theme_view = Theme(request) request.params = {} invalidate_region() themes, errors = theme_view._themes("interface_no_layers") assert themes == [] assert {e[:90] for e in errors} == { "The layer '__test_public_layer_no_layers' do not have any layers" }
def test_notmapfile(self): # mapfile error from c2cgeoportal_geoportal.views.theme import Theme theme_view = Theme(self._create_request_obj()) invalidate_region() themes, errors = theme_view._themes("interface_notmapfile") assert len(themes) == 0 assert { e[:90] for e in errors } == { "GetCapabilities from URL http://mapserver:8080/?map=not_a_mapfile&SERVICE=WMS&VERSION=1.1.", }
def test_not_in_mapfile(self): # mapfile error from c2cgeoportal_geoportal.views.theme import Theme theme_view = Theme(self._create_request_obj()) invalidate_region() themes, errors = theme_view._themes("interface_not_in_mapfile") assert len(themes) == 0 assert { e[:90] for e in errors } == { "The layer '__test_public_layer_not_in_mapfile' (__test_public_layer_not_in_mapfile) is not" }
def test_notmapfile(self): # mapfile error from c2cgeoportal_geoportal.views.theme import Theme theme_view = Theme(self._create_request_obj()) invalidate_region() themes, errors = theme_view._themes("interface_notmapfile") assert len(themes) == 1 groups = {g["name"] for g in themes[0]["children"]} assert groups == {"__test_layer_group"} layers = {l["name"] for l in themes[0]["children"][0]["children"]} assert layers == {"__test_public_layer_notmapfile"} assert {e[:90] for e in errors} == { "GetCapabilities from URL http://mapserver:8080/?map=not_a_mapfile&SERVICE=WMS&VERSION=1.1.", }
def test_not_in_mapfile(self): # mapfile error from c2cgeoportal_geoportal.views.theme import Theme theme_view = Theme(self._create_request_obj()) invalidate_region() themes, errors = theme_view._themes("interface_not_in_mapfile") assert len(themes) == 1 groups = {g["name"] for g in themes[0]["children"]} assert groups == {"__test_layer_group"} layers = {l["name"] for l in themes[0]["children"][0]["children"]} assert layers == {"__test_public_layer_not_in_mapfile"} assert {e[:90] for e in errors} == { "The layer '__test_public_layer_not_in_mapfile' (__test_public_layer_not_in_mapfile) is not" }
def test_no_layers(self): # mapfile error from c2cgeoportal_geoportal.views.entry import Entry request = self._create_request_obj() entry = Entry(request) request.params = {} from c2cgeoportal_geoportal.lib import caching caching.invalidate_region() themes, errors = entry._themes("interface_no_layers") assert themes == [] assert { e[:90] for e in errors } == { "The layer '__test_public_layer_no_layers' do not have any layers", }
def setup_db(): """ Cleanup the database """ cleanup_db() from c2cgeoportal_commons.models import DBSession from c2cgeoportal_commons.models.main import Role DBSession.add_all([Role(name) for name in ("anonymous", "registered", "intranet")]) transaction.commit() c2cgeoportal_geoportal.lib.ogc_server_wms_url_ids = None c2cgeoportal_geoportal.lib.ogc_server_wfs_url_ids = None caching.init_region({"backend": "dogpile.cache.null"}, "std") caching.init_region({"backend": "dogpile.cache.null"}, "obj") caching.invalidate_region()
def test_not_in_mapfile(self): # mapfile error from c2cgeoportal_geoportal.views.entry import Entry entry = Entry(self._create_request_obj()) from c2cgeoportal_geoportal.lib import caching caching.invalidate_region() themes, errors = entry._themes("interface_not_in_mapfile") assert len(themes) == 1 groups = {g["name"] for g in themes[0]["children"]} assert groups == { "__test_layer_group", } layers = {l["name"] for l in themes[0]["children"][0]["children"]} assert layers == { "__test_public_layer_not_in_mapfile", } assert { e[:90] for e in errors } == { "The layer '__test_public_layer_not_in_mapfile' (__test_public_layer_not_in_mapfile) is not", }
def test_notmapfile(self): # mapfile error from c2cgeoportal_geoportal.views.entry import Entry entry = Entry(self._create_request_obj()) from c2cgeoportal_geoportal.lib import caching caching.invalidate_region() themes, errors = entry._themes("interface_notmapfile") assert len(themes) == 1 groups = {g["name"] for g in themes[0]["children"]} assert groups == { "__test_layer_group", } layers = {l["name"] for l in themes[0]["children"][0]["children"]} assert layers == { "__test_public_layer_notmapfile", } assert { e[:90] for e in errors } == { "The layer '__test_public_layer_notmapfile' (__test_public_layer_notmapfile) is not defined", "GetCapabilities from URL http://mapserver:8080/?map=not_a_mapfile&SERVICE=WMS&VERSION=1.1." }
def handle(event: InvalidateCacheEvent): # pylint: disable=unused-variable del event caching.invalidate_region()
def handle(event: InvalidateCacheEvent): del event caching.invalidate_region()
def includeme(config): """ This function returns a Pyramid WSGI application. """ # update the settings object from the YAML application config file settings = config.get_settings() settings.update(c2c.template.get_config(settings.get("app.cfg"))) call_hook(settings, "after_settings", settings) get_user_from_request = create_get_user_from_request(settings) config.add_request_method(get_user_from_request, name="user", property=True) config.add_request_method(get_user_from_request, name="get_user") # configure 'locale' dir as the translation dir for c2cgeoportal app config.add_translation_dirs("c2cgeoportal_geoportal:locale/") config.include(c2cwsgiutils.pyramid.includeme) health_check = HealthCheck(config) # Initialise DBSessions from c2cgeoportal_commons.models import init_dbsessions init_dbsessions(settings, config, health_check) # initialize the dbreflection module dbreflection.init() from c2cgeoportal_geoportal.lib import checker, check_collector checker.init(config, health_check) check_collector.init(config, health_check) config.include("pyramid_closure") # dogpile.cache configuration caching.init_region(settings["cache"]) caching.invalidate_region() # Register a tween to get back the cache buster path. config.add_tween( "c2cgeoportal_geoportal.lib.cacheversion.CachebusterTween") # bind the mako renderer to other file extensions add_mako_renderer(config, ".html") add_mako_renderer(config, ".js") config.include("pyramid_chameleon") # add the "geojson" renderer config.add_renderer("geojson", GeoJSON()) # add decimal json renderer config.add_renderer("decimaljson", DecimalJSON()) # add the "xsd" renderer config.add_renderer( "xsd", XSD(sequence_callback=dbreflection.xsd_sequence_callback)) # add the set_user_validator directive, and set a default user # validator config.add_directive("set_user_validator", set_user_validator) config.set_user_validator(default_user_validator) if settings.get("ogcproxy_enable", False): # pragma: no cover # add an OGCProxy view config.add_route_predicate("ogc_server", OgcproxyRoutePredicate) config.add_route("ogcproxy", "/ogcproxy", ogc_server=True) config.add_view("papyrus_ogcproxy.views:ogcproxy", route_name="ogcproxy") # add routes to the mapserver proxy config.add_route_predicate("mapserverproxy", MapserverproxyRoutePredicate) config.add_route( "mapserverproxy", "/mapserv_proxy", mapserverproxy=True, pregenerator=C2CPregenerator(role=True), ) # add route to the tinyows proxy config.add_route( "tinyowsproxy", "/tinyows_proxy", pregenerator=C2CPregenerator(role=True), ) # add routes to csv view config.add_route("csvecho", "/csv", request_method="POST") # add route to the export GPX/KML view config.add_route("exportgpxkml", "/exportgpxkml") # add routes to the echo service config.add_route("echo", "/echo", request_method="POST") # add routes to the entry view class config.add_route("base", "/", static=True) config.add_route("loginform", "/login.html", request_method="GET") add_cors_route(config, "/login", "login") config.add_route("login", "/login", request_method="POST") add_cors_route(config, "/logout", "login") config.add_route("logout", "/logout", request_method="GET") add_cors_route(config, "/loginchange", "login") config.add_route("loginchange", "/loginchange", request_method="POST") add_cors_route(config, "/loginresetpassword", "login") config.add_route("loginresetpassword", "/loginresetpassword", request_method="POST") add_cors_route(config, "/loginuser", "login") config.add_route("loginuser", "/loginuser", request_method="GET") config.add_route("testi18n", "/testi18n.html", request_method="GET") config.add_route("apijs", "/api.js", request_method="GET") config.add_route("xapijs", "/xapi.js", request_method="GET") config.add_route("apihelp", "/apihelp.html", request_method="GET") config.add_route("xapihelp", "/xapihelp.html", request_method="GET") config.add_route( "themes", "/themes", request_method="GET", pregenerator=C2CPregenerator(role=True), ) config.add_route("invalidate", "/invalidate", request_method="GET") # print proxy routes config.add_route("printproxy", "/printproxy", request_method="HEAD") add_cors_route(config, "/printproxy/*all", "print") config.add_route( "printproxy_capabilities", "/printproxy/capabilities.json", request_method="GET", pregenerator=C2CPregenerator(role=True), ) config.add_route("printproxy_report_create", "/printproxy/report.{format}", request_method="POST", header=JSON_CONTENT_TYPE) config.add_route("printproxy_status", "/printproxy/status/{ref}.json", request_method="GET") config.add_route("printproxy_cancel", "/printproxy/cancel/{ref}", request_method="DELETE") config.add_route("printproxy_report_get", "/printproxy/report/{ref}", request_method="GET") # v2 config.add_route( "printproxy_info", "/printproxy/info.json", request_method="GET", pregenerator=C2CPregenerator(role=True), ) config.add_route( "printproxy_create", "/printproxy/create.json", request_method="POST", ) config.add_route( "printproxy_get", "/printproxy/{file}.printout", request_method="GET", ) # full text search routes add_cors_route(config, "/fulltextsearch", "fulltextsearch") config.add_route("fulltextsearch", "/fulltextsearch") # Access to raster data add_cors_route(config, "/raster", "raster") config.add_route("raster", "/raster", request_method="GET") add_cors_route(config, "/profile.{ext}", "profile") config.add_route("profile.csv", "/profile.csv", request_method="POST") config.add_route("profile.json", "/profile.json", request_method="POST") # shortener add_cors_route(config, "/short/create", "shortener") config.add_route("shortener_create", "/short/create", request_method="POST") config.add_route("shortener_get", "/short/{ref}", request_method="GET") # Geometry processing config.add_route("difference", "/difference", request_method="POST") # PDF report tool config.add_route("pdfreport", "/pdfreport/{layername}/{ids}", request_method="GET") # add routes for the "layers" web service add_cors_route(config, "/layers/*all", "layers") config.add_route("layers_count", "/layers/{layer_id:\\d+}/count", request_method="GET") config.add_route( "layers_metadata", "/layers/{layer_id:\\d+}/md.xsd", request_method="GET", pregenerator=C2CPregenerator(role=True), ) config.add_route("layers_read_many", "/layers/{layer_id:\\d+,?(\\d+,)*\\d*$}", request_method="GET") # supports URLs like /layers/1,2,3 config.add_route("layers_read_one", "/layers/{layer_id:\\d+}/{feature_id}", request_method="GET") config.add_route("layers_create", "/layers/{layer_id:\\d+}", request_method="POST", header=JSON_CONTENT_TYPE) config.add_route("layers_update", "/layers/{layer_id:\\d+}/{feature_id}", request_method="PUT", header=JSON_CONTENT_TYPE) config.add_route("layers_delete", "/layers/{layer_id:\\d+}/{feature_id}", request_method="DELETE") config.add_route( "layers_enumerate_attribute_values", "/layers/{layer_name}/values/{field_name}", request_method="GET", pregenerator=C2CPregenerator(), ) # there is no view corresponding to that route, it is to be used from # mako templates to get the root of the "layers" web service config.add_route("layers_root", "/layers/", request_method="HEAD") # Resource proxy (load external url, useful when loading non https content) config.add_route("resourceproxy", "/resourceproxy", request_method="GET") # scan view decorator for adding routes config.scan(ignore=[ "c2cgeoportal_geoportal.scripts", "c2cgeoportal_geoportal.wsgi_app" ]) if "subdomains" in settings: # pragma: no cover config.registry.registerUtility(MultiDomainStaticURLInfo(), IStaticURLInfo) # add the static view (for static resources) _add_static_view(config, "static", "c2cgeoportal_geoportal:static") _add_static_view(config, "project", "c2cgeoportal_geoportal:project") add_admin_interface(config) add_static_view(config) # Handles the other HTTP errors raised by the views. Without that, # the client receives a status=200 without content. config.add_view(error_handler, context=HTTPException)
def includeme(config): """ This function returns a Pyramid WSGI application. """ # update the settings object from the YAML application config file settings = config.get_settings() settings.update(c2c.template.get_config(settings.get("app.cfg"))) call_hook(settings, "after_settings", settings) get_user_from_request = create_get_user_from_request(settings) config.add_request_method(get_user_from_request, name="user", property=True) config.add_request_method(get_user_from_request, name="get_user") # configure 'locale' dir as the translation dir for c2cgeoportal app config.add_translation_dirs("c2cgeoportal_geoportal:locale/") config.include(c2cwsgiutils.pyramid.includeme) health_check = HealthCheck(config) # Initialise DBSessions from c2cgeoportal_commons.models import init_dbsessions init_dbsessions(settings, config, health_check) # initialize the dbreflection module dbreflection.init() from c2cgeoportal_geoportal.lib import checker, check_collector checker.init(config, health_check) check_collector.init(config, health_check) config.include("pyramid_closure") # dogpile.cache configuration caching.init_region(settings["cache"]) caching.invalidate_region() # Register a tween to get back the cache buster path. config.add_tween("c2cgeoportal_geoportal.lib.cacheversion.CachebusterTween") # bind the mako renderer to other file extensions add_mako_renderer(config, ".html") add_mako_renderer(config, ".js") # add the "geojson" renderer config.add_renderer("geojson", GeoJSON()) # add decimal json renderer config.add_renderer("decimaljson", DecimalJSON()) # add the "xsd" renderer config.add_renderer("xsd", XSD( sequence_callback=dbreflection.xsd_sequence_callback )) # add the set_user_validator directive, and set a default user # validator config.add_directive("set_user_validator", set_user_validator) config.set_user_validator(default_user_validator) if settings.get("ogcproxy_enable", False): # pragma: no cover # add an OGCProxy view config.add_route_predicate("ogc_server", OgcproxyRoutePredicate) config.add_route( "ogcproxy", "/ogcproxy", ogc_server=True ) config.add_view("papyrus_ogcproxy.views:ogcproxy", route_name="ogcproxy") # add routes to the mapserver proxy config.add_route_predicate("mapserverproxy", MapserverproxyRoutePredicate) config.add_route( "mapserverproxy", "/mapserv_proxy", mapserverproxy=True, pregenerator=C2CPregenerator(role=True), ) # add route to the tinyows proxy config.add_route( "tinyowsproxy", "/tinyows_proxy", pregenerator=C2CPregenerator(role=True), ) # add routes to csv view config.add_route("csvecho", "/csv", request_method="POST") # add route to the export GPX/KML view config.add_route("exportgpxkml", "/exportgpxkml") # add routes to the echo service config.add_route("echo", "/echo", request_method="POST") # add routes to the entry view class config.add_route("base", "/", static=True) config.add_route("loginform", "/login.html", request_method="GET") add_cors_route(config, "/login", "login") config.add_route("login", "/login", request_method="POST") add_cors_route(config, "/logout", "login") config.add_route("logout", "/logout", request_method="GET") add_cors_route(config, "/loginchange", "login") config.add_route("loginchange", "/loginchange", request_method="POST") add_cors_route(config, "/loginresetpassword", "login") config.add_route("loginresetpassword", "/loginresetpassword", request_method="POST") add_cors_route(config, "/loginuser", "login") config.add_route("loginuser", "/loginuser", request_method="GET") config.add_route("testi18n", "/testi18n.html", request_method="GET") config.add_route("apijs", "/api.js", request_method="GET") config.add_route("xapijs", "/xapi.js", request_method="GET") config.add_route("apihelp", "/apihelp.html", request_method="GET") config.add_route("xapihelp", "/xapihelp.html", request_method="GET") config.add_route( "themes", "/themes", request_method="GET", pregenerator=C2CPregenerator(role=True), ) config.add_route("invalidate", "/invalidate", request_method="GET") # print proxy routes config.add_route("printproxy", "/printproxy", request_method="HEAD") add_cors_route(config, "/printproxy/*all", "print") config.add_route( "printproxy_capabilities", "/printproxy/capabilities.json", request_method="GET", pregenerator=C2CPregenerator(role=True), ) config.add_route( "printproxy_report_create", "/printproxy/report.{format}", request_method="POST", header=JSON_CONTENT_TYPE ) config.add_route( "printproxy_status", "/printproxy/status/{ref}.json", request_method="GET" ) config.add_route( "printproxy_cancel", "/printproxy/cancel/{ref}", request_method="DELETE" ) config.add_route( "printproxy_report_get", "/printproxy/report/{ref}", request_method="GET" ) # v2 config.add_route( "printproxy_info", "/printproxy/info.json", request_method="GET", pregenerator=C2CPregenerator(role=True), ) config.add_route( "printproxy_create", "/printproxy/create.json", request_method="POST", ) config.add_route( "printproxy_get", "/printproxy/{file}.printout", request_method="GET", ) # full text search routes add_cors_route(config, "/fulltextsearch", "fulltextsearch") config.add_route("fulltextsearch", "/fulltextsearch") # Access to raster data add_cors_route(config, "/raster", "raster") config.add_route("raster", "/raster", request_method="GET") add_cors_route(config, "/profile.{ext}", "profile") config.add_route("profile.csv", "/profile.csv", request_method="POST") config.add_route("profile.json", "/profile.json", request_method="POST") # shortener add_cors_route(config, "/short/create", "shortener") config.add_route("shortener_create", "/short/create", request_method="POST") config.add_route("shortener_get", "/short/{ref}", request_method="GET") # Geometry processing config.add_route("difference", "/difference", request_method="POST") # PDF report tool config.add_route("pdfreport", "/pdfreport/{layername}/{ids}", request_method="GET") # add routes for the "layers" web service add_cors_route(config, "/layers/*all", "layers") config.add_route( "layers_count", "/layers/{layer_id:\\d+}/count", request_method="GET" ) config.add_route( "layers_metadata", "/layers/{layer_id:\\d+}/md.xsd", request_method="GET", pregenerator=C2CPregenerator(role=True), ) config.add_route( "layers_read_many", "/layers/{layer_id:\\d+,?(\\d+,)*\\d*$}", request_method="GET") # supports URLs like /layers/1,2,3 config.add_route( "layers_read_one", "/layers/{layer_id:\\d+}/{feature_id}", request_method="GET") config.add_route( "layers_create", "/layers/{layer_id:\\d+}", request_method="POST", header=JSON_CONTENT_TYPE) config.add_route( "layers_update", "/layers/{layer_id:\\d+}/{feature_id}", request_method="PUT", header=JSON_CONTENT_TYPE) config.add_route( "layers_delete", "/layers/{layer_id:\\d+}/{feature_id}", request_method="DELETE") config.add_route( "layers_enumerate_attribute_values", "/layers/{layer_name}/values/{field_name}", request_method="GET", pregenerator=C2CPregenerator(), ) # there is no view corresponding to that route, it is to be used from # mako templates to get the root of the "layers" web service config.add_route("layers_root", "/layers/", request_method="HEAD") # Resource proxy (load external url, useful when loading non https content) config.add_route("resourceproxy", "/resourceproxy", request_method="GET") # scan view decorator for adding routes config.scan(ignore=["c2cgeoportal_geoportal.scripts", "c2cgeoportal_geoportal.wsgi_app"]) if "subdomains" in settings: # pragma: no cover config.registry.registerUtility( MultiDomainStaticURLInfo(), IStaticURLInfo) # add the static view (for static resources) _add_static_view(config, "static", "c2cgeoportal_geoportal:static") _add_static_view(config, "project", "c2cgeoportal_geoportal:project") add_admin_interface(config) add_static_view(config) # Handles the other HTTP errors raised by the views. Without that, # the client receives a status=200 without content. config.add_view(error_handler, context=HTTPException)
def test_cache_invalidation(self): init_region({"backend": "dogpile.cache.memory"}, "std") cache_version = get_cache_version() invalidate_region() assert cache_version != get_cache_version()