From 630cc11c7d0362631b07e7a0b83ff3532a51f3bd Mon Sep 17 00:00:00 2001 From: Joe Heffer Date: Mon, 13 Jan 2025 11:31:23 +0000 Subject: [PATCH 1/2] Install DJANGO REST framework https://www.django-rest-framework.org/#installation --- SORT/settings.py | 19 +++++++++++-------- SORT/urls.py | 1 + requirements.txt | 3 ++- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/SORT/settings.py b/SORT/settings.py index 2eef0c0d..d07aebc4 100644 --- a/SORT/settings.py +++ b/SORT/settings.py @@ -20,7 +20,6 @@ # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent - # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/ @@ -32,7 +31,6 @@ ALLOWED_HOSTS = [] - # Application definition INSTALLED_APPS = [ @@ -49,6 +47,7 @@ "home", "survey", "invites", + "rest_framework", ] MIDDLEWARE = [ @@ -82,7 +81,6 @@ WSGI_APPLICATION = "SORT.wsgi.application" - # Database # https://docs.djangoproject.com/en/5.1/ref/settings/#databases @@ -116,7 +114,6 @@ }, ] - # Internationalization # https://docs.djangoproject.com/en/5.1/topics/i18n/ @@ -128,7 +125,6 @@ USE_TZ = True - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/5.1/howto/static-files/ @@ -136,7 +132,6 @@ STATICFILES_DIRS = [BASE_DIR / "static"] - # Default primary key field type # https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field @@ -145,7 +140,6 @@ LOGIN_REDIRECT_URL = "/" LOGOUT_REDIRECT_URL = "/" - # FA: End session when the browser is closed SESSION_EXPIRE_AT_BROWSER_CLOSE = True @@ -166,8 +160,17 @@ "127.0.0.1", # ... ] -AUTH_USER_MODEL = 'home.User' # FA: replace username with email as unique identifiers +AUTH_USER_MODEL = 'home.User' # FA: replace username with email as unique identifiers # FA: for production: # EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend" + +# Django REST framework +# https://www.django-rest-framework.org +REST_FRAMEWORK = dict( + # Use Django's standard `django.contrib.auth` permissions, or allow read-only access for unauthenticated users. + DEFAULT_PERMISSION_CLASSES=[ + 'rest_framework.permissions.DjangoModelPermissionsOrAnonReadOnly' + ] +) diff --git a/SORT/urls.py b/SORT/urls.py index 2e450d41..290cd46a 100644 --- a/SORT/urls.py +++ b/SORT/urls.py @@ -22,6 +22,7 @@ urlpatterns = [ path("admin/", admin.site.urls), path("", include("home.urls")), + path("api-auth/", include("rest_framework.urls")), ] if settings.DEBUG: diff --git a/requirements.txt b/requirements.txt index 47d408e4..14dd0291 100644 --- a/requirements.txt +++ b/requirements.txt @@ -24,4 +24,5 @@ traitlets==5.14.3 typing_extensions==4.12.2 tzdata==2024.1 wcwidth==0.2.13 -django_debug_toolbar==4.4.6 \ No newline at end of file +django_debug_toolbar==4.4.6 +djangorestframework==3.* From 20a6c74b235a8ba41aa66aefd71dd9b921192fd5 Mon Sep 17 00:00:00 2001 From: Joe Heffer Date: Mon, 13 Jan 2025 11:59:28 +0000 Subject: [PATCH 2/2] Add basic API for survey data --- SORT/settings.py | 1 + SORT/urls.py | 2 +- api/README.md | 3 +++ api/__init__.py | 0 api/serializers.py | 12 ++++++++++++ api/urls.py | 12 ++++++++++++ api/views.py | 15 +++++++++++++++ 7 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 api/README.md create mode 100644 api/__init__.py create mode 100644 api/serializers.py create mode 100644 api/urls.py create mode 100644 api/views.py diff --git a/SORT/settings.py b/SORT/settings.py index d07aebc4..e3bdd94d 100644 --- a/SORT/settings.py +++ b/SORT/settings.py @@ -48,6 +48,7 @@ "survey", "invites", "rest_framework", + "api", ] MIDDLEWARE = [ diff --git a/SORT/urls.py b/SORT/urls.py index 290cd46a..7e0f554a 100644 --- a/SORT/urls.py +++ b/SORT/urls.py @@ -22,7 +22,7 @@ urlpatterns = [ path("admin/", admin.site.urls), path("", include("home.urls")), - path("api-auth/", include("rest_framework.urls")), + path("api/", include("api.urls")), ] if settings.DEBUG: diff --git a/api/README.md b/api/README.md new file mode 100644 index 00000000..e707e4e1 --- /dev/null +++ b/api/README.md @@ -0,0 +1,3 @@ +# REST API + +This is a REST API implemented using [Django REST framework](https://www.django-rest-framework.org/). diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/api/serializers.py b/api/serializers.py new file mode 100644 index 00000000..3a35dde1 --- /dev/null +++ b/api/serializers.py @@ -0,0 +1,12 @@ +# Django REST framework serializers +# These are used to convert our models into data, such as JSON format. + +import rest_framework.serializers + +import survey.models + + +class AnswerSerializer(rest_framework.serializers.HyperlinkedModelSerializer): + class Meta: + model = survey.models.Answer + fields = '__all__' diff --git a/api/urls.py b/api/urls.py new file mode 100644 index 00000000..64129fcc --- /dev/null +++ b/api/urls.py @@ -0,0 +1,12 @@ +import rest_framework.routers +from django.urls import include, path + +import api.views + +router = rest_framework.routers.DefaultRouter() +router.register(r"answers", api.views.AnswerViewSet) + +urlpatterns = [ + path("", include(router.urls)), + path("api-auth/", include("rest_framework.urls")), +] diff --git a/api/views.py b/api/views.py new file mode 100644 index 00000000..b687dbf8 --- /dev/null +++ b/api/views.py @@ -0,0 +1,15 @@ +import rest_framework.permissions +import rest_framework.viewsets + +import api.serializers +import survey.models + + +class AnswerViewSet(rest_framework.viewsets.ModelViewSet): + """ + API endpoints for answers. + """ + + queryset = survey.models.Answer.objects.all() + serializer_class = api.serializers.AnswerSerializer + permission_classes = [rest_framework.permissions.IsAuthenticated]