-
Notifications
You must be signed in to change notification settings - Fork 0
/
apicall.py
220 lines (184 loc) · 6.6 KB
/
apicall.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
from flask import Flask, jsonify, request
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, Integer, String, Float
from flask_marshmallow import Marshmallow
from flask_jwt_extended import JWTManager, jwt_required, create_access_token
from flask_mail import Mail, Message
import os
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+os.path.join(basedir, 'planets.db')
app.config['JWT_SECRET_KEY']='super_secret_key'
app.config['MAIL_SERVER'] = 'smtp.mailtrap.io'
#app.config['MAIL_USERNAME'] = os.environ['MAIL_USERNAME']
#app.config['MAIL_PASSWORD'] = os.environ['MAIL_PASSWORD']
db=SQLAlchemy(app)
ma=Marshmallow(app)
jwt=JWTManager(app)
mail = Mail(app)
@app.cli.command('db_create')
def db_create():
db.create_all()
@app.cli.command('db_drop')
def db_drop():
db.drop_all()
@app.cli.command('db_seed')
def db_seed():
mercury=Planet(pname='mercury',
ptype='plan',
home_star='sol',
mass=3.222545,
radius=3211,
distance=5664)
venus=Planet(pname='venus',
ptype='plan',
home_star='sol',
mass=3.222545,
radius=3211,
distance=5664)
db.session.add(mercury)
db.session.add(venus)
test_user=User(fname='Om',
lname='Panchal',
email='omx@test.com',
password='123456')
db.session.add(test_user)
db.session.commit()
@app.route('/apicall')
def apicall():
return jsonify(message='hello API')
@app.route('/not_found')
def not_found():
return jsonify(message='Resource Not found'), 404
@app.route('/parameters/<string:name>/<int:age>')
def parameters(name: str, age: int):
if age < 18:
return jsonify(message='You are not old enough to visit this page'), 401
else:
return jsonify(message='Welcome '+name+', you are old enough to visit the website!')
@app.route('/planets', methods=['GET'])
def planets():
planets_list=Planet.query.all()
result = planets_schema.dump(planets_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='That email exists!'), 409
else:
first_name = request.form['fname']
last_name= request.form['lname']
password = request.form['password']
user = User(fname=first_name, lname=last_name, email=email, password=password)
db.session.add(user)
db.session.commit()
return jsonify(message='created 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 successfull', access_token=access_token)
else:
return jsonify(message='invalid email'), 401
@app.route('/forgotpass/<string:email>', methods=['GET'])
def forgotpass(email:str):
#checks whether user exits and stores the results in variable 'user'
user = User.query.filter_by(email=email).first()
if user:
msg = Message("your planetary api password is"+user.password,
sender = "admin@wwb.com",
recipients=[email])
mail.send(msg)
return jsonify(message = "Password sent to "+email)
else:
return jsonify(message = "does not exists")
#gets the planet name for id of the planet
@app.route('/planet_details/<int:planet_id>', methods=['GET'])
def planet_details(planet_id : int):
test = Planet.query.filter_by(planet_id=planet_id).first()
if test:
result = planet_schema.dump(test)
return jsonify(result)
else:
return jsonify(message='planet for the given id does not exists'), 404
@app.route('/add_planet', methods = ['POST'])
@jwt_required
def add_planet():
planet_name=request.form['pname']
test = Planet.query.filter_by(pname=planet_name).first()
if test:
return jsonify('Planet already exists!')
else:
planet_name=request.form['pname']
ptype = request.form['ptype']
home_star = request.form['hstar']
mass = request.form['mass']
radius = request.form['radius']
distance = request.form['distance']
new_planet = Planet(pname=planet_name,
ptype = ptype,
home_star = home_star,
mass=mass,
radius=radius,
distance= distance)
db.session.add(new_planet)
db.session.commit()
return jsonify(message="You added a planet to the database!")
@app.route('/update_planet', methods=['PUT'])
@jwt_required
def update_planet():
pname = request.form['pname']
test = Planet.query.filter_by(pname=pname).first()
if test:
test.ptype = request.form['ptype']
test.home_star = request.form['hstar']
test.radius = float(request.form['radius'])
db.session.commit()
return jsonify(message="Planet updated")
else:
return jsonify(message="Planet does not exist")
@app.route('/delete_planet', methods=['DELETE'])
def delete_planet():
pname = request.form['pname']
test = Planet.query.filter_by(pname=pname).first()
if test:
db.session.delete(test)
db.session.commit()
return jsonify(message = "Planet successfully deleted!")
else:
return jsonify(message="Planet with the given name does not exist!")
class User(db.Model):
__tablename__='users'
id = Column(Integer, primary_key=True)
fname = Column(String)
lname = Column(String)
email = Column(String, unique=True)
password = Column(String)
class Planet(db.Model):
__tablename__ = 'planets'
planet_id = Column(Integer, primary_key=True)
pname = Column(String)
ptype = Column(String)
home_star = Column(String)
mass = Column(Float)
radius = Column(Float)
distance = Column(Float)
class UserSchema(ma.Schema):
class Meta:
fields = ('id', 'fname', 'lname', 'email', 'password')
class PlanetSchema(ma.Schema):
class Meta:
fields = ('planet_id','pname', 'ptype', 'home_star', 'mass', 'radius', 'distance')
planet_schema=PlanetSchema()
planets_schema=PlanetSchema(many=True)
user_schema=UserSchema()
users_schema=UserSchema(many=True)