Skip to content

Commit 574dcde

Browse files
authored
Merge pull request #7 from EventAccess/pr-nfctag-scanned-1
Add API endpoint to check ticket validity
2 parents a4a3e5d + 85d1b24 commit 574dcde

7 files changed

Lines changed: 47 additions & 2 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.py[ocd]
22
**/.env
3+
*.sqlite3

backendapi/converters.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from base64 import b16decode, b16encode
2+
3+
4+
class BinaryHexConverter:
5+
regex = "(?:[0-9a-fA-F]{2})+"
6+
7+
def to_python(self, value: str) -> bytes:
8+
return b16decode(value, casefold=True)
9+
10+
def to_url(self, value: bytes) -> str:
11+
return b16encode(value).decode()

backendapi/urls.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.urls import path, register_converter
2+
3+
from . import views, converters
4+
5+
register_converter(converters.BinaryHexConverter, "hex")
6+
7+
urlpatterns = [
8+
path("nfctag/<hex:tag>", views.scanned),
9+
]

backendapi/views.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
from django.shortcuts import render
2+
from django.views.decorators.http import require_POST
3+
from django.views.decorators.cache import never_cache
4+
from django.http import JsonResponse
25

3-
# Create your views here.
6+
from database.models import Attendant
7+
8+
9+
@require_POST
10+
@never_cache
11+
def scanned(request, tag: bytes):
12+
try:
13+
attendant = Attendant.objects.get(tag=tag)
14+
# TODO: Queue event
15+
return JsonResponse({"valid": attendant.is_valid}, status=202)
16+
17+
except Attendant.DoesNotExist:
18+
return JsonResponse({"error": "No such tag"}, status=404)

config/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
"django.contrib.sessions",
3838
"django.contrib.messages",
3939
"django.contrib.staticfiles",
40+
# ---
41+
"database",
42+
"backendapi",
4043
]
4144

4245
MIDDLEWARE = [

config/urls.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
"""
1717

1818
from django.contrib import admin
19-
from django.urls import path
19+
from django.urls import include, path
20+
21+
import backendapi.urls
2022

2123
urlpatterns = [
2224
path("admin/", admin.site.urls),
25+
path("api/", include(backendapi.urls)),
2326
]

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[pytest]
2+
DJANGO_SETTINGS_MODULE = config.settings
3+
python_files = tests.py test_*.py *_tests.py

0 commit comments

Comments
 (0)