Commit 398db1f0 authored by Patrick Ade's avatar Patrick Ade
Browse files

added comment

parent 4c761710
......@@ -13,6 +13,8 @@ from django.views.decorators.csrf import csrf_exempt
from django.contrib.auth import authenticate, login, logout
from .forms import CreateUserForm
"""old functions not used by django ninja"""
@ensure_csrf_cookie
@require_http_methods(["GET"])
......@@ -30,18 +32,14 @@ def login_view(request):
email = data["email"]
password = data["password"]
except json.JSONDecodeError:
return JsonResponse(
{"success": False, "message": "Invalid JSON"}, status=400
)
return JsonResponse({"success": False, "message": "Invalid JSON"}, status=400)
user = authenticate(request, username=email, password=password)
if user:
login(request, user) # also creates a session in the browser
return JsonResponse({"success": True})
return JsonResponse(
{"success": False, "message": "Invalid credentials"}, status=401
)
return JsonResponse({"success": False, "message": "Invalid credentials"}, status=401)
def logout_view(request):
......@@ -52,9 +50,7 @@ def logout_view(request):
@require_http_methods(["GET"])
def user(request):
if request.user.is_authenticated:
return JsonResponse(
{"username": request.user.username, "email": request.user.email}
)
return JsonResponse({"username": request.user.username, "email": request.user.email})
return JsonResponse({"message": "Not logged in"}, status=401)
......@@ -64,9 +60,7 @@ def register(request):
form = CreateUserForm(data)
if form.is_valid():
form.save()
return JsonResponse(
{"success": "User registered successfully"}, status=201
)
return JsonResponse({"success": "User registered successfully"}, status=201)
else:
errors = form.errors.as_json()
return JsonResponse({"error": errors}, status=400)
......@@ -80,9 +74,7 @@ def room_data_range(request):
stop = request.GET.get("stop", "now()")
if not room:
return JsonResponse(
{"error": "Missing 'room' parameter"}, status=400
)
return JsonResponse({"error": "Missing 'room' parameter"}, status=400)
load_dotenv()
client = InfluxDBHelper(
......@@ -108,9 +100,7 @@ def room_data_range(request):
return JsonResponse({"room": room, "data": data_by_time}, status=200)
except json.JSONDecodeError:
return JsonResponse(
{"success": False, "message": "Invalid JSON"}, status=400
)
return JsonResponse({"success": False, "message": "Invalid JSON"}, status=400)
@require_http_methods(["GET"])
......@@ -130,6 +120,7 @@ def get_rooms(request):
return JsonResponse({"rooms": sorted(rooms)})
@require_http_methods(["GET"])
def get_rooms_from_building(request):
building = request.GET.get("building")
......@@ -155,6 +146,7 @@ def get_rooms_from_building(request):
return JsonResponse({"building": building, "rooms": sorted(rooms)})
@require_http_methods(["GET"])
def get_bau(request):
client = InfluxDBHelper(
......@@ -176,6 +168,7 @@ def get_bau(request):
return JsonResponse({"baus": sorted(baus)})
@require_http_methods(["GET"])
def room_data_csv_view(request):
room = request.GET.get("room")
......@@ -234,6 +227,7 @@ def room_data_csv_download(request):
response["Content-Disposition"] = f'attachment; filename="{room}_data.csv"'
return response
@require_http_methods(["GET"])
def room_availability(request):
room = request.GET.get("room")
......@@ -308,4 +302,3 @@ def all_sensors_availability(request):
sensors_available = client.sensors_with_available_data(start, stop)
return JsonResponse({"available_sensors": sensors_available}, status=200)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment