Compare commits

...

4 Commits

2 changed files with 26 additions and 8 deletions

View File

@ -6,9 +6,9 @@ from django.urls import path
app_name = 'api'
urlpatterns = [
path('snapshot/', views.NewSnapshot, name='new_snapshot'),
path('tokens/', views.TokenView.as_view(), name='tokens'),
path('tokens/new', views.TokenNewView.as_view(), name='new_token'),
path("tokens/<int:pk>/edit", views.EditTokenView.as_view(), name="edit_token"),
path('tokens/<int:pk>/del', views.TokenDeleteView.as_view(), name='delete_token'),
path('v1/snapshot/', views.NewSnapshot, name='new_snapshot'),
path('v1/tokens/', views.TokenView.as_view(), name='tokens'),
path('v1/tokens/new', views.TokenNewView.as_view(), name='new_token'),
path("v1/tokens/<int:pk>/edit", views.EditTokenView.as_view(), name="edit_token"),
path('v1/tokens/<int:pk>/del', views.TokenDeleteView.as_view(), name='delete_token'),
]

View File

@ -1,4 +1,6 @@
import json
import uuid
import logging
from uuid import uuid4
@ -22,6 +24,9 @@ from api.models import Token
from api.tables import TokensTable
logger = logging.getLogger('django')
@csrf_exempt
def NewSnapshot(request):
# Accept only posts
@ -31,18 +36,28 @@ def NewSnapshot(request):
# Authentication
auth_header = request.headers.get('Authorization')
if not auth_header or not auth_header.startswith('Bearer '):
logger.exception("Invalid or missing token {}".format(auth_header))
return JsonResponse({'error': 'Invalid or missing token'}, status=401)
token = auth_header.split(' ')[1].strip("'").strip('"')
try:
uuid.UUID(token)
except Exception:
logger.exception("Invalid token {}".format(token))
return JsonResponse({'error': 'Invalid or missing token'}, status=401)
token = auth_header.split(' ')[1]
tk = Token.objects.filter(token=token).first()
if not tk:
logger.exception("Invalid or missing token {}".format(token))
return JsonResponse({'error': 'Invalid or missing token'}, status=401)
# Validation snapshot
try:
data = json.loads(request.body)
except json.JSONDecodeError:
return JsonResponse({'error': 'Invalid JSON'}, status=400)
logger.exception("Invalid Snapshot of user {}".format(tk.owner))
return JsonResponse({'error': 'Invalid JSON'}, status=500)
# try:
# Build(data, None, check=True)
@ -55,6 +70,7 @@ def NewSnapshot(request):
if exist_annotation:
txt = "error: the snapshot {} exist".format(data['uuid'])
logger.exception(txt)
return JsonResponse({'status': txt}, status=500)
# Process snapshot
@ -63,6 +79,7 @@ def NewSnapshot(request):
try:
Build(data, tk.owner)
except Exception as err:
logger.exception(err)
return JsonResponse({'status': f"fail: {err}"}, status=500)
annotation = Annotation.objects.filter(
@ -75,6 +92,7 @@ def NewSnapshot(request):
if not annotation:
logger.exception("Error: No annotation for uuid: {}".format(data["uuid"]))
return JsonResponse({'status': 'fail'}, status=500)
url_args = reverse_lazy("device:details", args=(annotation.value,))