Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set user roles/update user fields, no tests/aws cognito #10

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions backend/user/urls.py
Original file line number Diff line number Diff line change
@@ -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'),
]
68 changes: 66 additions & 2 deletions backend/user/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,67 @@
from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework.response import Response
from .models import Admin, Bidder

# Create your views here.

@api_view(['POST'])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leuneri Update to follow format of auction/views.py

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 converted to {role}, fields 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)