Implemeted getters

This commit is contained in:
Pünkösd Marcell 2020-04-14 13:48:11 +02:00
parent 43a0fe0ca2
commit f97228b61c
9 changed files with 65 additions and 31 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from .db import db from .db import db
from .default import Default from .default import Default
from .details import Details from .aimodel import AIModel

View File

@ -1,16 +1,18 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from .db import db from .db import db
from sqlalchemy.sql import func from sqlalchemy.sql import func
from sqlalchemy.dialects.postgresql import UUID
import uuid
class Details(db.Model): class AIModel(db.Model):
id = db.Column(db.Integer, primary_key=True, auto_increment=True) id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4, unique=True, nullable=False)
name = db.Column(db.String(30), key=True)
timestamp = db.Column(db.TIMESTAMP, nullable=False, server_default=func.now()) timestamp = db.Column(db.TIMESTAMP, nullable=False, server_default=func.now())
# details
mid_window = db.Column(db.Float) mid_window = db.Column(db.Float)
mid_step = db.Column(db.Float) mid_step = db.Column(db.Float)
short_window = db.Column(db.Float) short_window = db.Column(db.Float)
short_step = db.Column(db.Float) short_step = db.Column(db.Float)
compute_beat = db.Column(db.Float) compute_beat = db.Column(db.Float)
type = db.Column(db.String(15)) type = db.Column(db.String(15))

View File

@ -1,7 +1,8 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from .db import db from .db import db
from sqlalchemy.dialects.postgresql import UUID
class Default(db.Model): class Default(db.AIModel):
default_id = db.Column(db.Integer, db.ForeignKey("details.id"), nullable=False) default_id = db.Column(UUID(as_uuid=True), db.ForeignKey("aimodel.id"), nullable=False)
default = db.relationship("Details") default = db.relationship("AIModel")

View File

@ -1,3 +1,3 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from .default_schema import DefaultSchema from .default_schema import DefaultSchema
from .details_schema import DetailsSchema from .aimodel_schema import AIModelSchema

View File

@ -1,9 +1,9 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from marshmallow import fields from marshmallow import fields
from marshmallow_sqlalchemy import ModelSchema from marshmallow_sqlalchemy import ModelSchema
from model import Details from model import AIModel
class DetailsSchema(ModelSchema): class AIModelSchema(ModelSchema):
class Meta: class Meta:
model = Details model = AIModel

View File

@ -1,3 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from .require_decorators import json_required from .require_decorators import json_required
from .error_handlers import register_all_error_handlers from .error_handlers import register_all_error_handlers
from .storage import storage

View File

@ -0,0 +1,3 @@
from flask_minio import Minio
storage = Minio()

View File

@ -1,14 +1,15 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from flask import request, jsonify import tempfile
from flask import request, jsonify, current_app, abort, Response
from flask_classful import FlaskView, route from flask_classful import FlaskView, route
from model import db, Default, Details from model import db, Default, AIModel
from schema import DetailsSchema, DefaultSchema from minio.error import BucketAlreadyExists, BucketAlreadyOwnedByYou, ResponseError, NoSuchKey
from utils import json_required from schemas import AIModelSchema, DefaultSchema
from utils import json_required, storage
class ModelView(FlaskView): class ModelView(FlaskView):
aimodel_schema = AIModelSchema(many=False)
details_schema = DetailsSchema(many=False)
default_schema = DefaultSchema(many=False) default_schema = DefaultSchema(many=False)
@json_required @json_required
@ -16,24 +17,43 @@ class ModelView(FlaskView):
return jsonify({}) return jsonify({})
def get(self, _id: str): def get(self, _id: str):
return jsonify({"id": _id})
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'])
@route('<_id>/details') @route('<_id>/details')
def get_details(self, _id: str): def get_details(self, _id: str):
return jsonify({"id": _id})
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()
return jsonify(self.aimodel_schema.dump(m))
def delete(self, _id: str): def delete(self, _id: str):
return jsonify({"id": _id})
m = AIModel.query.fiter_by(id=_id).first_or_404()
storage.connection.remove_object(current_app.config['MINIO_BUCKET_NAME'], m.id)
db.session.delete(m)
db.session.commit()
return '', 204
@json_required @json_required
@route('$default', methods=['PUT']) @route('$default', methods=['PUT'])
def put_default(self): def put_default(self):
return jsonify({})
Default.query.delete()
#new_default = Default()
db.session.add(new_default)
db.session.commit()
return jsonify(self.default_schema.dump(new_default))

View File

@ -9,4 +9,11 @@ SQLAlchemy
marshmallow-sqlalchemy marshmallow-sqlalchemy
psycopg2-binary psycopg2-binary
flask_minio flask_minio
sentry-sdk sentry-sdk
pyAudioanalysis
numpy
eyed3
pydub
scipy
matplotlib