Exemple #1
0
 def _functionality(
     self
 ) -> Dict[str, List[Union[str, int, float, bool, List[Any], Dict[str,
                                                                  Any]]]]:
     functionality = {}
     for func_ in get_setting(self.settings,
                              ("functionalities", "available_in_templates"),
                              []):
         functionality[func_] = get_functionality(func_, self.request,
                                                  is_intranet(self.request))
     return functionality
Exemple #2
0
    def capabilities(self) -> pyramid.response.Response:
        """ Get print capabilities. """

        templates = get_functionality("print_template", self.request,
                                      is_intranet(self.request))

        # get query string
        params = dict(self.request.params)
        query_string = urllib.parse.urlencode(params)

        resp, content = self._capabilities(templates, query_string,
                                           self.request.method)

        return self._build_response(resp, content, Cache.PRIVATE, "print")
    def capabilities(self) -> pyramid.response.Response:
        """Get print capabilities."""
        templates = get_functionality("print_template", self.request,
                                      is_intranet(self.request))

        # get query string
        params = dict(self.request.params)
        query_string = urllib.parse.urlencode(params)

        resp, content = self._capabilities(templates, query_string,
                                           self.request.method,
                                           self.request.referrer)

        response = self._build_response(resp, content, Cache.PRIVATE, "print")
        # Mapfish print will check the referer header to return the capabilities.
        response.vary += ("Referer", )
        return response
Exemple #4
0
 def _user(self, user: Optional[static.User] = None) -> Dict[str, Any]:
     result = {
         "functionalities": self._functionality(),
         "is_intranet": is_intranet(self.request),
         "two_factor_enable": self.two_factor_auth,
     }
     user = self.request.user if user is None else user
     if user is not None:
         result.update({
             "username":
             user.username,
             "email":
             user.email,
             "roles": [{
                 "name": r.name,
                 "id": r.id
             } for r in user.roles],
         })
     return result
Exemple #5
0
def get_mapserver_substitution_params(
        request: pyramid.request.Request) -> Dict[str, str]:
    params: Dict[str, str] = {}
    mss = get_functionality("mapserver_substitution", request,
                            is_intranet(request))
    if mss:
        for s_ in mss:
            s = cast(str, s_)
            index = s.find("=")
            if index > 0:
                attribute = "s_" + s[:index]
                value = s[index + 1:]
                if attribute in params:
                    params[attribute] += "," + value
                else:
                    params[attribute] = value
            else:
                LOG.warning(
                    "Mapserver Substitution '%s' does not "
                    "respect pattern: <attribute>=<value>", s)
    return params
Exemple #6
0
    def themes(self) -> Dict[str, Union[Dict[str, Dict[str, Any]], List[str]]]:
        interface = self.request.params.get("interface", "desktop")
        sets = self.request.params.get("set", "all")
        min_levels = int(self.request.params.get("min_levels", 1))
        group = self.request.params.get("group")
        background_layers_group = self.request.params.get("background")

        set_common_headers(self.request, "themes", Cache.PRIVATE)

        async def get_theme() -> Dict[str, Union[Dict[str, Any], List[str]]]:
            export_themes = sets in ("all", "themes")
            export_group = group is not None and sets in ("all", "group")
            export_background = background_layers_group is not None and sets in ("all", "background")

            result: Dict[str, Union[Dict[str, Any], List[Any]]] = {}
            all_errors: Set[str] = set()
            LOG.debug("Start preload")
            start_time = time.time()
            await self.preload(all_errors)
            LOG.debug("End preload")
            # Don't log if it looks to be already preloaded.
            if (time.time() - start_time) > 1:
                LOG.info("Do preload in %.3fs.", time.time() - start_time)
            result["ogcServers"] = {}
            for ogc_server in models.DBSession.query(main.OGCServer).all():
                nb_layers = (
                    models.DBSession.query(sqlalchemy.func.count(main.LayerWMS.id))
                    .filter(main.LayerWMS.ogc_server_id == ogc_server.id)
                    .one()
                )
                if nb_layers[0] == 0:
                    # QGIS Server langing page requires an OGC server that can't be used here.
                    continue

                url_internal_wfs, url, url_wfs = self.get_url_internal_wfs(ogc_server, all_errors)

                attributes = None
                namespace = None
                if ogc_server.wfs_support:
                    assert url_internal_wfs
                    attributes, namespace, errors = await self._get_features_attributes(
                        url_internal_wfs, ogc_server.name
                    )
                    # Create a local copy (don't modify the cache)
                    if attributes is not None:
                        attributes = dict(attributes)
                    all_errors |= errors

                    all_private_layers = get_private_layers([ogc_server.id]).values()
                    protected_layers_name = [
                        layer.name for layer in get_protected_layers(self.request, [ogc_server.id]).values()
                    ]
                    private_layers_name: List[str] = []
                    for layers in [
                        v.layer for v in all_private_layers if v.name not in protected_layers_name
                    ]:
                        private_layers_name.extend(layers.split(","))

                    if attributes is not None:
                        for name in private_layers_name:
                            if name in attributes:
                                del attributes[name]

                result["ogcServers"][ogc_server.name] = {
                    "url": url.url() if url else None,
                    "urlWfs": url_wfs.url() if url_wfs else None,
                    "type": ogc_server.type,
                    "credential": ogc_server.auth != main.OGCSERVER_AUTH_NOAUTH,
                    "imageType": ogc_server.image_type,
                    "wfsSupport": ogc_server.wfs_support,
                    "isSingleTile": ogc_server.is_single_tile,
                    "namespace": namespace,
                    "attributes": attributes,
                }
            if export_themes:
                themes, errors = await self._themes(interface, True, min_levels)

                result["themes"] = themes
                all_errors |= errors

            if export_group:
                exported_group, errors = await self._get_group(group, interface)
                if exported_group is not None:
                    result["group"] = exported_group
                all_errors |= errors

            if export_background:
                exported_group, errors = await self._get_group(background_layers_group, interface)
                result["background_layers"] = exported_group["children"] if exported_group is not None else []
                all_errors |= errors

            result["errors"] = list(all_errors)
            if all_errors:
                LOG.info("Theme errors:\n%s", "\n".join(all_errors))
            return result

        @CACHE_REGION.cache_on_arguments()  # type: ignore
        def get_theme_anonymous(
            intranet: bool,
            interface: str,
            sets: str,
            min_levels: str,
            group: str,
            background_layers_group: str,
            host: str,
        ) -> Dict[str, Union[Dict[str, Dict[str, Any]], List[str]]]:
            # Only for cache key
            del intranet, interface, sets, min_levels, group, background_layers_group, host
            return asyncio.run(get_theme())

        if self.request.user is None:
            return cast(
                Dict[str, Union[Dict[str, Dict[str, Any]], List[str]]],
                get_theme_anonymous(
                    is_intranet(self.request),
                    interface,
                    sets,
                    min_levels,
                    group,
                    background_layers_group,
                    self.request.headers.get("Host"),
                ),
            )
        return asyncio.run(get_theme())
