Skip to content
Closed
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion backendapi/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
from django.contrib import admin
from .models import Attendant

# Register your models here.

class AttendantAdmin(admin.ModelAdmin):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong repo, see other comment

list_display = ["first_name", "last_name", "email", "phone_number"]


admin.site.register(Attendant, AttendantAdmin)
3 changes: 3 additions & 0 deletions backendapi/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from rest_framework.exceptions import APIException

# write your custm exceptions here if we need it later.
31 changes: 31 additions & 0 deletions backendapi/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 5.1.3 on 2024-11-25 08:50
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong repo, see other comment


from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = []

operations = [
migrations.CreateModel(
name="Attendant",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
("first_name", models.CharField(max_length=30)),
("last_name", models.CharField(max_length=30)),
("email", models.EmailField(max_length=100)),
("phone_number", models.CharField(max_length=12)),
],
),
]
14 changes: 14 additions & 0 deletions backendapi/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
from django.db import models
from rest_framework import serializers
from django.contrib.auth.models import User


# this is the Attendant model, im unsure if we need more data? Maby date of birth? => 18 years maby?
class Attendant(models.Model):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Models should be implemented in the database repo.
We have exactly none of the identifying information listed here for attendants, but we could use some of it for the crew interface.

first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField(max_length=100)
phone_number = models.CharField(max_length=12)

def __str__(self):
return f"{self.first_name} {self.last_name}"


# Create your models here.
88 changes: 88 additions & 0 deletions backendapi/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import re
from rest_framework import serializers
from django.conf import settings
from .models import Attendant

# Serializer for the Attendant model: handles validation, serialization, and desersialization of data.
# Change the model later depending on requirements. Please tell me what, and il remake the model later to fit requirements.


class AttendantAdmin(serializers.ModelSerializer): # defined a serialiser class
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason for why this is named AttendantAdmin?

first_name = serializers.CharField(
label=("First Name* "), # Labels for the field
required=True, # This makes the fields required.
max_length=100,
style={
"input_type": "text",
"autofocus": False,
"autocomplete": "off",
"required": True,
},
error_messages={
"required": "This field is required.",
"blank": "First Name is required.",
},
)

last_name = serializers.CharField(
label=("Last Name* "), # Label for the field
required=True, # This makes the fields required.
max_length=100,
style={
"input_type": "text",
"autofocus": False,
"autocomplete": "off",
"required": True,
},
error_messages={
"required": "This field is required.",
"blank": "Last Name is required.",
"invalid": "Last Name can only contain characters.",
},
)

email = serializers.EmailField(
label=("Email* "), # Label for the field
required=True, # Field is required
max_length=100,
style={
"input_type": "email",
"autofocus": False,
"autocomplete": "off",
"required": True,
},
error_messages={
"required": "This field is required.",
"blank": "Email is required.",
},
)
phone_number = serializers.CharField(
label="Phone Number* ", # Label for the field
max_length=14,
min_length=10,
required=True, # Field is required
error_messages={
"required": "This field is required .",
"blank": "Phone number is required.",
},
)

class Meta:
model = Attendant
fields = ["first_name", "last_name", "email", "phone_number"]


def validate_first_name(value):
# Check if the first name contains only characters or letters with spaces and letters from a-Z
if not re.match(r"^[a-zA-Zå-öÅ-Ö ]*$", value):
raise serializers.ValidationError(
"First Name can only contain letters and spaces."
)


def validate_last_name(value):
# Check if the first name contains only characters
if not re.match(r"^[a-zA-Zå-öÅ-Ö ]*$", value):
raise serializers.ValidationError(
"Last Name can only contain letters and spaces."
)
1 change: 1 addition & 0 deletions backendapi/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

urlpatterns = [
path("nfctag/<hex:tag>", views.scanned),
path("test", views.api_get, name="api_get"),
]
87 changes: 86 additions & 1 deletion backendapi/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
from django.views.decorators.http import require_POST
from django.views.decorators.cache import never_cache
from django.http import JsonResponse

from database.models import Attendant

from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework import status
from backendapi import exceptions
from .serializers import AttendantAdmin


@csrf_exempt
@require_POST
Expand All @@ -18,3 +23,83 @@ def scanned(request, tag: bytes):

except Attendant.DoesNotExist:
return JsonResponse({"error": "No such tag", "valid": False}, status=404)


@api_view(["GET", "POST", "PUT", "PATCH", "DELETE"])
def api_get(request, pk=None):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, to get started on the frontend, it would make sense to just return some simple test data (hardcoded Response data, possibly a few mutations based on the provided input variable)

if request.method == "GET":
# If there's no 'pk' parameter, return all attendants
if pk is None:
attendants = Attendant.objects.all()
serializer = AttendantAdmin(attendants, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
# Otherwise, return a single attendant
try:
attendant = Attendant.objects.get(pk=pk)
serializer = AttendantAdmin(attendant)
return Response(serializer.data, status=status.HTTP_200_OK)
except Attendant.DoesNotExist:
return Response(
{"detail": "Attendant not found."}, status=status.HTTP_404_NOT_FOUND
)

elif request.method == "POST":
# Create a new attendant
serializer = AttendantAdmin(data=request.data)
if serializer.is_valid():
serializer.save() # Save the new attendant to the database
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

elif request.method == "PUT":
# Full update of an existing attendant
try:
attendant = Attendant.objects.get(pk=pk)
except Attendant.DoesNotExist:
return Response(
{"detail": "Attendant not found."}, status=status.HTTP_404_NOT_FOUND
)

serializer = AttendantAdmin(attendant, data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

elif request.method == "PATCH":
# Partial update of an existing attendant
try:
attendant = Attendant.objects.get(pk=pk)
except Attendant.DoesNotExist:
return Response(
{"detail": "Attendant not found."}, status=status.HTTP_404_NOT_FOUND
)

serializer = AttendantAdmin(attendant, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

elif request.method == "DELETE":
# Delete an existing attendant
try:
attendant = Attendant.objects.get(pk=pk)
except Attendant.DoesNotExist:
return Response(
{"detail": "Attendant not found."}, status=status.HTTP_404_NOT_FOUND
)

attendant.delete()
return Response(
{"detail": "Attendant deleted successfully."},
status=status.HTTP_204_NO_CONTENT,
)


"""
def api_get(request):
if request.method == "GET":
return Response({"message": "GET request received"}, status=202)
elif request.method == "POST":
return Response({"message": "POST request received"}, status=405) """
1 change: 1 addition & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
# ---
"database",
"backendapi",
"rest_framework",
]

MIDDLEWARE = [
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ Django[all]==5.1.3
django-urlconfchecks==0.11.0 # TODO: Add to CI
daphne==4.1.2
psycopg2-binary==2.9.10
djangorestframework==3.15.2