Skip to content

Commit 5d9f431

Browse files
author
App Generator
committed
release v0.0.3 - Full CRUD over Data table
1 parent 4142f10 commit 5d9f431

File tree

4 files changed

+181
-7
lines changed

4 files changed

+181
-7
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Change Log
22

3+
## [0.0.3] 2022-02-07
4+
### Improvements
5+
6+
- New API Methods
7+
- CRUD methods over `Data` table
8+
39
## [0.0.2] 2022-02-05
410
### Improvements
511

api/routes.py

Lines changed: 172 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,40 @@
1717
"""
1818
API Interface:
1919
20-
- /api/data
20+
- /data
2121
- GET: return all items
22-
23-
- /api/from_file
22+
- POST: create a new item
23+
24+
- /data/:id
2425
- GET : get item
26+
- PUT : update item
27+
- DELETE : delete item
28+
"""
2529

2630
"""
31+
Flask-RestX models Request & Response DATA
32+
"""
33+
34+
# Used to validate input data for creation
35+
create_model = rest_api.model('CreateModel', {"code": fields.String(required=True, min_length=2, max_length=64),
36+
"name": fields.String(required=True, min_length=2, max_length=128),
37+
"value": fields.Integer(required=True),
38+
"currency": fields.String(required=True, min_length=3, max_length=3),
39+
"type": fields.String(required=False, default='transaction'),
40+
})
41+
42+
# Used to validate input data for update
43+
update_model = rest_api.model('UpdateModel', {"code": fields.String(required=False, min_length=2, max_length=64),
44+
"name": fields.String(required=False, min_length=2, max_length=128),
45+
"value": fields.Integer(required=False),
46+
"currency": fields.String(required=False, min_length=3, max_length=3),
47+
"type": fields.String(required=False, default='transaction'),
48+
})
49+
50+
"""
51+
Return Files - Served by @APP object
52+
"""
2753

