Skip to content

Commit 900fadb

Browse files
chore: Modernize example app
1 parent 1e379b4 commit 900fadb

File tree

6 files changed

+107
-45
lines changed

6 files changed

+107
-45
lines changed

knox_project/asgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
ASGI config for project project.
3+
4+
It exposes the ASGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.asgi import get_asgi_application
13+
14+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "knox_project.settings")
15+
16+
application = get_asgi_application()

knox_project/settings.py

+65-38
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,83 @@
1-
import os
1+
"""
2+
Test project for Django REST Knox
3+
"""
24

3-
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
4-
SECRET_KEY = 'gcr$j^h2@d@sd(f#-ihtv6*hg7qno$otw62^*rzcf0tk2wz&sb'
5+
from pathlib import Path
6+
7+
BASE_DIR = Path(__file__).resolve().parent.parent
8+
SECRET_KEY = "i-am-a-super-secret-key"
59
DEBUG = True
6-
ALLOWED_HOSTS = []
7-
INSTALLED_APPS = (
8-
'django.contrib.auth',
9-
'django.contrib.contenttypes',
10-
'django.contrib.sessions',
11-
'rest_framework',
12-
'knox',
13-
)
14-
15-
MIDDLEWARE_CLASSES = (
16-
'django.contrib.sessions.middleware.SessionMiddleware',
17-
'django.middleware.common.CommonMiddleware',
18-
'django.middleware.csrf.CsrfViewMiddleware',
19-
'django.contrib.auth.middleware.AuthenticationMiddleware',
20-
'django.middleware.security.SecurityMiddleware',
21-
)
22-
23-
ROOT_URLCONF = 'knox_project.urls'
10+
ALLOWED_HOSTS = ["*"]
11+
12+
# Application definition
13+
INSTALLED_APPS = [
14+
"django.contrib.admin",
15+
"django.contrib.auth",
16+
"django.contrib.contenttypes",
17+
"django.contrib.sessions",
18+
"django.contrib.messages",
19+
"django.contrib.staticfiles",
20+
"rest_framework",
21+
"knox",
22+
]
23+
24+
MIDDLEWARE = [
25+
"django.middleware.security.SecurityMiddleware",
26+
"django.contrib.sessions.middleware.SessionMiddleware",
27+
"django.middleware.common.CommonMiddleware",
28+
"django.middleware.csrf.CsrfViewMiddleware",
29+
"django.contrib.auth.middleware.AuthenticationMiddleware",
30+
"django.contrib.messages.middleware.MessageMiddleware",
31+
"django.middleware.clickjacking.XFrameOptionsMiddleware",
32+
]
33+
34+
ROOT_URLCONF = "knox_project.urls"
2435

2536
TEMPLATES = [
2637
{
27-
'BACKEND': 'django.template.backends.django.DjangoTemplates',
28-
'DIRS': [],
29-
'APP_DIRS': True,
30-
'OPTIONS': {
31-
'context_processors': [
32-
'django.template.context_processors.debug',
33-
'django.template.context_processors.request',
34-
'django.contrib.auth.context_processors.auth',
38+
"BACKEND": "django.template.backends.django.DjangoTemplates",
39+
"DIRS": [],
40+
"APP_DIRS": True,
41+
"OPTIONS": {
42+
"context_processors": [
43+
"django.template.context_processors.debug",
44+
"django.template.context_processors.request",
45+
"django.contrib.auth.context_processors.auth",
46+
"django.contrib.messages.context_processors.messages",
3547
],
3648
},
3749
},
3850
]
3951

40-
WSGI_APPLICATION = 'knox_project.wsgi.application'
41-
4252
DATABASES = {
43-
'default': {
44-
'ENGINE': 'django.db.backends.sqlite3',
45-
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
53+
"default": {
54+
"ENGINE": "django.db.backends.sqlite3",
55+
"NAME": "db.sqlite3",
4656
}
4757
}
4858

49-
LANGUAGE_CODE = 'en-us'
50-
TIME_ZONE = 'UTC'
59+
WSGI_APPLICATION = "knox_project.wsgi.application"
60+
61+
LANGUAGE_CODE = "en-us"
62+
TIME_ZONE = "UTC"
5163
USE_I18N = True
5264
USE_TZ = True
5365

54-
STATIC_URL = '/static/'
66+
# Static files (CSS, JavaScript, Images)
67+
# https://docs.djangoproject.com/en/5.0/howto/static-files/
68+
69+
STATIC_URL = "static/"
5570

56-
KNOX_TOKEN_MODEL = 'knox.AuthToken'
71+
# Default primary key field type
72+
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field
73+
74+
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
75+
76+
# Django REST Knox settings
77+
REST_FRAMEWORK = {
78+
"DEFAULT_AUTHENTICATION_CLASSES": [
79+
"rest_framework.authentication.BasicAuthentication",
80+
"rest_framework.authentication.SessionAuthentication",
81+
"knox.auth.TokenAuthentication",
82+
]
83+
}

knox_project/urls.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
from django.conf import settings
2+
from django.conf.urls.static import static
3+
from django.contrib import admin
14
from django.urls import include, path
25

36
from .views import RootView
47

58
urlpatterns = [
9+
path('admin/', admin.site.urls),
610
path('api/', include('knox.urls')),
711
path('api/', RootView.as_view(), name="api-root"),
8-
]
12+
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

knox_project/views.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77

88
class RootView(APIView):
9+
"""
10+
API Root View to test authentication.
11+
"""
912
authentication_classes = (TokenAuthentication,)
1013
permission_classes = (IsAuthenticated,)
1114

1215
def get(self, request):
13-
return Response("api root")
16+
return Response("User is authenticated.")

knox_project/wsgi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
"""
2-
WSGI config for knox_project project.
2+
WSGI config for project project.
33
44
It exposes the WSGI callable as a module-level variable named ``application``.
55
66
For more information on this file, see
7-
https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/
7+
https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/
88
"""
99

1010
import os

manage.py

+15-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
23
import os
34
import sys
45

5-
if __name__ == "__main__":
6+
7+
def main():
8+
"""Run administrative tasks."""
69
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "knox_project.settings")
10+
try:
11+
from django.core.management import execute_from_command_line
12+
except ImportError as exc:
13+
raise ImportError(
14+
"Couldn't import Django. Are you sure it's installed and "
15+
"available on your PYTHONPATH environment variable? Did you "
16+
"forget to activate a virtual environment?"
17+
) from exc
18+
execute_from_command_line(sys.argv)
719

8-
from django.core.management import execute_from_command_line
920

10-
execute_from_command_line(sys.argv)
21+
if __name__ == "__main__":
22+
main()

0 commit comments

Comments
 (0)