From e509c636eafdc023a313a332bed1117867c9e429 Mon Sep 17 00:00:00 2001 From: gustavo2795 Date: Fri, 17 Nov 2017 19:57:10 -0200 Subject: [PATCH 1/2] adding comments in users view --- users/views.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/users/views.py b/users/views.py index d2de8db..7bd8518 100644 --- a/users/views.py +++ b/users/views.py @@ -14,6 +14,7 @@ @api_view(['GET', 'POST', 'PUT', 'DELETE']) def users(request): + #method to GET all users from API if request.method == 'GET': users = Users.objects.all() @@ -24,6 +25,7 @@ def users(request): else: return JsonResponse(seri, safe=False) + #method to POST a new User in API elif request.method == 'POST': if request.data: @@ -47,6 +49,7 @@ def users(request): else: return Response(status=status.HTTP_400_BAD_REQUEST) + #method to update an user from API elif request.method == 'PUT': u = User.objects.get(email=request.data['userEmail']) user_authenticate = authenticate(username=u.username, password=request.data['userPassword']) From ec9054b5ab596fa9db2440f803abaf38aeca892b Mon Sep 17 00:00:00 2001 From: vitorcx Date: Sat, 25 Nov 2017 01:04:05 -0200 Subject: [PATCH 2/2] Creating method to update user objects --- DepuTinder_API/urls.py | 2 ++ users/views.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/DepuTinder_API/urls.py b/DepuTinder_API/urls.py index 6ebddee..1b33024 100644 --- a/DepuTinder_API/urls.py +++ b/DepuTinder_API/urls.py @@ -20,6 +20,7 @@ from questionnaire.views import questionnaire from parlamentarians.views import parlamentarians from users.views import users +from users.views import updateUser from candidateRanking.views import rankingIndex from candidateRanking.views import answeredQuestions from question.views import question @@ -31,6 +32,7 @@ url(r'^ranking/', rankingIndex), url(r'^questionnaire/', questionnaire), url(r'^users/', users), + url(r'^updateUser/', updateUser), url(r'^parlamentarians/', parlamentarians), url(r'^sendAnsweredQuestions/', answeredQuestions), url(r'^question/', question), diff --git a/users/views.py b/users/views.py index 802faa3..edc5de6 100644 --- a/users/views.py +++ b/users/views.py @@ -78,3 +78,26 @@ def users(request): print(request.data) deleteUser = User.objects.get(email=request.data['userEmail']).delete() return Response(status=status.HTTP_200_OK) + +@api_view(['PUT']) +def updateUser(request): + u = User.objects.filter(email=request.data['oldUserEmail']) + + if not u: + return JsonResponse({"status":500, "message": "Email não existe!"}, status=status.HTTP_400_BAD_REQUEST) + else: + user_authenticate = authenticate(username=u[0].username, password=request.data['oldUserPassword']) + if user_authenticate is not None: + user = Users.objects.get(user=user_authenticate.id) + u[0].username = request.data['userName'] + u[0].email = request.data['userEmail'] + print(request.data['userPassword']) + if(request.data['userPassword'] != ''): + u[0].set_password(request.data['userPassword']) + u[0].save() + print(u[0].username) + print(u[0].email) + print(u[0].password) + return JsonResponse({"status":400, "message": "Email ou senha incorretos"}, status=status.HTTP_400_BAD_REQUEST) + else: + return JsonResponse({"status":400, "message": "Email ou senha incorretos"}, status=status.HTTP_400_BAD_REQUEST)