ciclos

commit 2b1df58a7df5d51f3a919c40bae5312492dadd4f

Author: Pedro Lucas Porcellis <pedrolucasporcellis@gmail.com>

Add new declarative database and config rig up

 app.py | 5 +++++
 core/app.py | 35 +++++++++++++++++++++++++++++++++++
 core/config.py | 8 ++++++++
 core/database.py | 33 +++++++++++++++++++++++++++++++++


diff --git a/app.py b/app.py
new file mode 100644
index 0000000000000000000000000000000000000000..0667a45e288a5f669b249ed64a32eddfa530478d
--- /dev/null
+++ b/app.py
@@ -0,0 +1,5 @@
+from core.app import app
+from core.config import read_from_config
+
+if __name__ == '__main__':
+    app.run()




diff --git a/core/__init__.py b/core/__init__.py
new file mode 100644
index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391
Binary files /dev/null and b/core/__init__.py differ




diff --git a/core/app.py b/core/app.py
new file mode 100644
index 0000000000000000000000000000000000000000..6d8d9e63ebeb930adedba49166da38ba5ee8368a
--- /dev/null
+++ b/core/app.py
@@ -0,0 +1,35 @@
+from flask import Flask, render_template
+from flask_login import LoginManager
+
+from core.config import read_from_config
+from core.database import db, init_database
+
+app = Flask(__name__)
+app.secret_key = read_from_config("secret-key")
+
+init_database()
+
+try:
+    locale.setlocale(locale.LC_ALL, read_from_config("locale"))
+except:
+    pass
+
+login_manager = LoginManager()
+login_manager.login_view = 'auth.login'
+login_manager.init_app(app)
+
+from .types.user import User
+
+@login_manager.user_loader
+def user_loader(user_id):
+    return User.query.get(user_id)
+
+from .blueprints.basic import basic
+from .blueprints.auth import auth
+
+app.register_blueprint(basic)
+app.register_blueprint(auth)
+
+@app.errorhandler(404)
+def handle_404(e):
+    return render_template("not_found.html"), 404




diff --git a/core/config.py b/core/config.py
new file mode 100644
index 0000000000000000000000000000000000000000..78151a860971e503d46a40ad5ad7d3dbb0a9b73b
--- /dev/null
+++ b/core/config.py
@@ -0,0 +1,8 @@
+from configparser import ConfigParser
+
+config = ConfigParser()
+config.read_file(open('config.ini'))
+
+env = config['meta']['environment'] or 'dev'
+
+read_from_config = lambda key: config.get(env, key)




diff --git a/core/database.py b/core/database.py
new file mode 100644
index 0000000000000000000000000000000000000000..07e1ab555f550d69cc53933312edd79cc8863ace
--- /dev/null
+++ b/core/database.py
@@ -0,0 +1,33 @@
+from sqlalchemy import create_engine, event
+from sqlalchemy.orm import sessionmaker, scoped_session
+from sqlalchemy.ext.declarative import declarative_base
+
+from datetime import datetime
+
+from .config import read_from_config
+
+engine = create_engine(read_from_config('connection-string'), echo=True)
+db = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
+
+Base = declarative_base()
+Base.query = db.query_property()
+
+def init_database():
+    @event.listens_for(Base, 'before_insert', propagate=True)
+    def before_insert(mapper, connection, target):
+        if hasattr(target, '_no_autoupdate'):
+            return
+        if hasattr(target, 'created_at'):
+            target.created_at = datetime.utcnow()
+        if hasattr(target, 'updated_at'):
+            target.updated_at = datetime.utcnow()
+
+    @event.listens_for(Base, 'before_update', propagate=True)
+    def before_insert(mapper, connection, target):
+        if hasattr(target, '_no_autoupdate'):
+            return
+        if hasattr(target, 'updated_at'):
+            target.updated_at = datetime.utcnow()
+
+    import core.types
+    Base.metadata.create_all(bind=engine)