Compare commits
4 Commits
78ba2387ec
...
0f8815eb43
Author | SHA1 | Date |
---|---|---|
Cayo Puigdefabregas | 0f8815eb43 | |
Cayo Puigdefabregas | ba94a01062 | |
Cayo Puigdefabregas | ff92e28b97 | |
Cayo Puigdefabregas | 9c78ff6c1b |
10
api/urls.py
10
api/urls.py
|
@ -6,9 +6,9 @@ from django.urls import path
|
||||||
app_name = 'api'
|
app_name = 'api'
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('snapshot/', views.NewSnapshot, name='new_snapshot'),
|
path('v1/snapshot/', views.NewSnapshot, name='new_snapshot'),
|
||||||
path('tokens/', views.TokenView.as_view(), name='tokens'),
|
path('v1/tokens/', views.TokenView.as_view(), name='tokens'),
|
||||||
path('tokens/new', views.TokenNewView.as_view(), name='new_token'),
|
path('v1/tokens/new', views.TokenNewView.as_view(), name='new_token'),
|
||||||
path("tokens/<int:pk>/edit", views.EditTokenView.as_view(), name="edit_token"),
|
path("v1/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/tokens/<int:pk>/del', views.TokenDeleteView.as_view(), name='delete_token'),
|
||||||
]
|
]
|
||||||
|
|
24
api/views.py
24
api/views.py
|
@ -1,4 +1,6 @@
|
||||||
import json
|
import json
|
||||||
|
import uuid
|
||||||
|
import logging
|
||||||
|
|
||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
|
|
||||||
|
@ -22,6 +24,9 @@ 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
|
||||||
|
@ -31,18 +36,28 @@ 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)
|
return JsonResponse({'error': 'Invalid or missing token'}, status=401)
|
||||||
|
|
||||||
token = auth_header.split(' ')[1]
|
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)
|
||||||
|
|
||||||
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:
|
||||||
return JsonResponse({'error': 'Invalid JSON'}, status=400)
|
logger.exception("Invalid Snapshot of user {}".format(tk.owner))
|
||||||
|
return JsonResponse({'error': 'Invalid JSON'}, status=500)
|
||||||
|
|
||||||
# try:
|
# try:
|
||||||
# Build(data, None, check=True)
|
# Build(data, None, check=True)
|
||||||
|
@ -55,6 +70,7 @@ 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
|
||||||
|
@ -63,6 +79,7 @@ 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(
|
||||||
|
@ -75,6 +92,7 @@ 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,))
|
||||||
|
|
Loading…
Reference in New Issue