Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add REST API #43

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions SORT/settings.py
Original file line number Diff line number Diff line change
@@ -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,15 +126,13 @@

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.1/howto/static-files/

STATIC_URL = "static/"

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'
]
)
1 change: 1 addition & 0 deletions SORT/urls.py
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
urlpatterns = [
path("admin/", admin.site.urls),
path("", include("home.urls")),
path("api/", include("api.urls")),
]

if settings.DEBUG:
3 changes: 3 additions & 0 deletions api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# REST API

This is a REST API implemented using [Django REST framework](https://www.django-rest-framework.org/).
Empty file added api/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions api/serializers.py
Original file line number Diff line number Diff line change
@@ -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__'
12 changes: 12 additions & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
@@ -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")),
]
15 changes: 15 additions & 0 deletions api/views.py
Original file line number Diff line number Diff line change
@@ -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]
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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
django_debug_toolbar==4.4.6
djangorestframework==3.*