Skip to content

Commit

Permalink
Implement user data update feature (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
veldic authored Nov 27, 2021
1 parent f2ee861 commit e43d338
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 6 deletions.
68 changes: 64 additions & 4 deletions backend/connectoon/user/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ def test_user_register(self):

# Check with image
# response = client.post('/users/',
# {'email': 'test4@snu.ac.kr', 'username': 'test4', 'password': 'qwe123',
# 'tags': [], 'profile_picture': self.generate_photo_file()},
# content_type='multipart/formdata', HTTP_X_CSRFTOKEN=csrftoken)
# urlencode({'email': 'test4@snu.ac.kr', 'username': 'test4', 'password': 'qwe123',
# 'tags': [], 'profile_picture': self.generate_photo_file()}, True),
# content_type='application/x-www-form-urlencoded', HTTP_X_CSRFTOKEN=csrftoken)
# self.assertEqual(response.status_code, 201)
# self.assertEqual(User.objects.count(), 4)
# self.assertEqual(UserTagFav.objects.count(), 3)
Expand Down Expand Up @@ -208,7 +208,7 @@ def test_user_id(self):

self.assertEqual(response.status_code, 501)

def test_user_me(self):
def test_user_me_get(self):
client = Client(enforce_csrf_checks=True)

# Check Not Logged in
Expand All @@ -233,6 +233,66 @@ def test_user_me(self):
response = client.post('/users/me/', HTTP_X_CSRFTOKEN=csrftoken)
self.assertEqual(response.status_code, 405)

def test_user_me_put(self):
client = Client()

# Check Not Logged in
response = client.put('/users/me/')
self.assertEqual(response.status_code, 401)

# Login
response = client.post('/users/login/',
json.dumps({'email': 'test1@snu.ac.kr', 'password': 'qwe123'}),
content_type='application/json')
self.assertEqual(response.status_code, 200)

# Check only username change
response = client.put('/users/me/',
urlencode({'username': 'test2', 'tags': ['tag1']}, True),
content_type='application/x-www-form-urlencoded')
self.assertEqual(response.status_code, 200)

# Check only tag change
response = client.put('/users/me/',
urlencode({'tags': ['tag1', 'tag2']}, True),
content_type='application/x-www-form-urlencoded')
self.assertEqual(response.status_code, 200)
self.assertEqual(UserTagFav.objects.count(), 2)

response = client.put('/users/me/',
urlencode({'tags': []}, True),
content_type='application/x-www-form-urlencoded')
self.assertEqual(response.status_code, 200)
self.assertEqual(UserTagFav.objects.count(), 0)

# Check tag not appropriate
response = client.put('/users/me/',
urlencode({'tags': ['tag1', 'tag5']}, True),
content_type='application/x-www-form-urlencoded')
self.assertEqual(response.status_code, 400)

# Check change password
response = client.put('/users/me/',
urlencode({'password': 'qwe1234', 'tags': ['tag1', 'tag2']}, True),
content_type='application/x-www-form-urlencoded')
self.assertEqual(response.status_code, 200)

# Fail Login
response = client.post('/users/login/',
json.dumps({'email': 'test1@snu.ac.kr', 'password': 'qwe123'}),
content_type='application/json')
self.assertEqual(response.status_code, 401)

# Success Login
response = client.post('/users/login/',
json.dumps({'email': 'test1@snu.ac.kr', 'password': 'qwe1234'}),
content_type='application/json')
self.assertEqual(response.status_code, 200)

# Check Not Allowed
response = client.post('/users/me/')
self.assertEqual(response.status_code, 405)

def test_user_me_review(self):
client = Client()
response = client.get('/users/me/reviews/')
Expand Down
74 changes: 72 additions & 2 deletions backend/connectoon/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from django.utils.datastructures import MultiValueDictKeyError
from django.views.decorators.csrf import ensure_csrf_cookie
from django.contrib.auth import get_user_model, authenticate, login as auth_login, logout as auth_logout
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotAllowed, HttpResponseForbidden, JsonResponse
from django.http import HttpResponse, HttpResponseBadRequest, HttpResponseNotAllowed, HttpResponseForbidden, \
JsonResponse, QueryDict

import json
from json.decoder import JSONDecodeError
Expand Down Expand Up @@ -161,11 +162,80 @@ def user_me(request):
'tags': tag_list,
'profile_picture': request.build_absolute_uri(request_user.profile_picture.url) if request_user.profile_picture else ''
}
return JsonResponse(response_dict, status=200)
else:
return HttpResponse(status=401)
if request.method == 'PUT':
if request_user.is_authenticated:

request.method = 'POST'
request._load_post_and_files()
request.method = 'PUT'
request.PUT = request.POST

try:
username = request.POST['username']
except MultiValueDictKeyError:
username = None

try:
password = request.POST['password']
except MultiValueDictKeyError:
password = None

tags = request.POST.getlist('tags', [])
profile_picture = request.FILES.get('profile_picture')

tag_new = []
for tag_name in tags:
try:
tag_obj = Tag.objects.get(name=tag_name)
tag_new.append(tag_obj)
except Tag.DoesNotExist as e:
return HttpResponseBadRequest()

user_tag_old = request_user.user_tag.all()

# delete UserTagFav if not in request
for user_tag in user_tag_old:
if not(user_tag.tag.name in tags):
user_tag.delete()

# create new UserTagFav if not exist
user_tag_old_name_list = [ut.tag.name for ut in user_tag_old]
for tag in tag_new:
if not(tag.name in user_tag_old_name_list):
UserTagFav.objects.create(user=request_user, tag=tag)

# change data
if username:
request_user.username = username

if password:
request_user.set_password(password)

if profile_picture:
request_user.profile_picture = profile_picture

request_user.save()

updated_user_tag = request_user.user_tag.all()
tag_list = [{'id': user_tag.tag.id, 'name': user_tag.tag.name} for user_tag in updated_user_tag]

response_dict = {
'id': request_user.id,
'email': request_user.email,
'username': request_user.username,
'tags': tag_list,
'profile_picture': request.build_absolute_uri(
request_user.profile_picture.url) if request_user.profile_picture else ''
}

return JsonResponse(response_dict, status=200)
else:
return HttpResponse(status=401)
else:
return HttpResponseNotAllowed(['GET'])
return HttpResponseNotAllowed(['GET', 'PUT'])



Expand Down

0 comments on commit e43d338

Please sign in to comment.