-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
100 lines (73 loc) · 2.24 KB
/
app.py
File metadata and controls
100 lines (73 loc) · 2.24 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
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
# Init app
app = Flask(__name__)
app.debug = True
basedir = os.path.abspath(os.path.dirname(__file__))
# Database
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'db.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# Initialize DB
db = SQLAlchemy(app)
# Init marshmallow
ma = Marshmallow(app)
class Textile(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True)
description = db.Column(db.String(200))
def __init__(self, name, description):
self.name = name
self.description = description
# Textile Schema
class TextileSchema(ma.Schema):
class Meta:
fields = ('id', 'name', 'description')
# Init Schema
# strict so that it does not report errors
textile_schema = TextileSchema()
textiles_schema = TextileSchema(many=True)
# create a Textile
@app.route('/textile', methods = ['POST'])
def add_textile():
name = request.json['name']
description = request.json['description']
new_textile = Textile(name, description)
db.session.add(new_textile)
db.session.commit()
return textile_schema.jsonify(new_textile)
# Get textiles
@app.route('/textile', methods=['GET'])
def get_textiles():
all_textiles = Textile.query.all()
result = textiles_schema.dump(all_textiles)
return jsonify(result)
# Get one textiles
@app.route('/textile/<id>', methods=['GET'])
def get_textile(id):
textile = Textile.query.get(id)
result = textile_schema.dump(textile)
return jsonify(result)
# Update a Textile
@app.route('/textile/<id>', methods = ['PUT'])
def update_textile(id):
textile = Textile.query.get(id)
# Debugger
# import pdb; pdb.set_trace()
name = request.json['name']
description = request.json['description']
textile.name = name
textile.description = description
db.session.commit()
return textile_schema.jsonify(textile)
# Remove textile
@app.route('/textile/<id>', methods=['DELETE'])
def delete_textile(id):
textile = Textile.query.get(id)
db.session.delete(textile)
db.session.commit()
return textile_schema.jsonify(textile)
# Run server
if __name__ == '__main__':
app.run(debug=True)