Exemple #7
0
    def themes(self):
        interface = self.request.params.get("interface", "desktop")
        sets = self.request.params.get("set", "all")
        min_levels = int(self.request.params.get("min_levels", 1))
        group = self.request.params.get("group")
        background_layers_group = self.request.params.get("background")

        set_common_headers(self.request, "themes", PRIVATE_CACHE)

        def get_theme():
            export_themes = sets in ("all", "themes")
            export_group = group is not None and sets in ("all", "group")
            export_background = background_layers_group is not None and sets in (
                "all", "background")

            result: Dict[str, Union[Dict[str, Dict[str, Any]], List[str]]] = {}
            all_errors: Set[str] = set()
            LOG.debug("Start preload")
            start_time = time.time()
            asyncio.run(self.preload(all_errors))
            LOG.debug("End preload")
            LOG.info("Do preload in %.3fs.", time.time() - start_time)
            result["ogcServers"] = {}
            for ogc_server in models.DBSession.query(main.OGCServer).all():
                url_internal_wfs, url, url_wfs = self.get_url_internal_wfs(
                    ogc_server, all_errors)

                attributes = None
                namespace = None
                if ogc_server.wfs_support:
                    attributes, namespace, errors = self._get_features_attributes(
                        url_internal_wfs)
                    all_errors |= errors

                    all_private_layers = get_private_layers([ogc_server.id
                                                             ]).values()
                    protected_layers_name = [
                        l.name for l in get_protected_layers(
                            self.request, [ogc_server.id]).values()
                    ]
                    private_layers_name: List[str] = []
                    for layers in [
                            v.layer for v in all_private_layers
                            if v.name not in protected_layers_name
                    ]:
                        private_layers_name.extend(layers.split(","))

                    if attributes is not None:
                        for name in private_layers_name:
                            if name in attributes:
                                del attributes[name]

                result["ogcServers"][ogc_server.name] = {
                    "url": url,
                    "urlWfs": url_wfs,
                    "type": ogc_server.type,
                    "credential":
                    ogc_server.auth != main.OGCSERVER_AUTH_NOAUTH,
                    "imageType": ogc_server.image_type,
                    "wfsSupport": ogc_server.wfs_support,
                    "isSingleTile": ogc_server.is_single_tile,
                    "namespace": namespace,
                    "attributes": attributes,
                }
            if export_themes:
                themes, errors = self._themes(interface, True, min_levels)

                result["themes"] = themes
                all_errors |= errors

            if export_group:
                exported_group, errors = self._get_group(group, interface)
                if exported_group is not None:
                    result["group"] = exported_group
                all_errors |= errors

            if export_background:
                exported_group, errors = self._get_group(
                    background_layers_group, interface)
                result["background_layers"] = exported_group[
                    "children"] if exported_group is not None else []
                all_errors |= errors

            result["errors"] = list(all_errors)
            if all_errors:
                LOG.info("Theme errors:\n%s", "\n".join(all_errors))
            return result

        @CACHE_REGION.cache_on_arguments()
        def get_theme_anonymous(intranet, interface, sets, min_levels, group,
                                background_layers_group, host):
            # Only for cache key
            del intranet, interface, sets, min_levels, group, background_layers_group, host
            return get_theme()

        if self.request.user is None:
            return get_theme_anonymous(
                is_intranet(self.request),
                interface,
                sets,
                min_levels,
                group,
                background_layers_group,
                self.request.headers.get("Host"),
            )
        return get_theme()