-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.py
More file actions
71 lines (60 loc) · 2.04 KB
/
Copy pathuser.py
File metadata and controls
71 lines (60 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from flask import Flask
from flask_restful import Resource, Api, reqparse
from models import User, db
from errors import *
class UserSignup(Resource):
def __init__(self):
self.parser = reqparse.RequestParser()
self.parser.add_argument('name')
self.parser.add_argument('phone')
self.parser.add_argument('email')
def post(self):
# Extract data from URL
args = self.parser.parse_args()
name = args['name']
phone = args['phone']
email = args['email']
# Validate data
if len(name) == 0:
raise NameEmptyError()
if len(email) == 0:
raise EmailEmptyError()
if len(phone) != 10:
raise InvalidPhoneNumberError()
#Insert into database
try:
user_db_object = User(name, phone, email)
db.session.add(user_db_object)
db.session.commit()
except:
raise DBInsertError()
# Check if user insertion is successfull in db
try:
inserted_user = User.query.filter_by(phone=phone).first()
if inserted_user is None:
return {"success": "false"}, 400
else:
return {"success":"true", "name" : inserted_user.username}
except:
raise DBQueryError()
class UserLogin(Resource):
def __init__(self):
self.parser = reqparse.RequestParser()
self.parser.add_argument('phone')
def get(self):
# Extract data from URL
args = self.parser.parse_args()
phone = args['phone']
# Get corresponding User's OTP
try:
user_obj = User.query.filter_by(phone=phone).first()
return {"success" : "true", "otp" : user_obj.otp}
except:
raise UserNotFound()
if __name__ == '__main__':
db.create_all()
app = Flask(__name__)
api = Api(app, catch_all_404s=True, errors=CUSTOM_ERRORS)
api.add_resource(UserSignup, '/user/signup')
api.add_resource(UserLogin, '/user/login')
app.run(debug=True)