2020-04-13 21:14:06 +02:00
|
|
|
#!/usr/bin/env python3
|
2020-04-14 13:48:11 +02:00
|
|
|
import tempfile
|
|
|
|
from flask import request, jsonify, current_app, abort, Response
|
2020-04-13 21:14:06 +02:00
|
|
|
from flask_classful import FlaskView, route
|
2020-04-14 13:48:11 +02:00
|
|
|
from model import db, Default, AIModel
|
|
|
|
from minio.error import BucketAlreadyExists, BucketAlreadyOwnedByYou, ResponseError, NoSuchKey
|
|
|
|
from schemas import AIModelSchema, DefaultSchema
|
2020-04-14 14:02:35 +02:00
|
|
|
from marshmallow.exceptions import ValidationError
|
2020-04-14 13:48:11 +02:00
|
|
|
from utils import json_required, storage
|
2020-04-13 21:14:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ModelView(FlaskView):
|
2020-04-14 13:48:11 +02:00
|
|
|
aimodel_schema = AIModelSchema(many=False)
|
2020-04-14 02:01:44 +02:00
|
|
|
default_schema = DefaultSchema(many=False)
|
|
|
|
|
|
|
|
@json_required
|
2020-04-13 21:14:06 +02:00
|
|
|
def post(self):
|
|
|
|
return jsonify({})
|
|
|
|
|
|
|
|
def get(self, _id: str):
|
2020-04-14 13:48:11 +02:00
|
|
|
|
|
|
|
if _id == "$default":
|
|
|
|
default = Default.query.first_or_404() # TODO: Kitalálni, hogy inkább a latestestest-el térjen-e vissza
|
|
|
|
m = default.default
|
|
|
|
else:
|
|
|
|
m = AIModel.query.fiter_by(id=_id).first_or_404()
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = storage.connection.get_object(current_app.config['MINIO_BUCKET_NAME'], m.id)
|
|
|
|
except NoSuchKey:
|
|
|
|
abort(500, "The ID is stored in the database but not int the Object Store")
|
|
|
|
|
|
|
|
return Response(data.stream(), mimetype=data.headers['Content-type'])
|
2020-04-13 21:14:06 +02:00
|
|
|
|
|
|
|
@route('<_id>/details')
|
|
|
|
def get_details(self, _id: str):
|
|
|
|
|
2020-04-14 13:48:11 +02:00
|
|
|
if _id == "$default":
|
|
|
|
default = Default.query.first_or_404() # TODO: Kitalálni, hogy inkább a latestestest-el térjen-e vissza
|
|
|
|
m = default.default
|
|
|
|
else:
|
|
|
|
m = AIModel.query.fiter_by(id=_id).first_or_404()
|
2020-04-13 21:14:06 +02:00
|
|
|
|
2020-04-14 13:48:11 +02:00
|
|
|
return jsonify(self.aimodel_schema.dump(m))
|
2020-04-14 02:01:44 +02:00
|
|
|
|
2020-04-14 13:48:11 +02:00
|
|
|
def delete(self, _id: str):
|
|
|
|
|
|
|
|
m = AIModel.query.fiter_by(id=_id).first_or_404()
|
2020-04-14 02:01:44 +02:00
|
|
|
|
2020-04-14 13:48:11 +02:00
|
|
|
storage.connection.remove_object(current_app.config['MINIO_BUCKET_NAME'], m.id)
|
2020-04-14 02:01:44 +02:00
|
|
|
|
2020-04-14 13:48:11 +02:00
|
|
|
db.session.delete(m)
|
2020-04-14 02:01:44 +02:00
|
|
|
db.session.commit()
|
|
|
|
|
2020-04-14 13:48:11 +02:00
|
|
|
return '', 204
|
|
|
|
|
|
|
|
@json_required
|
|
|
|
@route('$default', methods=['PUT'])
|
|
|
|
def put_default(self):
|
2020-04-14 14:02:35 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
req = self.default_schema.load(request.json())
|
|
|
|
except ValidationError as e:
|
|
|
|
abort(404, str(e))
|
|
|
|
|
|
|
|
m = AIModel.query.fiter_by(id=req['id']).first_or_404()
|
|
|
|
|
|
|
|
Default.query.delete()
|
|
|
|
new_default = Default(m)
|
|
|
|
db.session.add(new_default)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return jsonify(self.aimodel_schema.dump(m))
|