From 4d1de2a1f6a8a016037520803e46cd1ed6a38bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torma=20Krist=C3=B3f?= Date: Wed, 25 Mar 2020 02:54:59 +0100 Subject: [PATCH] add orm --- requirements.txt | 4 +++- src/app.py | 5 +++++ src/db.py | 13 +++++++++++++ src/models.py | 25 +++++++++++++++++++++++++ src/resources.py | 5 +++++ 5 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/db.py create mode 100644 src/models.py diff --git a/requirements.txt b/requirements.txt index 9df3fb8..3f27ce6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,6 @@ Flask Flask-RESTful requests werkzeug -filetype \ No newline at end of file +filetype +sqlalchemy +flask_sqlalchemy \ No newline at end of file diff --git a/src/app.py b/src/app.py index 92b7430..ce207a7 100644 --- a/src/app.py +++ b/src/app.py @@ -6,6 +6,7 @@ from flask_restful import Api from sentry_sdk.integrations.flask import FlaskIntegration from config import SENTRY_DSN, RELEASE_ID, RELEASEMODE, PORT, DEBUG +import db from resources import * """ @@ -28,6 +29,10 @@ if SENTRY_DSN: app = Flask(__name__) api = Api(app) +db.init_app(app) + +with app.app_context(): + db.create_all() formatter = logging.Formatter( fmt="%(asctime)s - %(levelname)s - %(module)s - %(message)s" diff --git a/src/db.py b/src/db.py new file mode 100644 index 0000000..acfa6d2 --- /dev/null +++ b/src/db.py @@ -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() diff --git a/src/models.py b/src/models.py new file mode 100644 index 0000000..8f7df0d --- /dev/null +++ b/src/models.py @@ -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) diff --git a/src/resources.py b/src/resources.py index ceeac1f..80092f4 100644 --- a/src/resources.py +++ b/src/resources.py @@ -5,6 +5,8 @@ from flask import request from flask_restful import Resource, reqparse from werkzeug.datastructures import FileStorage import filetype +import db +from models import SampleMetadata from schemas import * """ @@ -54,3 +56,6 @@ class SampleResource(Resource): "Input JSON did not conform to schema. It was: {}", desc) return {'status': 'error', '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