-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
210 lines (175 loc) · 5.78 KB
/
app.py
File metadata and controls
210 lines (175 loc) · 5.78 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
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
from flask import Flask
from flask_restx import Api, Resource, fields
from werkzeug.contrib.fixers import ProxyFix
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
import os
from werkzeug.exceptions import BadRequest
# import './src/routes/users.py'
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://postgres:Sidhu@9693@localhost:5432/news?gssencmode=disable"
db = SQLAlchemy(app)
migrate = Migrate(app, db)
class CarsModel(db.Model):
__tablename__ = 'cars'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String())
model = db.Column(db.String())
doors = db.Column(db.Integer())
def __init__(self, name, model, doors):
self.name = name
self.model = model
self.doors = doors
def __repr__(self):
return f"<Car {self.name}>"
app.wsgi_app = ProxyFix(app.wsgi_app)
api = Api(app, version='1.0', title='TodoMVC API',
description='A simple TodoMVC API',
)
ns = api.namespace('todos', description='TODO operations')
cs = api.namespace('cars', description='Cars operations')
todo = api.model('Todo', {
'id': fields.Integer(readonly=True, description='The task unique identifier'),
'task': fields.String(required=True, description='The task details')
})
car = api.model('Car', {
'id': fields.Integer(readonly=True, description='The task unique identifier'),
'name': fields.String(required=True, description='cars'),
'model': fields.String(required=True, description='cars'),
'doors': fields.Integer(required=True, description='cars'),
})
class TodoDAO(object):
def __init__(self):
self.counter = 0
self.todos = []
def get(self, id):
for todo in self.todos:
if todo['id'] == id:
return todo
api.abort(404, "Todo {} doesn't exist".format(id))
def create(self, data):
todo = data
todo['id'] = self.counter = self.counter + 1
self.todos.append(todo)
return todo
def update(self, id, data):
todo = self.get(id)
todo.update(data)
return todo
def delete(self, id):
todo = self.get(id)
self.todos.remove(todo)
DAO = TodoDAO()
DAO.create({'task': 'Build an API'})
DAO.create({'task': '?????'})
DAO.create({'task': 'profit!'})
@ns.route('/')
class TodoList(Resource):
'''Shows a list of all todos, and lets you POST to add new tasks'''
@ns.doc('list_todos')
@ns.marshal_list_with(todo)
def get(self):
'''List all tasks'''
return DAO.todos
@ns.doc('create_todo')
@ns.expect(todo)
@ns.marshal_with(todo, code=201)
def post(self):
'''Create a new task'''
return DAO.create(api.payload), 201
@ns.route('/<int:id>')
@ns.response(404, 'Todo not found')
@ns.param('id', 'The task identifier')
class Todo(Resource):
'''Show a single todo item and lets you delete them'''
@ns.doc('get_todo')
@ns.marshal_with(todo)
def get(self, id):
'''Fetch a given resource'''
return DAO.get(id)
@ns.doc('delete_todo')
@ns.response(204, 'Todo deleted')
def delete(self, id):
'''Delete a task given its identifier'''
DAO.delete(id)
return '', 204
@ns.expect(todo)
@ns.marshal_with(todo)
def put(self, id):
'''Update a task given its identifier'''
return DAO.update(id, api.payload)
# Cars :
@cs.route('/')
class CarsList(Resource):
'''Shows a list of all todos, and lets you POST to add new tasks'''
@cs.doc('list_cars')
# @cs.marshal_list_with(cars)
def get(self):
'''List all cars'''
cars = CarsModel.query.all()
results = [
{
"name": car.name,
"model": car.model,
"doors": car.doors
} for car in cars]
print("cars=", results)
return results, 200
@cs.doc('create_car')
@cs.expect(car)
# @cs.marshal_with(car, code=201)
def post(self):
'''Create a new car'''
data = api.payload
try:
new_car = CarsModel(
name=data['name'], model=data['model'], doors=data['doors'])
db.session.add(new_car)
db.session.commit()
return {"message": f"car {new_car.name} has been created successfully."},201
except:
raise BadRequest('My custom message')
# Operations on /
@cs.route('/<int:id>')
@cs.response(404, 'car not found')
@cs.param('id', 'The task identifier')
class Car(Resource):
'''Show a single car item and lets you delete them'''
@ns.doc('get_car')
# @ns.marshal_with(car)
def get(self, id):
'''Fetch a given resource'''
print("GET id",id)
car = CarsModel.query.get_or_404(id)
print("GET cR",car)
response = {
"name": car.name,
"model": car.model,
"doors": car.doors
}
return {"message": "success", "car": response}
@ns.doc('delete_car')
@ns.response(204, 'car deleted')
def delete(self, id):
'''Delete a task given its identifier'''
car = CarsModel.query.get_or_404(id)
db.session.delete(car)
db.session.commit()
return {"message": f"Car {car.name} successfully deleted."},204
@ns.expect(car)
# @ns.marshal_with(car)
def put(self, id):
'''Update a car given its identifier'''
car = CarsModel.query.get_or_404(id)
data = api.payload
car.name = data['name']
car.model = data['model']
car.doors = data['doors']
db.session.add(car)
db.session.commit()
return {"message": f"car {car.name} successfully updated"}
# Users route:
# swagger
users_swagger = api.namespace('users', description='users operations')
if __name__ == '__main__':
app.run(debug=True)