Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Hodor Web Backend
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Akash Ravi
Hodor Web Backend
Commits
9c8a6b32
Verified
Commit
9c8a6b32
authored
7 years ago
by
Sachin Kamath
Browse files
Options
Downloads
Patches
Plain Diff
Add API endpoint to check if a user exists in database
Signed-off-by:
Sachin S. Kamath
<
sskamath96@gmail.com
>
parent
8e7c16dc
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
hodor/controllers/user.py
+49
-8
49 additions, 8 deletions
hodor/controllers/user.py
with
49 additions
and
8 deletions
hodor/controllers/user.py
+
49
−
8
View file @
9c8a6b32
...
...
@@ -10,7 +10,7 @@ from sqlalchemy.exc import IntegrityError
def
get_all_users
():
# TODO: Authentication for calling this API endpoint. Admin only.
"""
This function iterates the database to find all users and returns
it
as JSON
.
This function iterates the database to find all users and returns as JSON
:return: Response Code
"""
response
=
dict
()
...
...
@@ -20,8 +20,8 @@ def get_all_users():
for
user
in
User
.
get_all
():
current_user
=
dict
()
'''
This can be done by directly dumping the dictionary but there
'
s always
a risk of data leak.
So, we pick what we need to give out out of the API
.
This can be done by directly dumping the dictionary but there
'
s always
a risk of data leak.
So, we pick what we need to give out out of the API
'''
current_user
[
'
username
'
]
=
user
.
username
current_user
[
'
first_name
'
]
=
user
.
first_name
...
...
@@ -33,7 +33,8 @@ def get_all_users():
return
response
@app.route
(
'
/users/new
'
,
methods
=
[
'
GET
'
,
'
POST
'
])
# Register a user
@app.route
(
'
/users/new
'
,
methods
=
[
'
POST
'
])
def
add_new_user
():
"""
This function adds a new user
...
...
@@ -48,21 +49,61 @@ def add_new_user():
newuser
[
'
last_name
'
]
=
str
(
request
.
data
.
get
(
'
last_name
'
).
strip
())
newuser
[
'
email
'
]
=
str
(
request
.
data
.
get
(
'
email
'
).
strip
())
newuser
[
'
password
'
]
=
str
(
request
.
data
.
get
(
'
password
'
).
strip
())
newuser
[
'
verification_code
'
]
=
str
(
request
.
data
.
get
(
'
verification_code
'
).
strip
())
newuser
[
'
verification_code
'
]
=
str
(
request
.
data
.
get
(
'
verification_code
'
).
strip
())
except
Exception
as
e
:
print
(
e
)
abort
(
500
)
user
=
User
(
**
newuser
)
user
.
save
()
return
make_response
(
jsonify
(
status
=
201
,
msg
=
"
User {} successfully added to database
"
.
format
(
user
.
username
)),
201
)
return
make_response
(
jsonify
(
status
=
201
,
msg
=
"
User {} successfully added
"
+
"
to database
"
.
format
(
user
.
username
)),
201
)
#################################################
# Check for an existing user in the database #
#################################################
@app.route
(
'
/users/check
'
,
methods
=
[
'
POST
'
])
def
check_for_existing_user
():
errors
=
[]
# If the username field is passed, checl the username field.
if
request
.
data
.
get
(
'
username
'
):
check_username
=
str
(
request
.
data
.
get
(
'
username
'
).
strip
())
user_checkuser
=
User
.
query
.
filter_by
(
username
=
check_username
).
first
()
if
user_checkuser
:
errors
.
append
({
'
field
'
:
'
username
'
,
'
error
'
:
'
{} is taken
'
.
format
(
check_username
)
})
del
(
user_checkuser
)
# If the email field is set, check for duplicate email
if
request
.
data
.
get
(
'
email
'
):
check_email
=
str
(
request
.
data
.
get
(
'
email
'
).
strip
())
user_checkmail
=
User
.
query
.
filter_by
(
email
=
check_email
).
first
()
if
user_checkmail
:
errors
.
append
({
'
field
'
:
'
email
'
,
'
error
'
:
'
{} exists in the database
'
.
format
(
check_email
)
})
if
errors
:
return
make_response
(
jsonify
(
status
=
400
,
err
=
errors
),
400
)
else
:
return
jsonify
(
status
=
200
,
msg
=
"
ok
"
)
@app.errorhandler
(
IntegrityError
)
def
handle_sqlalchemy_assertion_error
(
err
):
try
:
'''
err.orig.args is from the DBAPIError class of SQLAlchemy. It usually contains the original error message.
The below is an attempt to clean up the message and only return the relevant part to API
err.orig.args is from the DBAPIError class of SQLAlchemy. It usually
contains the original error message.
The below is an attempt to clean up the message and only return the
relevant part to API
'''
errmsg
=
err
.
orig
.
args
[
0
].
split
(
'
\n
'
)[
1
][
9
:]
except
IndexError
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment