Skip to content

Commit

Permalink
Merge pull request #7 from EventAccess/pr-nfctag-scanned-1
Browse files Browse the repository at this point in the history
Add API endpoint to check ticket validity
  • Loading branch information
oddstr13 authored Oct 2, 2024
2 parents a4a3e5d + 85d1b24 commit 574dcde
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.py[ocd]
**/.env
*.sqlite3
11 changes: 11 additions & 0 deletions backendapi/converters.py
Original file line number Diff line number Diff line change
@@ -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()
9 changes: 9 additions & 0 deletions backendapi/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path, register_converter

from . import views, converters

register_converter(converters.BinaryHexConverter, "hex")

urlpatterns = [
path("nfctag/<hex:tag>", views.scanned),
]
17 changes: 16 additions & 1 deletion backendapi/views.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
# ---
"database",
"backendapi",
]

MIDDLEWARE = [
Expand Down
5 changes: 4 additions & 1 deletion config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
]
3 changes: 3 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[pytest]
DJANGO_SETTINGS_MODULE = config.settings
python_files = tests.py test_*.py *_tests.py

0 comments on commit 574dcde

Please sign in to comment.