add orm
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Torma Kristóf 2020-03-25 02:54:59 +01:00
parent 7d7227aa47
commit 4d1de2a1f6
Signed by: tormakris
GPG Key ID: DC83C4F2C41B1047
5 changed files with 51 additions and 1 deletions

View File

@ -6,3 +6,5 @@ Flask-RESTful
requests requests
werkzeug werkzeug
filetype filetype
sqlalchemy
flask_sqlalchemy

View File

@ -6,6 +6,7 @@ from flask_restful import Api
from sentry_sdk.integrations.flask import FlaskIntegration from sentry_sdk.integrations.flask import FlaskIntegration
from config import SENTRY_DSN, RELEASE_ID, RELEASEMODE, PORT, DEBUG from config import SENTRY_DSN, RELEASE_ID, RELEASEMODE, PORT, DEBUG
import db
from resources import * from resources import *
""" """
@ -28,6 +29,10 @@ if SENTRY_DSN:
app = Flask(__name__) app = Flask(__name__)
api = Api(app) api = Api(app)
db.init_app(app)
with app.app_context():
db.create_all()
formatter = logging.Formatter( formatter = logging.Formatter(
fmt="%(asctime)s - %(levelname)s - %(module)s - %(message)s" fmt="%(asctime)s - %(levelname)s - %(module)s - %(message)s"

13
src/db.py Normal file
View File

@ -0,0 +1,13 @@
#!/usr/bin/env python3
from flask_sqlalchemy import SQLAlchemy
"""
Flask Restful endpoints
"""
__author__ = '@tormakris'
__copyright__ = "Copyright 2020, Birbnetes Team"
__module_name__ = "db"
__version__text__ = "1"
db = SQLAlchemy()

25
src/models.py Normal file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env python3
import db
from sqlalchemy.sql import func
"""
Flask Restful endpoints
"""
__author__ = '@tormakris'
__copyright__ = "Copyright 2020, Birbnetes Team"
__module_name__ = "models"
__version__text__ = "1"
class SampleMetadata(db.Model):
"""
SQLAlchemy model of metadata entries
"""
id = db.Column(db.Integer, primary_key=True, auto_increment=True)
timestamp = db.Column(db.TIMESTAMP, nullable=False, server_default=func.now())
device_id = db.Column(db.String, nullable=False)
device_date = db.Column(db.DateTime, nullable=False)
tag = db.Column(db.String, nullable=False)

View File

@ -5,6 +5,8 @@ from flask import request
from flask_restful import Resource, reqparse from flask_restful import Resource, reqparse
from werkzeug.datastructures import FileStorage from werkzeug.datastructures import FileStorage
import filetype import filetype
import db
from models import SampleMetadata
from schemas import * from schemas import *
""" """
@ -54,3 +56,6 @@ class SampleResource(Resource):
"Input JSON did not conform to schema. It was: {}", desc) "Input JSON did not conform to schema. It was: {}", desc)
return {'status': 'error', return {'status': 'error',
'message': 'Input JSON schema invalid'}, 417 'message': 'Input JSON schema invalid'}, 417
# TODO: Preserve input data in relational DB
# TODO: Pass data and tag to ObjectStorage
# TODO: Pass tag to MQ