Skip to content

Commit

Permalink
[Fix #34] Updating user returning a false positive DB update
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaOndieki committed Jul 6, 2018
1 parent 6c2f2df commit b0b5e05
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
26 changes: 16 additions & 10 deletions ridemyway/api/v2/controllers/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ridemyway.utils.response import Response
from ridemyway.utils.db_queries import select_user, update_user
from ridemyway.utils.warnings import edit_warnings
from flask_restful import abort


class UserController():
Expand Down Expand Up @@ -35,19 +36,24 @@ def edit_user(self, **kwargs):
user = select_user(username=username)
if 'email' in kwargs:
user_exists = select_user(email=kwargs['email'])
if user_exists and user_exists['username'] is not user['username']:
print(user_exists['username'])
print(username)
if user_exists and user_exists['username'] != username:
message = 'Email already in use by another user'
response = Response.failed(message=message)
return response, 403
for field in kwargs:
if field not in immutable_fields:
user[field] = kwargs[field]
update_user(**user)
message = 'Edit user successful'
if self.warnings:
message = self.warnings[2]
meta = self.warnings[1]
warnings = self.warnings[0]
return Response.success(message=message, meta=meta,
warnings=warnings), 201
return Response.success(message=message), 201
user_updated = update_user(**user)
if user_updated:
message = 'Edit user successful'
if self.warnings:
message = self.warnings[2]
meta = self.warnings[1]
warnings = self.warnings[0]
return Response.success(message=message, meta=meta,
warnings=warnings), 201
return Response.success(message=message), 201
# If nothing works, it's probably a server error, abort
abort(500)
15 changes: 14 additions & 1 deletion ridemyway/utils/db_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,17 @@ def select_user(username=None, email=None):


def update_user(**kwargs):
pass
sql = """UPDATE appuser
SET name=%s, gender=%s, contacts=%s, email=%s, password=%s
WHERE username=%s
"""
cur = app.conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
try:
cur.execute(sql, (kwargs['name'], kwargs['gender'], kwargs['contacts'],
kwargs['email'], kwargs['password'],
kwargs['username']))
app.conn.commit()
cur.close()
return True
except psycopg2.Error:
return False

0 comments on commit b0b5e05

Please sign in to comment.