-
Notifications
You must be signed in to change notification settings - Fork 1
Basic api draft #18
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
Basic api draft #18
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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): | ||
| list_display = ["first_name", "last_name", "email", "phone_number"] | ||
|
|
||
|
|
||
| admin.site.register(Attendant, AttendantAdmin) | ||
| 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. |
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)), | ||
| ], | ||
| ), | ||
| ] | ||
| 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): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Models should be implemented in the database repo. |
||
| 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. | ||
| 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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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." | ||
| ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,4 +6,5 @@ | |
|
|
||
| urlpatterns = [ | ||
| path("nfctag/<hex:tag>", views.scanned), | ||
| path("test", views.api_get, name="api_get"), | ||
| ] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) """ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,7 @@ | |
| # --- | ||
| "database", | ||
| "backendapi", | ||
| "rest_framework", | ||
| ] | ||
|
|
||
| MIDDLEWARE = [ | ||
|
|
||
There was a problem hiding this comment.
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