From 1d0dbdf511fca651c8d57d7c82c2c55421be5a69 Mon Sep 17 00:00:00 2001 From: leuneri Date: Tue, 31 Oct 2023 19:10:25 -0800 Subject: [PATCH 1/4] set user roles/update user fields, no tests/aws cognito --- backend/user/urls.py | 7 +++++ backend/user/views.py | 69 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 backend/user/urls.py diff --git a/backend/user/urls.py b/backend/user/urls.py new file mode 100644 index 0000000..69d5445 --- /dev/null +++ b/backend/user/urls.py @@ -0,0 +1,7 @@ +from django.urls import path +from .views import set_user_role, update_user_profile + +urlpatterns = [ + path('user/updateUser/', update_user_profile, name='update_user_profile'), + path('user/setRole/', set_user_role, name='set_user_role'), +] diff --git a/backend/user/views.py b/backend/user/views.py index 91ea44a..75d1cb2 100644 --- a/backend/user/views.py +++ b/backend/user/views.py @@ -1,3 +1,68 @@ -from django.shortcuts import render +from rest_framework.decorators import api_view +from rest_framework.response import Response +from .models import Admin, Bidder + +@api_view(['POST']) +def set_user_role(request): + email = request.data.get("email") + role = request.data.get("role") + + # Check if the role is valid + if role not in ["admin", "bidder"]: + return Response({"error": "Invalid role"}, status=400) + + # Input user fields + if role == "admin": + admin, newAdmin = Admin.objects.get_or_create(email=email) + if newAdmin: + admin.password = request.data.get("password") + admin.first_name = request.data.get("first_name") + admin.last_name = request.data.get("last_name") + admin.permission_level = request.data.get("permission_level") + admin.save() + + elif role == "bidder": + bidder, newBidder = Bidder.objects.get_or_create(email=email) + if newBidder: + bidder.password = request.data.get("password") + bidder.first_name = request.data.get("first_name") + bidder.last_name = request.data.get("last_name") + bidder.company_name = request.data.get("company_name") + bidder.bidder_number = request.data.get("bidder_number") + bidder.is_verified = request.data.get("is_verified") + bidder.is_blacklisted = request.data.get("is_blacklisted") + bidder.save() + + return Response({"message": f"User has been converted to {role} and fields have been populated"}) + +@api_view(['PUT']) +def update_user_profile(request, email, role): + if role == "admin": + try: + admin = Admin.objects.get(email=email) + admin.password = request.data.get("password") + admin.first_name = request.data.get("first_name") + admin.last_name = request.data.get("last_name") + admin.permission_level = request.data.get("permission_level") + admin.save() + return Response({"message": "Admin has been updated"}) + except Admin.DoesNotExist: + return Response({"error": "Admin not found"}, status=400) + + elif role == "bidder": + try: + bidder = Bidder.objects.get(email=email) + bidder.first_name = request.data.get("first_name") + bidder.last_name = request.data.get("last_name") + bidder.company_name = request.data.get("company_name") + bidder.bidder_number = request.data.get("bidder_number") + bidder.is_verified = request.data.get("is_verified") + bidder.is_blacklisted = request.data.get("is_blacklisted") + bidder.save() + return Response({"message": "Bidder has been updated"}) + except Bidder.DoesNotExist: + return Response({"error": "Bidder not found"}, status=400) + + else: + return Response({"error": "Invalid role"}, status=400) -# Create your views here. From dd5289fee91ef6de8ad844bea21b4f75a2c124a4 Mon Sep 17 00:00:00 2001 From: leuneri Date: Tue, 31 Oct 2023 19:16:55 -0800 Subject: [PATCH 2/4] changed for styles check --- backend/user/views.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/backend/user/views.py b/backend/user/views.py index 75d1cb2..ab01862 100644 --- a/backend/user/views.py +++ b/backend/user/views.py @@ -2,6 +2,7 @@ from rest_framework.response import Response from .models import Admin, Bidder + @api_view(['POST']) def set_user_role(request): email = request.data.get("email") @@ -33,7 +34,8 @@ def set_user_role(request): bidder.is_blacklisted = request.data.get("is_blacklisted") bidder.save() - return Response({"message": f"User has been converted to {role} and fields have been populated"}) + return Response({"message": f"User converted to {role}, fields populated"}) + @api_view(['PUT']) def update_user_profile(request, email, role): @@ -48,7 +50,6 @@ def update_user_profile(request, email, role): return Response({"message": "Admin has been updated"}) except Admin.DoesNotExist: return Response({"error": "Admin not found"}, status=400) - elif role == "bidder": try: bidder = Bidder.objects.get(email=email) @@ -61,8 +62,6 @@ def update_user_profile(request, email, role): bidder.save() return Response({"message": "Bidder has been updated"}) except Bidder.DoesNotExist: - return Response({"error": "Bidder not found"}, status=400) - + return Response({"error": "Bidder not found"}, status=400) else: - return Response({"error": "Invalid role"}, status=400) - + return Response({"error": "Invalid role"}, status=400) \ No newline at end of file From 414ec91dbbc93b755ea032c32ce48ad6e755b5b2 Mon Sep 17 00:00:00 2001 From: leuneri Date: Tue, 31 Oct 2023 19:19:10 -0800 Subject: [PATCH 3/4] styling check again --- backend/user/views.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/user/views.py b/backend/user/views.py index ab01862..1bb4642 100644 --- a/backend/user/views.py +++ b/backend/user/views.py @@ -62,6 +62,7 @@ def update_user_profile(request, email, role): bidder.save() return Response({"message": "Bidder has been updated"}) except Bidder.DoesNotExist: - return Response({"error": "Bidder not found"}, status=400) + return Response({"error": "Bidder not found"}, status=400) else: - return Response({"error": "Invalid role"}, status=400) \ No newline at end of file + return Response({"error": "Invalid role"}, status=400) + \ No newline at end of file From 9dc986a4088b8f0692bca11f4a16f6b46f342e71 Mon Sep 17 00:00:00 2001 From: leuneri Date: Tue, 31 Oct 2023 19:21:09 -0800 Subject: [PATCH 4/4] bruh plssss build don't fail on me for styling --- backend/user/views.py | 1 - 1 file changed, 1 deletion(-) diff --git a/backend/user/views.py b/backend/user/views.py index 1bb4642..2881c40 100644 --- a/backend/user/views.py +++ b/backend/user/views.py @@ -65,4 +65,3 @@ def update_user_profile(request, email, role): return Response({"error": "Bidder not found"}, status=400) else: return Response({"error": "Invalid role"}, status=400) - \ No newline at end of file