Compare commits

..

No commits in common. "0f8815eb432583123ebef1043f892d6bfa801ca8" and "78ba2387ec85bcafaadf40867d84d33ee1f09939" have entirely different histories.

2 changed files with 8 additions and 26 deletions

View file

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

View file

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