-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
229 lines (179 loc) · 6.68 KB
/
app.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
from flask import Flask, jsonify, request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, Integer, Float, String
import os
from flask_marshmallow import Marshmallow
from flask_jwt_extended import JWTManager, jwt_required, create_access_token
from flask_mail import Mail, Message
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'vehicles.db')
app.config['JWT_SECRET_KEY'] = 'not-a-secret'
app.config['MAIL_SERVER'] = 'smtp.mailtrap.io'
app.config['MAIL_USERNAME'] = os.environ['MAIL_USERNAME']
app.config['MAIL_PASSWORD'] = os.environ['MAIL_PASSWORD']
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USE_SSL'] = False
db = SQLAlchemy(app)
ma = Marshmallow(app)
jwt = JWTManager(app)
mail = Mail(app)
@app.cli.command('db_create')
def db_create():
db.create_all()
print('DB Created!')
@app.cli.command('db_drop')
def db_drop():
db.drop_all()
print('DB Dropped!')
@app.cli.command('db_seed')
def db_seed():
vehicle1 = Vehicles(vname='Romeo Pourche', vtype='Sports Car', vowner='Poursche', vd='1003.87')
vehicle2 = Vehicles(vname='E Class', vtype='Jeep', vowner='Mercedes Benz', vd='1058.0')
vehicle3 = Vehicles(vname='Prado', vtype='Land Rover', vowner='Toyota', vd='54.9')
db.session.add(vehicle1)
db.session.add(vehicle2)
db.session.add(vehicle3)
test_user = User(first_name='John', last_name='Matrix', email='jmatrix@mia.com', password='567890qw')
db.session.add(test_user)
db.session.commit()
print('DB Seeded!')
@app.route('/')
def index():
return jsonify(message='Welcome to Flask Rest API'), 200
# crouting
@app.route('/hello')
def hello():
return 'Hello World', 200
@app.route('/param')
def param():
name = request.args.get('name')
age = int(request.args.get('age'))
if age < 18:
return jsonify(message='sorry ' + name + ', you are not old enough'), 401
else:
return jsonify(message='welcome ' + name + ', you are old enough')
@app.route('/vars/<string:name>/<int:age>')
def var(name: str, age: int):
if age < 18:
return jsonify(message='sorry ' + name + ', you are not old enough'), 401
else:
return jsonify(message='welcome ' + name + ', you are old enough')
@app.route('/vehicles', methods=['GET'])
def vehicles():
v_list = Vehicles.query.all()
result = vehicles_schema.dump(v_list)
return jsonify(result)
@app.route('/register', methods=['POST'])
def register():
email = request.form['email']
test = User.query.filter_by(email=email).first()
if test:
return jsonify(message='Email already exists!'), 409
else:
first_name = request.form['first_name']
last_name = request.form['last_name']
password = request.form['password']
user = User(first_name=first_name, last_name=last_name, email=email, password=password)
db.session.add(user)
db.session.commit()
return jsonify(message='User registered successfully!'), 201
@app.route('/login', methods=['POST'])
def login():
if request.is_json:
email = request.json['email']
password = request.json['password']
else:
email = request.form['email']
password = request.form['password']
test = User.query.filter_by(email=email, password=password).first()
if test:
access_token = create_access_token(identity=email)
return jsonify(message='Login SSuccessful!', access_token=access_token)
else:
return jsonify(message='Wrong credentials!'), 401
@app.route('/retrieve_pass/<string:email>', methods=['GET'])
def retrieve_pass(email: str):
user = User.query.filter_by(email=email).first()
if user:
msg = Message('Your Vehicles Collection password is ' + user.password, sender='support@vehicles.com', recipients=[email])
mail.send(msg)
return jsonify(message='Password sent to ' + email)
else:
return jsonify(message='That email does not exist'), 401
@app.route('/vehicle/<int:id>', methods=['GET'])
def vehicle(id: int):
vehicle = Vehicles.query.filter_by(vid=id).first()
if vehicle:
result = vehicle_schema.dump(vehicle)
return jsonify(result)
else:
return jsonify(message='Vehicle does not exist'), 404
@app.route('/add_vehicle', methods=['POST'])
@jwt_required
def add_vehicle():
vehicle_name = request.form['vname']
test = Vehicles.query.filter_by(vname=vehicle_name).first()
if test:
return jsonify(message='That vehicle already exists!'), 409 # conflict
else:
vtype = request.form['vtype']
vowner = request.form['vowner']
vd = float(request.form['vd'])
new_vehicle = Vehicles(vname=vehicle_name, vtype=vtype, vowner=vowner, vd=vd)
db.session.add(new_vehicle)
db.session.commit()
return jsonify(message='New vehicle added to collection'), 201
@app.route('/update_vehicle', methods=['PUT'])
@jwt_required
def update_vehicle():
vid = int(request.form['vid'])
vehicle = Vehicles.query.filter_by(vid=vid).first()
if vehicle:
vehicle.vname = request.form['vname']
vehicle.vtype = request.form['vtype']
vehicle.vowner = request.form['vowner']
vehicle.vd = float(request.form['vd'])
db.session.commit()
return jsonify(message='Vehicle updated successfully!'), 202
else:
return jsonify(message='That vehicle does not exist!'), 404
@app.route('/delete_vehicle/<int:vid>', methods=['DELETE'])
@jwt_required
def delete_vehicle(vid: int):
vehicle = Vehicles.query.filter_by(vid=vid).first()
if vehicle:
db.session.delete(vehicle)
db.session.commit()
return jsonify(message='Planet deleted!'), 202
else:
return jsonify(message='Planet does not exist!'), 404
# db models
class User(db.Model):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
first_name = Column(String)
last_name = Column(String)
email = Column(String, unique=True)
password = Column(String)
class Vehicles(db.Model):
__tablename__ = 'vehicles'
vid = Column(Integer, primary_key=True)
vname = Column(String)
vtype = Column(String)
vowner = Column(String)
vd = Column(Float)
class UserSchema(ma.Schema):
class Meta:
fields = ('id', 'first_name', 'last_name', 'email', 'password')
class VehiclesSchema(ma.Schema):
class Meta:
fields = ('vid', 'vname', 'vtype', 'vowner', 'vd')
user_schema = UserSchema()
users_schema = UserSchema(many=True)
vehicle_schema = VehiclesSchema()
vehicles_schema = VehiclesSchema(many=True)
if __name__ == '__main__':
app.run(port='4500')