Ejemplo n.º 1
0
def our_application(
        environ: Dict[str, Any],
        start_response: 'StartResponse'
) -> Iterable[bytes]:
    """Dispatches the request based on its URI."""
    util.set_locale()

    language = util.setup_localization(environ)

    relations = areas.Relations(config.Config.get_workdir())

    request_uri = webframe.get_request_uri(environ, relations)
    _, _, ext = request_uri.partition('.')

    if ext in ("txt", "chkl"):
        return our_application_txt(environ, start_response, relations, request_uri)

    prefix = config.Config.get_uri_prefix()
    found = request_uri == "/" or request_uri.startswith(prefix)
    if not found:
        doc = webframe.handle_404()
        return webframe.send_response(environ,
                                      start_response,
                                      webframe.ResponseProperties("text/html", "404 Not Found"),
                                      doc.getvalue().encode("utf-8"),
                                      [])

    if request_uri.startswith(prefix + "/static/") or request_uri.endswith("favicon.ico"):
        output, content_type, extra_headers = webframe.handle_static(request_uri)
        return webframe.send_response(environ,
                                      start_response,
                                      webframe.ResponseProperties(content_type, "200 OK"),
                                      output,
                                      extra_headers)

    if ext == "json":
        return wsgi_json.our_application_json(environ, start_response, relations, request_uri)

    doc = yattag.doc.Doc()
    util.write_html_header(doc)
    with doc.tag("html", lang=language):
        write_html_head(doc, get_html_title(request_uri))

        with doc.tag("body"):
            no_such_relation = webframe.check_existing_relation(relations, request_uri)
            handler = get_handler(request_uri)
            if no_such_relation.getvalue():
                doc.asis(no_such_relation.getvalue())
            elif handler:
                doc.asis(handler(relations, request_uri).getvalue())
            elif request_uri.startswith(prefix + "/webhooks/github"):
                doc.asis(handle_github_webhook(environ).getvalue())
            else:
                doc.asis(handle_main(request_uri, relations).getvalue())

    return webframe.send_response(environ,
                                  start_response,
                                  webframe.ResponseProperties("text/html", "200 OK"),
                                  doc.getvalue().encode("utf-8"),
                                  [])
Ejemplo n.º 2
0
def handle_404() -> yattag.doc.Doc:
    """Displays a not-found page."""
    doc = yattag.doc.Doc()
    util.write_html_header(doc)
    with doc.tag("html"):
        with doc.tag("body"):
            with doc.tag("h1"):
                doc.text(_("Not Found"))
            with doc.tag("p"):
                doc.text(_("The requested URL was not found on this server."))
    return doc
Ejemplo n.º 3
0
def our_application(
        environ: Dict[str, Any],
        start_response: 'StartResponse'
) -> Iterable[bytes]:
    """Dispatches the request based on its URI."""
    config = get_config()
    if config.has_option("wsgi", "locale"):
        ui_locale = config.get("wsgi", "locale")
    else:
        ui_locale = "hu_HU.UTF-8"
    try:
        locale.setlocale(locale.LC_ALL, ui_locale)
    except locale.Error:
        # Ignore, this happens only on the cut-down CI environment.
        pass

    language = util.setup_localization(environ)
    if not language:
        language = "hu"

    request_uri = get_request_uri(environ)
    _ignore, _ignore, ext = request_uri.partition('.')

    relations = helpers.Relations(get_datadir(), helpers.get_workdir(config))

    if ext == "txt":
        return our_application_txt(start_response, relations, request_uri)

    if request_uri.startswith("/osm/static/"):
        output, content_type = handle_static(request_uri)
        return send_response(start_response, content_type, "200 OK", output)

    doc = yattag.Doc()
    util.write_html_header(doc)
    with doc.tag("html", lang=language):
        write_html_head(doc, get_html_title(request_uri))

        with doc.tag("body"):
            if request_uri.startswith("/osm/streets/"):
                doc.asis(handle_streets(relations, request_uri).getvalue())
            elif request_uri.startswith("/osm/missing-streets/"):
                doc.asis(handle_missing_streets(relations, request_uri).getvalue())
            elif request_uri.startswith("/osm/street-housenumbers/"):
                doc.asis(handle_street_housenumbers(relations, request_uri).getvalue())
            elif request_uri.startswith("/osm/missing-housenumbers/"):
                doc.asis(handle_missing_housenumbers(relations, request_uri).getvalue())
            elif request_uri.startswith("/osm/webhooks/github"):
                doc.asis(handle_github_webhook(environ).getvalue())
            else:
                doc.asis(handle_main(request_uri, relations).getvalue())

    return send_response(start_response, "text/html", "200 OK", doc.getvalue())
