Exemplo n.º 1
0
 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()
Exemplo n.º 2
0
 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()
Exemplo n.º 3
0
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# The views and conclusions contained in the software and documentation are those
# of the authors and should not be interpreted as representing official policies,
# either expressed or implied, of the FreeBSD Project.

from c2cgeoportal_geoportal.lib import caching

cache_region = caching.get_region()


def _get_layers_query(role_id, what):
    from c2cgeoportal_commons.models import DBSession, main

    q = DBSession.query(what)
    q = q.join(main.Layer.restrictionareas)
    q = q.join(main.RestrictionArea.roles)
    q = q.filter(main.Role.id == role_id)

    return q


def get_protected_layers_query(role_id, ogc_server_ids, what=None, version=1):
    from c2cgeoportal_commons.models import main
Exemplo n.º 4
0
# either expressed or implied, of the FreeBSD Project.

import logging
import sys
import urllib.parse

import requests
from pyramid.httpexceptions import HTTPBadGateway, exception_response
from pyramid.response import Response

from c2cgeoportal_geoportal.lib.caching import (NO_CACHE, PRIVATE_CACHE,
                                                PUBLIC_CACHE, get_region,
                                                set_common_headers)

log = logging.getLogger(__name__)
cache_region = get_region()


class Proxy(object):
    def __init__(self, request):
        self.request = request
        self.host_forward_host = request.registry.settings["host_forward_host"]
        self.http_options = self.request.registry.settings.get(
            "http_options", {})

    def _proxy(self,
               url,
               params=None,
               method=None,
               cache=False,
               body=None,
Exemplo n.º 5
0
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# The views and conclusions contained in the software and documentation are those
# of the authors and should not be interpreted as representing official policies,
# either expressed or implied, of the FreeBSD Project.


from urllib.parse import urljoin
import uuid

import pyramid.registry

from c2cgeoportal_geoportal.lib.caching import get_region

CACHE_REGION = get_region("std")


@CACHE_REGION.cache_on_arguments()
def get_cache_version():
    """Return a cache version that is regenerate after each cache invalidation"""
    return uuid.uuid4().hex


def version_cache_buster(request, subpath, kw):  # pragma: no cover
    del request  # unused
    return urljoin(get_cache_version() + "/", subpath), kw


class CachebusterTween:
    """ Get back the cachebuster URL. """
Exemplo n.º 6
0
from xml.sax.saxutils import XMLFilterBase, XMLGenerator

import defusedxml.expatreader
from owslib.wms import WebMapService
from pyramid.httpexceptions import HTTPBadGateway
import requests

from c2cgeoportal_geoportal.lib import (
    add_url_params,
    caching,
    get_ogc_server_wfs_url_ids,
    get_ogc_server_wms_url_ids,
)
from c2cgeoportal_geoportal.lib.layers import get_private_layers, get_protected_layers, get_writable_layers

CACHE_REGION = caching.get_region("std")
LOG = logging.getLogger(__name__)


@CACHE_REGION.cache_on_arguments()
def wms_structure(wms_url, host, request):
    url = urlsplit(wms_url)
    wms_url = add_url_params(wms_url, {
        "SERVICE": "WMS",
        "VERSION": "1.1.1",
        "REQUEST": "GetCapabilities"
    })

    # Forward request to target (without Host Header)
    headers = dict()
    if url.hostname == "localhost" and host is not None:  # pragma: no cover
Exemplo n.º 7
0
# of the authors and should not be interpreted as representing official policies,
# either expressed or implied, of the FreeBSD Project.

import logging
from typing import Dict  # noqa, pylint: disable=unused-import

from pyramid.httpexceptions import HTTPBadRequest
from sqlalchemy.orm.exc import NoResultFound

from c2cgeoportal_commons.models import DBSession
from c2cgeoportal_commons.models.main import OGCServer
from c2cgeoportal_geoportal.lib import get_url2
from c2cgeoportal_geoportal.lib.caching import get_region
from c2cgeoportal_geoportal.views.proxy import Proxy

CACHE_REGION = get_region()
LOG = logging.getLogger(__name__)


class OGCProxy(Proxy):

    params = {}  # type: Dict[str, str]

    def __init__(self, request, has_default_ogc_server=False):
        Proxy.__init__(self, request)

        # params hold the parameters we"re going to send to backend
        self.params = dict(self.request.params)

        # reset possible value of role_id and user_id
        if "role_id" in self.params:  # pragma: no cover
Exemplo n.º 8
0
import re
from string import Formatter
from typing import Any, Dict, Iterable, List, Optional, Set, Tuple, Union, cast

import dateutil
import pyramid.request
import pyramid.response
from pyramid.interfaces import IRoutePregenerator
from zope.interface import implementer

from c2cgeoportal_commons.lib.url import get_url2
from c2cgeoportal_geoportal.lib.cacheversion import get_cache_version
from c2cgeoportal_geoportal.lib.caching import get_region

LOG = logging.getLogger(__name__)
CACHE_REGION = get_region("std")
CACHE_REGION_OBJ = get_region("obj")


def get_types_map(types_array: List[Dict[str, Any]]) -> Dict[str, Dict[str, Any]]:
    """Get the type name of a metadata or a functionality."""
    return {type_["name"]: type_ for type_ in types_array}


def get_typed(
    name: str,
    value: str,
    types: Dict[str, Any],
    request: pyramid.request.Request,
    errors: Set[str],
    layer_name: Optional[str] = None,
Exemplo n.º 9
0

from pyramid.httpexceptions import HTTPBadRequest, HTTPInternalServerError
from pyramid.view import view_config

from geojson import Feature, FeatureCollection
from sqlalchemy import func, desc, or_, and_
from geoalchemy2.shape import to_shape
import re

from c2cgeoportal_geoportal import locale_negotiator
from c2cgeoportal_commons.models import DBSession
from c2cgeoportal_commons.models.main import FullTextSearch, Interface
from c2cgeoportal_geoportal.lib.caching import get_region, set_common_headers, NO_CACHE

cache_region = get_region()

IGNORED_CHARS_RE = re.compile(r"[()&|!:]")


class FullTextSearchView:

    def __init__(self, request):
        self.request = request
        set_common_headers(request, "fulltextsearch", NO_CACHE)
        self.settings = request.registry.settings.get("fulltextsearch", {})
        self.languages = self.settings.get("languages", {})

    @staticmethod
    @cache_region.cache_on_arguments()
    def _get_interface_id(interface):
Exemplo n.º 10
0
import random
import threading
import warnings
from typing import Dict, Iterable, List, Optional, Tuple, Union

import sqlalchemy.ext.declarative
from papyrus.geo_interface import GeoInterface
from sqlalchemy import Column, Integer, MetaData, Table
from sqlalchemy.exc import SAWarning
from sqlalchemy.orm import relationship
from sqlalchemy.orm.session import Session
from sqlalchemy.orm.util import class_mapper

from c2cgeoportal_geoportal.lib.caching import get_region

CACHE_REGION_OBJ = get_region("obj")
SQL_GEOMETRY_COLUMNS = """
    SELECT srid, type
    FROM geometry_columns
    WHERE
      f_table_schema = :table_schema AND
      f_table_name = :table_name AND
      f_geometry_column = :geometry_column
    """


class _AssociationProxy:
    # A specific "association proxy" implementation

    def __init__(self,
                 target: str,
Exemplo n.º 11
0
# either expressed or implied, of the FreeBSD Project.

import logging
from datetime import datetime, timedelta
from typing import Any, Dict, List, Union

import basicauth
import oauthlib.common
import oauthlib.oauth2
import pyramid.threadlocal

import c2cgeoportal_commons  # pylint: disable=unused-import
from c2cgeoportal_geoportal.lib.caching import get_region

LOG = logging.getLogger(__name__)
OBJECT_CACHE_REGION = get_region("obj")


class RequestValidator(oauthlib.oauth2.RequestValidator):  # type: ignore
    """The oauth2 request validator implementation."""
    def __init__(self, authorization_expires_in: int) -> None:
        # in minutes
        self.authorization_expires_in = authorization_expires_in

    def authenticate_client(
        self,
        request: oauthlib.common.Request,
        *args: Any,
        **kwargs: Any,
    ) -> bool:
        """
Exemplo n.º 12
0
from xml.sax import saxutils
from xml.sax.saxutils import XMLFilterBase, XMLGenerator
from xml.sax.xmlreader import InputSource
import defusedxml.expatreader

from pyramid.httpexceptions import HTTPBadGateway

from owslib.wms import WebMapService

from c2cgeoportal_geoportal.lib import caching, get_protected_layers_query, \
    get_writable_layers_query, add_url_params, get_ogc_server_wms_url_ids,\
    get_ogc_server_wfs_url_ids
from c2cgeoportal_commons.models import DBSession
from c2cgeoportal_commons.models.main import LayerWMS, OGCServer

cache_region = caching.get_region()
log = logging.getLogger(__name__)


@cache_region.cache_on_arguments()
def get_protected_layers(role_id, ogc_server_ids):
    q = get_protected_layers_query(role_id, ogc_server_ids, what=LayerWMS, version=2)
    results = q.all()
    DBSession.expunge_all()
    return {r.id: r for r in results}


@cache_region.cache_on_arguments()
def get_private_layers(ogc_server_ids):
    q = DBSession.query(LayerWMS) \
        .filter(LayerWMS.public.is_(False)) \