Exemple #1
0
def switch(request: HttpRequest) -> NoReturn:
    if request.method != "post":
        raise MethodNotAllowed

    current_theme = get_theme(request.session)
    new_theme = switch_theme(current_theme)
    request.session["theme"] = new_theme

    raise Redirect("/hello", headers=request.session.headers)
 def dispatch(self, http_method):
     try:
         request = HttpRequest.build(
             url=self.path,
             method=http_method,
             headers=dict(self.headers.items()),
             form_data=self.get_form_data(),
         )
         handler = self.get_handler(request)
         response = handler(request)
         self.respond(response)
     except HttpControl as ctl:
         self.respond(ctl.response)
     except MethodNotAllowed:
         self.handle_405()
     except Exception:
         self.handle_500()
Exemple #3
0
def update(request: HttpRequest) -> Union[HttpResponse, NoReturn]:
    if request.method != "post":
        raise Redirect("/hello")

    user_new = User.build(request.POST)
    profile_saved = request.session.get("profile", {})

    if not user_new.errors:
        profile_new = asdict(user_new)
        profile_saved.update(profile_new)
        request.session["profile"] = profile_saved
        raise Redirect("/hello", headers=request.session.headers)

    user_saved = User.build(profile_saved)

    context = _build_context(request, user_new=user_new, user_saved=user_saved)

    html = render_html("hello.html", context)
    return HttpResponse(status_code=400, body=html)
Exemple #4
0
def reset(request: HttpRequest) -> NoReturn:
    if request.method != "post":
        raise Redirect("/hello")

    request.session["profile"] = None
    raise Redirect("/hello", headers=request.session.headers)
Exemple #5
0
def test():
    defargs = dict(headers={}, GET={}, POST={}, content_type="text/html")

    data_set = {
        "": HttpRequest(**defargs),
        "/": HttpRequest(**defargs),
        "/images": HttpRequest(**merge(defargs, path="/images/")),
        "/images/": HttpRequest(**merge(defargs, path="/images/")),
        "/images/a": HttpRequest(**merge(defargs, path="/images/a/")),
        "/images/a/": HttpRequest(**merge(defargs, path="/images/a/")),
        "/images/image.jpg": HttpRequest(
            **merge(
                defargs,
                path="/images/",
                file_name="image.jpg",
                content_type="image/jpeg",
            )
        ),
        "/images/image.jpg/": HttpRequest(
            **merge(
                defargs,
                path="/images/",
                file_name="image.jpg",
                content_type="image/jpeg",
            )
        ),
        "/images/x/image.jpg": HttpRequest(
            **merge(
                defargs,
                path="/images/x/",
                file_name="image.jpg",
                content_type="image/jpeg",
            )
        ),
        "/images/x/image.jpg/": HttpRequest(
            **merge(
                defargs,
                path="/images/x/",
                file_name="image.jpg",
                content_type="image/jpeg",
            )
        ),
    }

    for path, expected in data_set.items():
        actual = HttpRequest.build(path)

        compare_requests(actual, expected)