diff --git a/SORT/settings.py b/SORT/settings.py index 2eef0c0d..e3bdd94d 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,8 @@ "home", "survey", "invites", + "rest_framework", + "api", ] MIDDLEWARE = [ @@ -82,7 +82,6 @@ WSGI_APPLICATION = "SORT.wsgi.application" - # Database # https://docs.djangoproject.com/en/5.1/ref/settings/#databases @@ -116,7 +115,6 @@ }, ] - # Internationalization # https://docs.djangoproject.com/en/5.1/topics/i18n/ @@ -128,7 +126,6 @@ USE_TZ = True - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/5.1/howto/static-files/ @@ -136,7 +133,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 +141,6 @@ LOGIN_REDIRECT_URL = "/" LOGOUT_REDIRECT_URL = "/" - # FA: End session when the browser is closed SESSION_EXPIRE_AT_BROWSER_CLOSE = True @@ -166,8 +161,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..7e0f554a 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/", 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] 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.*