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() if not (request_uri == "/" or request_uri.startswith(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) if request_uri.startswith(prefix + "/static/") or \ request_uri.endswith("favicon.ico") or request_uri.endswith("favicon.svg"): output, content_type, headers = webframe.handle_static(request_uri) return webframe.send_response( environ, start_response, webframe.Response(content_type, "200 OK", output, 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.Response("text/html", "200 OK", doc.getvalue().encode("utf-8"), []))
def our_application_txt(environ: Dict[str, Any], start_response: 'StartResponse', relations: areas.Relations, request_uri: str) -> Iterable[bytes]: """Dispatches plain text requests based on their URIs.""" content_type = "text/plain" headers: List[Tuple[str, str]] = [] prefix = config.Config.get_uri_prefix() _, _, ext = request_uri.partition('.') chkl = ext == "chkl" if request_uri.startswith(prefix + "/missing-streets/"): output, relation_name = missing_streets_view_txt( relations, request_uri, chkl) if chkl: content_type = "application/octet-stream" headers.append(("Content-Disposition", 'attachment;filename="' + relation_name + '.txt"')) elif request_uri.startswith(prefix + "/additional-streets/"): output, relation_name = wsgi_additional.additional_streets_view_txt( relations, request_uri, chkl) if chkl: content_type = "application/octet-stream" headers.append(("Content-Disposition", 'attachment;filename="' + relation_name + '.txt"')) else: # assume prefix + "/missing-housenumbers/" if chkl: output, relation_name = missing_housenumbers_view_chkl( relations, request_uri) content_type = "application/octet-stream" headers.append(("Content-Disposition", 'attachment;filename="' + relation_name + '.txt"')) elif request_uri.endswith("robots.txt"): output = util.get_content(config.get_abspath("data"), "robots.txt").decode("utf-8") else: # assume txt output = missing_housenumbers_view_txt(relations, request_uri) output_bytes = output.encode("utf-8") response_properties = webframe.Response(content_type, "200 OK", output_bytes, headers) return webframe.send_response(environ, start_response, response_properties)
def our_application_json(environ: Dict[str, Any], start_response: 'StartResponse', relations: areas.Relations, request_uri: str) -> Iterable[bytes]: """Dispatches json requests based on their URIs.""" content_type = "application/json" headers: List[Tuple[str, str]] = [] prefix = config.Config.get_uri_prefix() if request_uri.startswith(prefix + "/streets/"): output = streets_update_result_json(relations, request_uri) elif request_uri.startswith(prefix + "/street-housenumbers/"): output = street_housenumbers_update_result_json(relations, request_uri) elif request_uri.startswith(prefix + "/missing-housenumbers/"): output = missing_housenumbers_update_result_json( relations, request_uri) else: # Assume that request_uri starts with prefix + "/missing-streets/". output = missing_streets_update_result_json(relations, request_uri) output_bytes = output.encode("utf-8") response = webframe.Response(content_type, "200 OK", output_bytes, headers) return webframe.send_response(environ, start_response, response)
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()