File: utils.py

package info (click to toggle)
django-cors-headers 4.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 352 kB
  • sloc: python: 925; sh: 14; makefile: 3
file content (26 lines) | stat: -rw-r--r-- 700 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from __future__ import annotations

from collections.abc import Generator
from contextlib import contextmanager
from typing import Callable

from django.test.utils import modify_settings

from corsheaders.signals import check_request_enabled


def add_middleware(action: str, path: str) -> modify_settings:
    return modify_settings(**{"MIDDLEWARE": {action: path}})


def prepend_middleware(path: str) -> modify_settings:
    return add_middleware("prepend", path)


@contextmanager
def temporary_check_request_handler(handler: Callable[..., bool]) -> Generator[None]:
    check_request_enabled.connect(handler)
    try:
        yield
    finally:
        check_request_enabled.disconnect(handler)