Ejemplo n.º 4
0
def handle_exception(environ: Dict[str, Any],
                     start_response: 'StartResponse') -> Iterable[bytes]:
    """Displays an unhandled exception on the page."""
    status = '500 Internal Server Error'
    path_info = environ.get("PATH_INFO")
    request_uri = path_info
    doc = yattag.doc.Doc()
    util.write_html_header(doc)
    with doc.tag("pre"):
        doc.text(
            _("Internal error when serving {0}").format(request_uri) + "\n")
        doc.text(traceback.format_exc())
    return send_response(start_response, "text/html", status, doc.getvalue(),
                         [])
Ejemplo n.º 5
0
def our_application(environ: Dict[str, Any],
                    start_response: 'StartResponse') -> Iterable[bytes]:
    """Dispatches the request based on its URI."""
    util.set_locale()

    language = util.setup_localization(environ)

    relations = areas.Relations(util.Config.get_workdir())

    request_uri = get_request_uri(environ, relations)
    _, _, ext = request_uri.partition('.')

    if ext in ("txt", "chkl"):
        return our_application_txt(start_response, relations, request_uri)

    prefix = util.Config.get_uri_prefix()
    if request_uri.startswith(prefix + "/static/"):
        output, content_type = webframe.handle_static(request_uri)
        return webframe.send_response(start_response, content_type, "200 OK",
                                      output, [])

    doc = yattag.doc.Doc()
    util.write_html_header(doc)
    with doc.tag("html", lang=language):
        write_html_head(doc, get_html_title(request_uri))

        with doc.tag("body"):
            no_such_relation = check_existing_relation(relations, request_uri)
            handler = get_handler(request_uri)
            if no_such_relation.getvalue():
                doc.asis(no_such_relation.getvalue())
            elif handler:
                doc.asis(handler(relations, request_uri).getvalue())
            elif request_uri.startswith(prefix + "/webhooks/github"):
                doc.asis(handle_github_webhook(environ).getvalue())
            else:
                doc.asis(handle_main(request_uri, relations).getvalue())

    return webframe.send_response(start_response, "text/html", "200 OK",
                                  doc.getvalue(), [])
Ejemplo n.º 6
0
def our_application(environ: Dict[str, Any], start_response: 'StartResponse',
                    ctx: context.Context) -> Tuple[Iterable[bytes], str]:
    """Dispatches the request based on its URI."""
    try:
        language = util.setup_localization(environ, ctx)

        relations = areas.Relations(ctx)

        request_uri = webframe.get_request_uri(environ, ctx, relations)
        _, _, ext = request_uri.partition('.')

        if ext in ("txt", "chkl"):
            return our_application_txt(environ, start_response, ctx, relations,
                                       request_uri), str()

        if not (request_uri == "/"
                or request_uri.startswith(ctx.get_ini().get_uri_prefix())):
            doc = webframe.handle_404()
            response = webframe.Response("text/html", "404 Not Found",
                                         doc.getvalue().encode("utf-8"), [])
            return webframe.send_response(environ, start_response,
                                          response), str()

        if request_uri.startswith(ctx.get_ini().get_uri_prefix() + "/static/") or \
                request_uri.endswith("favicon.ico") or request_uri.endswith("favicon.svg"):
            output, content_type, headers = webframe.handle_static(
                ctx, request_uri)
            return webframe.send_response(
                environ, start_response,
                webframe.Response(content_type, "200 OK", output,
                                  headers)), str()

        if ext == "json":
            return wsgi_json.our_application_json(environ, start_response, ctx,
                                                  relations,
                                                  request_uri), str()

        doc = yattag.doc.Doc()
        util.write_html_header(doc)
        with doc.tag("html", lang=language):
            write_html_head(ctx, doc, get_html_title(request_uri))

            with doc.tag("body"):
                no_such_relation = webframe.check_existing_relation(
                    ctx, relations, request_uri)
                handler = get_handler(ctx, request_uri)
                if no_such_relation.getvalue():
                    doc.asis(no_such_relation.getvalue())
                elif handler:
                    doc.asis(handler(ctx, relations, request_uri).getvalue())
                elif request_uri.startswith(ctx.get_ini().get_uri_prefix() +
                                            "/webhooks/github"):
                    doc.asis(
                        webframe.handle_github_webhook(environ,
                                                       ctx).getvalue())
                else:
                    doc.asis(
                        handle_main(request_uri, ctx, relations).getvalue())

        err = ctx.get_unit().make_error()
        if err:
            return [], err
        return webframe.send_response(
            environ, start_response,
            webframe.Response("text/html", "200 OK",
                              doc.getvalue().encode("utf-8"), [])), err
    # pylint: disable=broad-except
    except Exception:  # pragma: no cover
        return [], traceback.format_exc()