-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
181 lines (141 loc) · 4.56 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
from flask import Flask, request, jsonify, render_template
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
import requests
from walrus import *
# import simplejson
# import json
#Init app
app = Flask(__name__)
basedir = os.path.abspath(os.path.dirname(__file__))
# Database
# app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'db.sqlite')
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:igdefault@localhost:5432/flaskapi'
# app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://owner_username:password@db_host:5432/db_name'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Init db
db = SQLAlchemy(app)
"""
from app import db
db.create_all()
"""
# cache = Cache(db)
dba = Database()
cache = dba.cache()
# Init ma
ma = Marshmallow(app)
# Product Class/Model
class Product(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=False)
# name = unique
description = db.Column(db.String(200))
price = db.Column(db.Float)
qty = db.Column(db.Integer)
def __init__(self, name, description, price, qty):
self.name = name
self.description = description
self.price = price
self.qty = qty
# Product Schema
class ProductSchema(ma.Schema):
class Meta:
fields = ('id', 'name', 'description', 'price', 'qty')
# Init Schema
product_schema = ProductSchema(strict=True)
products_schema = ProductSchema(many=True, strict=True)
# Create a Product
@app.route('/product', methods=['POST'])
def add_product():
name = request.json['name']
description = request.json['description']
price = request.json['price']
qty = request.json['qty']
new_product = Product(name, description, price, qty)
db.session.add(new_product)
db.session.commit()
return product_schema.jsonify(new_product)
# Get all products
@app.route('/product', methods=['GET'])
def get_products():
all_products = Product.query.all()
result = products_schema.dump(all_products)
return jsonify(result.data)
# Get single product
@app.route('/product/<id>', methods=['GET'])
def get_product(id):
product = Product.query.get(id)
return product_schema.jsonify(product)
# Update a Product
@app.route('/product/<id>', methods=['PUT'])
def update_product(id):
product = Product.query.get(id)
name = request.json['name']
description = request.json['description']
price = request.json['price']
qty = request.json['qty']
product.name = name
product.description = description
product.price = price
product.qty = qty
db.session.commit()
return product_schema.jsonify(product)
# Delete product
@app.route('/product/<id>', methods=['DELETE'])
def delete_product(id):
product = Product.query.get(id)
db.session.delete(product)
db.session.commit()
return product_schema.jsonify(product)
@app.route('/', methods=['GET'])
# @Cache.cached(Cache, timeout=60)
# @cache.cached(timeout=30)
def computation_task():
for i in range(1, 10000):
res = 0
result = 0
for j in range(1, 1000):
res = res + j
result = j + j
return render_template('index.html', sum=res)
@app.route('/data', methods=['GET'])
def data_api():
return jsonify([0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946])
# return jsonify(requests.get("https://jsonplaceholder.typicode.com/comments").json())
@app.route('/list_data', methods=['GET'])
# @Cache.cached(Cache, timeout=60)
def new_api():
r = requests.get("https://jsonplaceholder.typicode.com/comments")
list_data = r.json()
for num in range(0, 8):
list_data.extend(list_data)
return jsonify(list_data)
# return jsonify(requests.get("https://jsonplaceholder.typicode.com/comments").json())
@app.route('/fibonacci', methods=['GET'])
def get_fibonacci():
series_length = 10000
first = 0
second = 1
fibonacci_list = []
count = 0
while count < series_length:
fibonacci_list.append(first)
third = first + second
first = second
second = third
count = count + 1
return render_template("index.html", series=fibonacci_list)
@app.route('/direct', methods=['GET'])
def hello_word():
for i in range(1, 10000):
res = 0
result = 0
for j in range(1, 1000):
res = res + j
result = j + j
return render_template('index.html', sum=res)
# Run server
if __name__ == '__main__':
# app.run(debug=True)
app.run(debug=True, port=8000)