diff --git a/knox_project/asgi.py b/knox_project/asgi.py new file mode 100644 index 0000000..2aa99bc --- /dev/null +++ b/knox_project/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for project project. + +It exposes the ASGI callable as a module-level variable named ``application``. + +For more information on this file, see +https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "knox_project.settings") + +application = get_asgi_application() diff --git a/knox_project/settings.py b/knox_project/settings.py index a6b07b0..08a22f4 100644 --- a/knox_project/settings.py +++ b/knox_project/settings.py @@ -1,56 +1,83 @@ -import os +""" +Test project for Django REST Knox +""" -BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -SECRET_KEY = 'gcr$j^h2@d@sd(f#-ihtv6*hg7qno$otw62^*rzcf0tk2wz&sb' +from pathlib import Path + +BASE_DIR = Path(__file__).resolve().parent.parent +SECRET_KEY = "i-am-a-super-secret-key" DEBUG = True -ALLOWED_HOSTS = [] -INSTALLED_APPS = ( - 'django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'rest_framework', - 'knox', -) - -MIDDLEWARE_CLASSES = ( - 'django.contrib.sessions.middleware.SessionMiddleware', - 'django.middleware.common.CommonMiddleware', - 'django.middleware.csrf.CsrfViewMiddleware', - 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.middleware.security.SecurityMiddleware', -) - -ROOT_URLCONF = 'knox_project.urls' +ALLOWED_HOSTS = ["*"] + +# Application definition +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "rest_framework", + "knox", +] + +MIDDLEWARE = [ + "django.middleware.security.SecurityMiddleware", + "django.contrib.sessions.middleware.SessionMiddleware", + "django.middleware.common.CommonMiddleware", + "django.middleware.csrf.CsrfViewMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware", + "django.middleware.clickjacking.XFrameOptionsMiddleware", +] + +ROOT_URLCONF = "knox_project.urls" TEMPLATES = [ { - 'BACKEND': 'django.template.backends.django.DjangoTemplates', - 'DIRS': [], - 'APP_DIRS': True, - 'OPTIONS': { - 'context_processors': [ - 'django.template.context_processors.debug', - 'django.template.context_processors.request', - 'django.contrib.auth.context_processors.auth', + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", ], }, }, ] -WSGI_APPLICATION = 'knox_project.wsgi.application' - DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": "db.sqlite3", } } -LANGUAGE_CODE = 'en-us' -TIME_ZONE = 'UTC' +WSGI_APPLICATION = "knox_project.wsgi.application" + +LANGUAGE_CODE = "en-us" +TIME_ZONE = "UTC" USE_I18N = True USE_TZ = True -STATIC_URL = '/static/' +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/5.0/howto/static-files/ + +STATIC_URL = "static/" -KNOX_TOKEN_MODEL = 'knox.AuthToken' +# Default primary key field type +# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" + +# Django REST Knox settings +REST_FRAMEWORK = { + "DEFAULT_AUTHENTICATION_CLASSES": [ + "rest_framework.authentication.BasicAuthentication", + "rest_framework.authentication.SessionAuthentication", + "knox.auth.TokenAuthentication", + ] +} diff --git a/knox_project/urls.py b/knox_project/urls.py index bd44c3a..99959fb 100644 --- a/knox_project/urls.py +++ b/knox_project/urls.py @@ -1,8 +1,12 @@ +from django.conf import settings +from django.conf.urls.static import static +from django.contrib import admin from django.urls import include, path from .views import RootView urlpatterns = [ + path('admin/', admin.site.urls), path('api/', include('knox.urls')), path('api/', RootView.as_view(), name="api-root"), -] +] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/knox_project/views.py b/knox_project/views.py index a3cb1ba..5524fe4 100644 --- a/knox_project/views.py +++ b/knox_project/views.py @@ -6,8 +6,11 @@ class RootView(APIView): + """ + API Root View to test authentication. + """ authentication_classes = (TokenAuthentication,) permission_classes = (IsAuthenticated,) def get(self, request): - return Response("api root") + return Response("User is authenticated.") diff --git a/knox_project/wsgi.py b/knox_project/wsgi.py index e607bb4..e1061e2 100644 --- a/knox_project/wsgi.py +++ b/knox_project/wsgi.py @@ -1,10 +1,10 @@ """ -WSGI config for knox_project project. +WSGI config for project project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see -https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/ +https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/ """ import os diff --git a/manage.py b/manage.py index f7d6a8d..85b2257 100755 --- a/manage.py +++ b/manage.py @@ -1,10 +1,22 @@ #!/usr/bin/env python +"""Django's command-line utility for administrative tasks.""" import os import sys -if __name__ == "__main__": + +def main(): + """Run administrative tasks.""" os.environ.setdefault("DJANGO_SETTINGS_MODULE", "knox_project.settings") + try: + from django.core.management import execute_from_command_line + except ImportError as exc: + raise ImportError( + "Couldn't import Django. Are you sure it's installed and " + "available on your PYTHONPATH environment variable? Did you " + "forget to activate a virtual environment?" + ) from exc + execute_from_command_line(sys.argv) - from django.core.management import execute_from_command_line - execute_from_command_line(sys.argv) +if __name__ == "__main__": + main()