diff --git a/apps/templates/.keep b/apps/templates/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/poetry.lock b/poetry.lock index 5858235..391b3be 100644 --- a/poetry.lock +++ b/poetry.lock @@ -325,6 +325,20 @@ develop = ["coverage[toml] (>=5.0a4)", "furo (>=2021.8.17b43,<2021.9.dev0)", "py docs = ["furo (>=2021.8.17b43,<2021.9.dev0)", "sphinx (>=3.5.0)", "sphinx-notfound-page"] testing = ["coverage[toml] (>=5.0a4)", "pytest (>=4.6.11)"] +[[package]] +name = "django-guardian" +version = "2.4.0" +description = "Implementation of per object permissions for Django." +optional = false +python-versions = ">=3.5" +files = [ + {file = "django-guardian-2.4.0.tar.gz", hash = "sha256:c58a68ae76922d33e6bdc0e69af1892097838de56e93e78a8361090bcd9f89a0"}, + {file = "django_guardian-2.4.0-py3-none-any.whl", hash = "sha256:440ca61358427e575323648b25f8384739e54c38b3d655c81d75e0cd0d61b697"}, +] + +[package.dependencies] +Django = ">=2.2" + [[package]] name = "django-stubs" version = "4.2.4" @@ -1214,4 +1228,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "967a300f6e56903859bf57de51046e3b4a61148fa292662d8da1b7e420727b3c" +content-hash = "2ea75b49f99ad42defebffde188bf07a9beb5b8e72907a5ac25b7bf8f294ad42" diff --git a/pyproject.toml b/pyproject.toml index d47c19c..9ee6f3e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,6 +11,7 @@ python = "^3.11" django = "^4.2.5" psycopg2 = "^2.9.7" django-environ = "^0.11.2" +django-guardian = "^2.4.0" [tool.poetry.group.dev.dependencies] pre-commit = "^3.4.0" diff --git a/server/settings/__init__.py b/server/settings/__init__.py index 0ddd0b5..5e9c0fe 100644 --- a/server/settings/__init__.py +++ b/server/settings/__init__.py @@ -1,13 +1,14 @@ -from os.path import join from pathlib import Path from environ import Env BASE_DIR = Path(__file__).parent.parent.parent -APPS_DIR = BASE_DIR / "apps" # Load environment variables from config/.env file. See # https://django-environ.readthedocs.io/en/latest/ env = Env() -env.read_env(join(BASE_DIR, "config", ".env")) +READ_DOTENV = env.bool("DJANGO_READ_DOTENV", default=False) + +if READ_DOTENV: + env.read_env(BASE_DIR / "config" / ".env") diff --git a/server/settings/base.py b/server/settings/base.py index ac2e850..5afbdc0 100644 --- a/server/settings/base.py +++ b/server/settings/base.py @@ -1,7 +1,7 @@ from os.path import join from typing import List -from server.settings import APPS_DIR, BASE_DIR, env +from server.settings import BASE_DIR, env ###################### # General Settings # @@ -24,11 +24,6 @@ LOCALE_PATHS = [join(BASE_DIR, "locale")] -# Default primary key field type -# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field - -DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" - ################# # Middlewares # ################# @@ -49,6 +44,11 @@ DATABASES = {"default": env.db()} +# Default primary key field type +# https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + ########## # URLs # ########## @@ -70,7 +70,9 @@ "django.contrib.staticfiles", ] -THIRD_PARTY_APPS: List[str] = [] +THIRD_PARTY_APPS = [ + "guardian", +] LOCAL_APPS: List[str] = [] @@ -100,7 +102,7 @@ TEMPLATES = [ { "BACKEND": "django.template.backends.django.DjangoTemplates", - "DIRS": [APPS_DIR / "templates"], + "DIRS": [BASE_DIR / "templates"], "APP_DIRS": True, "OPTIONS": { "context_processors": [ @@ -118,3 +120,24 @@ ################## STATIC_URL = "/static/" + +#################### +# Authentication # +#################### + +AUTHENTICATION_BACKENDS = [ + "django.contrib.auth.backends.ModelBackend", + "guardian.backends.ObjectPermissionBackend", +] + +LOGIN_REDIRECT_URL = "home" + +LOGOUT_REDIRECT_URL = "home" + +LOGIN_URL = "login" + +##################### +# Django Guardian # +##################### + +ANONYMOUS_USER_NAME = None diff --git a/server/urls.py b/server/urls.py index 083932c..bc168b6 100644 --- a/server/urls.py +++ b/server/urls.py @@ -1,6 +1,11 @@ from django.contrib import admin -from django.urls import path +from django.urls import include, path +from django.views.generic import TemplateView + +home_view = TemplateView.as_view(template_name="pages/home.html") urlpatterns = [ path("admin/", admin.site.urls), + path("", include("django.contrib.auth.urls")), + path("", home_view, name="home"), ] diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..1f5d613 --- /dev/null +++ b/templates/base.html @@ -0,0 +1,23 @@ +{% load static i18n %} +{% get_current_language as LANGUAGE_CODE %} + + +
+ + +Welcome, {{ user.username }}!
+ {% else %} +Welcome, new user!
+ {% endif %} +{% endblock content %} diff --git a/templates/registration/login.html b/templates/registration/login.html new file mode 100644 index 0000000..069a545 --- /dev/null +++ b/templates/registration/login.html @@ -0,0 +1,38 @@ +{% extends "base.html" %} + +{% block title %}Login{% endblock title %} + +{% block content %} +{% if form.errors %} +Your username and password didn't match. Please try again.
+{% endif %} + +{% if next %} + {% if user.is_authenticated %} +Your account doesn't have access to this page. To proceed, + please login with an account that has access.
+ {% else %} +Please login to see this page.
+ {% endif %} +{% endif %} + + + +{# Assumes you set up the password_reset view in your URLconf #} + +{% endblock %}