-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatches.py
47 lines (37 loc) · 1.5 KB
/
batches.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
from flask import jsonify, request, url_for, abort
from app import db, log
from app.models import Author, Batch
from app.api import bp
from sqlalchemy.exc import IntegrityError
@bp.route('/batches/generate', methods=['GET'])
def generate_batches():
log.debug(f"ENTER /batches/generate")
b = Batch(name="OX-OILS-80DC-21-11-28")
db.session.add(b)
try:
db.session.commit()
except IntegrityError as e:
if "duplicate" in str(e.orig).lower():
return jsonify({"error": f"{e.params[0]} already exists in DB"})
else:
raise e
return jsonify(b.to_dict())
@bp.route('/batches/<int:id>', methods=['GET'])
def get_batch(id):
log.debug(f"ENTER /batches/id/{id}")
return jsonify(Batch.query.get_or_404(id).to_dict())
@bp.route('/batches', methods=['GET'])
def get_batches():
log.debug(f"ENTER /batches")
page = request.args.get('page', 1, type=int)
per_page = min(request.args.get('per_page', 10, type=int), 100)
data = Batch.to_collection_dict(Batch.query, page, per_page, 'api.get_batches')
return jsonify(data)
@bp.route('/batches/<int:id>/experiments', methods=['GET'])
def get_batch_experiments(id):
log.debug(f"ENTER /batches/{id}/experiments")
batch = Batch.query.get_or_404(id)
page = request.args.get('page', 1, type=int)
per_page = min(request.args.get('per_page', 10, type=int), 100)
data = Batch.to_collection_dict(batch.experiments, page, per_page, 'api.get_batch_experiments', id=id)
return jsonify(data)