Skip to content

Commit

Permalink
chore: Modernize example app
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannicimolin committed Aug 13, 2024
1 parent 1e379b4 commit 286434c
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 50 deletions.
16 changes: 16 additions & 0 deletions knox_project/asgi.py
Original file line number Diff line number Diff line change
@@ -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()
101 changes: 63 additions & 38 deletions knox_project/settings.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,81 @@
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": [
"knox.auth.TokenAuthentication",
]
}
10 changes: 7 additions & 3 deletions knox_project/urls.py
Original file line number Diff line number Diff line change
@@ -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('api/', include('knox.urls')),
path('api/', RootView.as_view(), name="api-root"),
]
path('admin/', admin.site.urls),
path('', include('knox.urls')),
path('', RootView.as_view(), name="api-root"),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
8 changes: 4 additions & 4 deletions knox_project/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
from rest_framework.response import Response
from rest_framework.views import APIView

from knox.auth import TokenAuthentication


class RootView(APIView):
authentication_classes = (TokenAuthentication,)
"""
API Root View to test authentication.
"""
permission_classes = (IsAuthenticated,)

def get(self, request):
return Response("api root")
return Response("User is authenticated.")
4 changes: 2 additions & 2 deletions knox_project/wsgi.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 15 additions & 3 deletions manage.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 286434c

Please sign in to comment.