TG-103 Initial Flask app stucture
This commit is contained in:
18
backend/app/api/exampleapi/controller.py
Normal file
18
backend/app/api/exampleapi/controller.py
Normal file
@ -0,0 +1,18 @@
|
||||
from flask_restful import Resource
|
||||
|
||||
|
||||
class SomeApi(Resource):
|
||||
"""
|
||||
Some Api Resource
|
||||
"""
|
||||
def post(self):
|
||||
return {'somepost': 'somepostdata'}, 201
|
||||
|
||||
def get(self, id=None):
|
||||
return {'someget': 'somegetdata'}, 200
|
||||
|
||||
def delete(self, id=None):
|
||||
return {'somedelete': 'somedeletedata'}, 204
|
||||
|
||||
def put(self, id=None):
|
||||
return {'someput': 'someputdata'}, 204
|
33
backend/app/api/exampleapi/model.py
Normal file
33
backend/app/api/exampleapi/model.py
Normal file
@ -0,0 +1,33 @@
|
||||
from passlib.apps import custom_app_context as pwd_context
|
||||
|
||||
from app.core import db
|
||||
|
||||
users_roles = db.Table('users_roles',
|
||||
db.Column('user_id',
|
||||
db.Integer,
|
||||
db.ForeignKey('user.id')
|
||||
),
|
||||
db.Column('role_id',
|
||||
db.Integer,
|
||||
db.ForeignKey('role.id')
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class Role(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
name = db.Column(db.String(80), unique=True, nullable=False)
|
||||
description = db.Column(db.String(255), nullable=False)
|
||||
|
||||
|
||||
class User(db.Model):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
email = db.Column(db.String(255), index=True, unique=True, nullable=False)
|
||||
password_hash = db.Column(db.String(64), nullable=False)
|
||||
roles = db.relationship('Role', secondary=users_roles)
|
||||
|
||||
def hash_password(self, password):
|
||||
self.password_hash = pwd_context.encrypt(password)
|
||||
|
||||
def verify_password(self, password):
|
||||
return pwd_context.verify(password, self.password_hash)
|
31
backend/app/config.py
Normal file
31
backend/app/config.py
Normal file
@ -0,0 +1,31 @@
|
||||
import os
|
||||
|
||||
|
||||
class Config:
|
||||
DEBUG = False
|
||||
TESTING = False
|
||||
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
|
||||
SQLALCHEMY_DATABASE_URI = \
|
||||
'sqlite:///' + os.path.join(BASE_DIR, '../app.db')
|
||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||
THREADS_PER_PAGE = 2
|
||||
SECRET_KEY = "secret"
|
||||
BUNDLE_ERRORS = True
|
||||
SESSION_COOKIE_SECURE = True
|
||||
SESSION_VALIDITY_DURATION_WITHOUT_ACTIVITY_MIN = 20
|
||||
|
||||
|
||||
class Prod(Config):
|
||||
SQLALCHEMY_DATABASE_URI = 'mysql://user@localhost/foo'
|
||||
|
||||
|
||||
class Debug(Config):
|
||||
DEBUG = True
|
||||
SESSION_COOKIE_SECURE = False
|
||||
|
||||
|
||||
class Test(Config):
|
||||
TESTING = True
|
||||
BASE_DIR = os.path.abspath(os.path.dirname(__file__))
|
||||
SQLALCHEMY_DATABASE_URI = \
|
||||
'sqlite:///' + os.path.join(BASE_DIR, '../test.db')
|
40
backend/app/core.py
Normal file
40
backend/app/core.py
Normal file
@ -0,0 +1,40 @@
|
||||
from datetime import timedelta
|
||||
|
||||
from flask import Flask, session
|
||||
from flask_restful import Api
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
|
||||
import importlib
|
||||
|
||||
|
||||
def configure_app(config="prod"):
|
||||
if config.lower() == "debug":
|
||||
app.config.from_object('app.config.Debug')
|
||||
elif config.lower() == "test":
|
||||
app.config.from_object('app.config.Test')
|
||||
else:
|
||||
app.config.from_object('app.config.Prod')
|
||||
|
||||
app.permanent_session_lifetime = \
|
||||
timedelta(
|
||||
minutes=app.config
|
||||
['SESSION_VALIDITY_DURATION_WITHOUT_ACTIVITY_MIN']
|
||||
)
|
||||
|
||||
@app.before_request
|
||||
def before_request():
|
||||
session.modified = True
|
||||
|
||||
|
||||
# initialization Flask
|
||||
app = Flask(__name__)
|
||||
configure_app()
|
||||
|
||||
# SQLAlchemy
|
||||
db = SQLAlchemy(app)
|
||||
|
||||
# RestFul Flask
|
||||
api = Api(app)
|
||||
|
||||
# import api resources
|
||||
importlib.import_module("app.urls")
|
4
backend/app/urls.py
Normal file
4
backend/app/urls.py
Normal file
@ -0,0 +1,4 @@
|
||||
from app.core import api
|
||||
|
||||
# Some Api resource
|
||||
api.add_resource(api, '/api/someapi', '/api/someapi/<int:id>')
|
Reference in New Issue
Block a user