diff --git a/.gitignore b/.gitignore index 56a5549..dc80f26 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.py[ocd] **/.env +*.sqlite3 diff --git a/backendapi/converters.py b/backendapi/converters.py new file mode 100644 index 0000000..0876959 --- /dev/null +++ b/backendapi/converters.py @@ -0,0 +1,11 @@ +from base64 import b16decode, b16encode + + +class BinaryHexConverter: + regex = "(?:[0-9a-fA-F]{2})+" + + def to_python(self, value: str) -> bytes: + return b16decode(value, casefold=True) + + def to_url(self, value: bytes) -> str: + return b16encode(value).decode() diff --git a/backendapi/urls.py b/backendapi/urls.py new file mode 100644 index 0000000..692238f --- /dev/null +++ b/backendapi/urls.py @@ -0,0 +1,9 @@ +from django.urls import path, register_converter + +from . import views, converters + +register_converter(converters.BinaryHexConverter, "hex") + +urlpatterns = [ + path("nfctag/", views.scanned), +] diff --git a/backendapi/views.py b/backendapi/views.py index 91ea44a..0495c06 100644 --- a/backendapi/views.py +++ b/backendapi/views.py @@ -1,3 +1,18 @@ from django.shortcuts import render +from django.views.decorators.http import require_POST +from django.views.decorators.cache import never_cache +from django.http import JsonResponse -# Create your views here. +from database.models import Attendant + + +@require_POST +@never_cache +def scanned(request, tag: bytes): + try: + attendant = Attendant.objects.get(tag=tag) + # TODO: Queue event + return JsonResponse({"valid": attendant.is_valid}, status=202) + + except Attendant.DoesNotExist: + return JsonResponse({"error": "No such tag"}, status=404) diff --git a/config/settings.py b/config/settings.py index 3b16218..2839ffc 100644 --- a/config/settings.py +++ b/config/settings.py @@ -37,6 +37,9 @@ "django.contrib.sessions", "django.contrib.messages", "django.contrib.staticfiles", + # --- + "database", + "backendapi", ] MIDDLEWARE = [ diff --git a/config/urls.py b/config/urls.py index 379140a..282345a 100644 --- a/config/urls.py +++ b/config/urls.py @@ -16,8 +16,11 @@ """ from django.contrib import admin -from django.urls import path +from django.urls import include, path + +import backendapi.urls urlpatterns = [ path("admin/", admin.site.urls), + path("api/", include(backendapi.urls)), ] diff --git a/pytest.ini b/pytest.ini new file mode 100644 index 0000000..7a4fb9b --- /dev/null +++ b/pytest.ini @@ -0,0 +1,3 @@ +[pytest] +DJANGO_SETTINGS_MODULE = config.settings +python_files = tests.py test_*.py *_tests.py