-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
121 lines (98 loc) · 3.33 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
from flask import Flask, request, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_marshmallow import Marshmallow
import os
# Init app
app = Flask(__name__)
"""
@app.route('/',methods=['GET'])
def get():
return jsonify({'msg': "hello world"})
"""
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
# Init db
db = SQLAlchemy(app)
# Init ma
ma = Marshmallow(app)
# Dynamixel Class/Model
class Dynamixel(db.Model):
Motor_Key = db.Column(db.String(200), unique=True)
Motor_Type = db.Column(db.String(100),unique =True)
ID = db.Column(db.Integer, primary_key=True)
PyPot_Support = db.Column(db.Boolean)
SDK_Support = db.Column(db.Boolean)
Wizard_Support = db.Column(db.Boolean)
Location = db.Column(db.String(200))
def __init__(self, Motor_Key,Motor_Type,ID,PyPot_Support,SDK_Support,Wizard_Support,Location):
self.Motor_Key = Motor_Key
self.Motor_Type = Motor_Type
self.ID = ID
self.PyPot_Support = PyPot_Support
self.SDK_Support =SDK_Support
self.Wizard_Support = Wizard_Support
self.Location = Location
# Dynamixel Schema
class DynamixelSchema(ma.Schema):
class Meta:
fields = ('Motor_Key', 'Motor_Type', 'ID', 'PyPot_Support', 'SDK_Support','Wizard_Support','Location')
# Init schema
dynamixel_schema = DynamixelSchema(strict=True)
dynamixels_schema = DynamixelSchema(many=True, strict=True)
# Create a entry
@app.route('/dynamixel', methods=['POST'])
def add_entry():
Motor_Key = request.json['Motor_Key']
Motor_Type = request.json['Motor_Type']
ID =request.json['ID']
PyPot_Support = request.json['PyPot_Support']
SDK_Support = request.json['SDK_Support']
Wizard_Support = request.json['Wizard_Support']
Location = request.json['Location']
new_entry = Dynamixel( Motor_Key,Motor_Type,ID,PyPot_Support,SDK_Support,Wizard_Support,Location)
db.session.add(new_entry)
db.session.commit()
return dynamixel_schema.jsonify(new_entry)
# Get All Products
@app.route('/dynamixel', methods=['GET'])
def get_entrys():
all_entrys = Dynamixel.query.all()
result = dynamixels_schema.dump(all_entrys)
return jsonify(result.data)
# Get Single Entry
@app.route('/dynamixel/<id>', methods=['GET'])
def get_entry(id):
entry = Dynamixel.query.get(id)
return dynamixel_schema.jsonify(entry)
# Update a entry
@app.route('/dynamixel/<id>', methods=['PUT'])
def update_entry(id):
entry = Dynamixel.query.get(id)
Motor_Key = request.json['Motor_Key']
Motor_Type = request.json['Motor_Type']
ID =request.json['ID']
PyPot_Support = request.json['PyPot_Support']
SDK_Support = request.json['SDK_Support']
Wizard_Support = request.json['Wizard_Support']
Location = request.json['Location']
entry.Motor_Key = Motor_Key
entry.Motor_Type = Motor_Type
entry.ID = ID
entry.PyPot_Support = PyPot_Support
entry.SDK_Support = SDK_Support
entry.Wizard_Support = Wizard_Support
entry.Location = Location
db.session.commit()
return dynamixel_schema.jsonify(entry)
# Delete Entry
@app.route('/dynamixel/<id>', methods=['DELETE'])
def delete_entry(id):
entry = Dynamixel.query.get(id)
db.session.delete(entry)
db.session.commit()
return dynamixel_schema.jsonify(entry)
# Run Server
if __name__ == '__main__':
app.run(debug=True)