From 9080425e1ee9e51768d567b69b964a4b2db83570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Wed, 25 Jan 2023 17:48:17 +0100 Subject: [PATCH] Add django-cors-headers --- poetry.lock | 17 ++++++++++++++++- pyproject.toml | 1 + timetracker/settings.py | 18 ++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/poetry.lock b/poetry.lock index 23988d5..1003694 100644 --- a/poetry.lock +++ b/poetry.lock @@ -220,6 +220,21 @@ tzdata = {version = "*", markers = "sys_platform == \"win32\""} argon2 = ["argon2-cffi (>=19.1.0)"] bcrypt = ["bcrypt"] +[[package]] +name = "django-cors-headers" +version = "3.13.0" +description = "django-cors-headers is a Django application for handling the server headers required for Cross-Origin Resource Sharing (CORS)." +category = "main" +optional = false +python-versions = ">=3.7" +files = [ + {file = "django-cors-headers-3.13.0.tar.gz", hash = "sha256:f9dc6b4e3f611c3199700b3e5f3398c28757dcd559c2f82932687f3d0443cfdf"}, + {file = "django_cors_headers-3.13.0-py3-none-any.whl", hash = "sha256:37e42883b5f1f2295df6b4bba96eb2417a14a03270cb24b2a07f021cd4487cf4"}, +] + +[package.dependencies] +Django = ">=3.2" + [[package]] name = "django-extensions" version = "3.2.1" @@ -1334,4 +1349,4 @@ testing = ["flake8 (<5)", "func-timeout", "jaraco.functools", "jaraco.itertools" [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "4ca6b31c654012f0d2b72bd44d3f40d986b183d6dd731c3a2aaed9a733032c08" +content-hash = "7a7ba3831802cf91b722b956817a0606b7b8f97724b1a23e5e581657fb34ea19" diff --git a/pyproject.toml b/pyproject.toml index 0fbf0c7..19e9f58 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,7 @@ uvicorn = "^0.20.0" pandas = "^1.5.2" matplotlib = "^3.6.3" django-rest-framework = "^0.1.0" +django-cors-headers = "^3.13.0" [tool.poetry.group.dev.dependencies] black = "^22.12.0" diff --git a/timetracker/settings.py b/timetracker/settings.py index efb55a3..af35a78 100644 --- a/timetracker/settings.py +++ b/timetracker/settings.py @@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/4.1/ref/settings/ import os from pathlib import Path +from corsheaders.defaults import default_headers, default_methods # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -39,6 +40,7 @@ INSTALLED_APPS = [ "django.contrib.messages", "django.contrib.staticfiles", "rest_framework", + "corsheaders", ] if DEBUG: @@ -46,6 +48,7 @@ if DEBUG: INSTALLED_APPS.append("django.contrib.admin") MIDDLEWARE = [ + "corsheaders.middleware.CorsMiddleware", "django.middleware.security.SecurityMiddleware", "django.contrib.sessions.middleware.SessionMiddleware", "django.middleware.common.CommonMiddleware", @@ -156,3 +159,18 @@ REST_FRAMEWORK = { "rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly" ] } + +FRONTEND_ROOT = os.path.abspath(os.path.join(BASE_DIR, "..", "frontend", "dist")) +CORS_ORIGIN_ALLOW_ALL = True +CORS_ALLOW_CREDENTIALS = True + +CORS_ALLOW_HEADERS = list(default_headers) + [ + "Accept-Language", + "Connection", + "Host", + "Origin", + "Referer", + "Sec-Fetch-Dest", + "Sec-Fetch-Mode", + "Sec-Fetch-Site", +]