28-
# Return Files - Served by @APP object
2954
@app.route('/api/from_file')
3055
def api_from_file():
3156
return send_from_directory(os.path.join(app.root_path, 'static', 'datatables'), 'data.json')
@@ -34,6 +59,22 @@ def api_from_file():
3459
Flask-Restx routes
3560
"""
3661

62+
"""
63+
class Data(db.Model):
64+
65+
66+
id = db.Column(db.Integer, primary_key=True)
67+
68+
code = db.Column(db.String(64)) # product code
69+
name = db.Column(db.String(128)) # product name
70+
value = db.Column(db.Integer) # numeric
71+
currency = db.Column(db.String(10)) # string: usd, euro
72+
type = db.Column(db.String(64)) # transaction
73+
74+
ts = db.Column(db.Integer, default=datetime.utcnow().timestamp())
75+
76+
"""
77+
3778
@rest_api.route('/api/data')
3879
class Items(Resource):
3980

@@ -53,4 +94,131 @@ def get(self):
5394
mimetype='application/json'
5495
)
5596

97+
return response
98+
99+
"""
100+
Create new item
101+
"""
102+
@rest_api.expect(create_model, validate=True)
103+
def post(self):
104+
105+
# Read ALL input
106+
req_data = request.get_json()
107+
108+
code = req_data.get("code")
109+
name = req_data.get("name")
110+
value = req_data.get("value")
111+
currency = req_data.get("currency")
112+
type = req_data.get("type")
113+
114+
# Create new object
115+
new_item = Data(code=code, name=name, value=value, currency=currency, type=type)
116+
117+
# Save the data
118+
new_item.save()
119+
120+
response = app.response_class(
121+
response=json.dumps( new_item.toDICT() ),
122+
status=200,
123+
mimetype='application/json'
124+
)
125+
126+
return response
127+
128+
@rest_api.route('/api/data/<int:id>')
129+
class ItemManager(Resource):
130+
131+
"""
132+
Return Item
133+
"""
134+
def get(self, id):
135+
136+
response_data = []
137+
status = 400
138+
139+
item = Data.get_by_id(id)
140+
141+
if item:
142+
status = 200
143+
response_data = item.toDICT()
144+
145+
response = app.response_class(
146+
response=json.dumps( response_data ),
147+
status=status,
148+
mimetype='application/json'
149+
)
150+
151+
return response
152+
153+
"""
154+
Update Item
155+
"""
156+
@rest_api.expect(update_model, validate=True)
157+
def put(self, id):
158+
159+
response_data = []
160+
status = 400
161+
162+
# Read ALL input
163+
req_data = request.get_json()
164+
165+
item = Data.get_by_id(id)
166+
167+
if item:
168+
169+
status = 200
170+
171+
# Update data
172+
if req_data.get("code"):
173+
item.code = req_data.get("code")
174+
175+
if req_data.get("name"):
176+
item.name = req_data.get("name")
177+
178+
if req_data.get("value"):
179+
item.value = req_data.get("value")
180+
181+
if req_data.get("currency"):
182+
item.currency = req_data.get("currency")
183+
184+
if req_data.get("type"):
185+
item.type = req_data.get("type")
186+
187+
# Save the data
188+
item.save()
189+
190+
response_data = item.toDICT()
191+
192+
response = app.response_class(
193+
response=json.dumps( response_data ),
194+
status=status,
195+
mimetype='application/json'
196+
)
197+
198+
return response
199+
200+
"""
201+
Delete Item
202+
"""
203+
def delete(self, id):
204+
205+
response_data = []
206+
status = 400
207+
208+
item = Data.get_by_id(id)
209+
210+
if item:
211+
212+
status = 200
213+
214+
# Delete and save the change
215+
Data.query.filter_by(id=id).delete()
216+
db.session.commit()
217+
218+
response = app.response_class(
219+
response=json.dumps( response_data ),
220+
status=status,
221+
mimetype='application/json'
222+
)
223+
56224
return response

app/templates/includes/sidebar.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ <h2 class="h5 mb-3">Hi, Jane</h2>
5656
<span class="sidebar-icon">
5757
<svg class="icon icon-xs me-2" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M4 4a2 2 0 00-2 2v1h16V6a2 2 0 00-2-2H4z"></path><path fill-rule="evenodd" d="M18 9H2v5a2 2 0 002 2h12a2 2 0 002-2V9zM4 13a1 1 0 011-1h1a1 1 0 110 2H5a1 1 0 01-1-1zm5-1a1 1 0 100 2h1a1 1 0 100-2H9z" clip-rule="evenodd"></path></svg>
5858
</span>
59-
<span class="sidebar-text">Data Tables - API</span>
59+
<span class="sidebar-text">DataTables API</span>
6060
</a>
6161
</li>
6262

@@ -65,7 +65,7 @@ <h2 class="h5 mb-3">Hi, Jane</h2>
6565
<span class="sidebar-icon">
6666
<svg class="icon icon-xs me-2" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path d="M4 4a2 2 0 00-2 2v1h16V6a2 2 0 00-2-2H4z"></path><path fill-rule="evenodd" d="M18 9H2v5a2 2 0 002 2h12a2 2 0 002-2V9zM4 13a1 1 0 011-1h1a1 1 0 110 2H5a1 1 0 01-1-1zm5-1a1 1 0 100 2h1a1 1 0 100-2H9z" clip-rule="evenodd"></path></svg>
6767
</span>
68-
<span class="sidebar-text">Data Tables - From File</span>
68+
<span class="sidebar-text">DataTables File</span>
6969
</a>
7070
</li>
7171

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "flask-volt-datatables",
33
"mastertemplate": "flask-volt-datatables",
4-
"version": "0.0.2",
4+
"version": "0.0.3",
55
"description": "Template project - Flask/Jinja2 Theme",
66
"scripts": {},
77
"repository": {

0 commit comments

Comments
 (0)