Update
This commit is contained in:
parent
e371b7aabc
commit
be5bfa1fb5
10 changed files with 115 additions and 51 deletions
6
backend/app.py
Normal file
6
backend/app.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from application import create_app
|
||||
|
||||
app = create_app()
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run(host='0.0.0.0')
|
||||
31
backend/application/__init__.py
Normal file
31
backend/application/__init__.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
from flask import Flask
|
||||
from flask_sqlalchemy import SQLAlchemy
|
||||
from ddtrace import patch_all
|
||||
import sys
|
||||
import os
|
||||
|
||||
db = SQLAlchemy()
|
||||
patch_all()
|
||||
|
||||
|
||||
def create_app():
|
||||
app = Flask(__name__)
|
||||
FLASK_ENV = os.environ.get('FLASK_ENV', None)
|
||||
if FLASK_ENV == 'production':
|
||||
app.config.from_object("config.ProductionConfig")
|
||||
elif FLASK_ENV == 'staging':
|
||||
app.config.from_object("config.StagingConfig")
|
||||
elif FLASK_ENV == 'development':
|
||||
app.config.from_object("config.DevelopmentConfig")
|
||||
else:
|
||||
app.config.from_object("config.Config")
|
||||
|
||||
if app.config['SQLALCHEMY_DATABASE_URI_1'] is None or app.config['SQLALCHEMY_DATABASE_URI_2'] is None:
|
||||
print('No ENV Variable for DATABASE_URL_1 or DATABASE_URL_2')
|
||||
sys.exit(1)
|
||||
|
||||
db.init_app(app)
|
||||
with app.app_context():
|
||||
from . import routes
|
||||
db.create_all()
|
||||
return app
|
||||
32
backend/application/logs_model.py
Normal file
32
backend/application/logs_model.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
from . import db
|
||||
|
||||
|
||||
class Logs(db.Model):
|
||||
__bind_key__ = 'logs'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
date = db.Column(db.Date())
|
||||
user = db.Column(db.String())
|
||||
ip = db.Column(db.String())
|
||||
table = db.Column(db.String())
|
||||
action = db.Column(db.String())
|
||||
status = db.Column(db.String())
|
||||
|
||||
def __init__(self, date, user, ip, table, action, status):
|
||||
self.date = date
|
||||
self.user = user
|
||||
self.ip = ip
|
||||
self.table = table
|
||||
self.action = action
|
||||
self.status = status
|
||||
|
||||
def __repr__(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'date': self.date,
|
||||
'user': self.user,
|
||||
'ip': self.ip,
|
||||
'table': self.table,
|
||||
'action': self.action,
|
||||
'status': self.status
|
||||
}
|
||||
31
backend/application/responses.py
Normal file
31
backend/application/responses.py
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
from flask import current_app as app
|
||||
import json
|
||||
|
||||
|
||||
def send_error(status_code, message):
|
||||
data_json = {
|
||||
'status': 'error',
|
||||
'message': message
|
||||
}
|
||||
res = app.response_class(
|
||||
response=json.dumps(data_json, sort_keys=True),
|
||||
status=status_code,
|
||||
mimetype='application/json'
|
||||
)
|
||||
res.headers['Access-Control-Allow-Origin'] = '*'
|
||||
return res
|
||||
|
||||
|
||||
def send_message(message, data):
|
||||
data_json = {
|
||||
'status': 'success',
|
||||
'message': message,
|
||||
'data': data
|
||||
}
|
||||
res = app.response_class(
|
||||
response=json.dumps(data_json, sort_keys=True),
|
||||
status=200,
|
||||
mimetype='application/json'
|
||||
)
|
||||
res.headers['Access-Control-Allow-Origin'] = '*'
|
||||
return res
|
||||
93
backend/application/routes.py
Normal file
93
backend/application/routes.py
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
from flask import current_app as app
|
||||
from flask import request
|
||||
from .logs_model import Logs, db
|
||||
from .users_model import Users, db
|
||||
from .responses import send_message, send_error
|
||||
|
||||
|
||||
# Login
|
||||
@app.route('/api/login', methods=['POST'])
|
||||
def login():
|
||||
return send_message('Login not implemented', None)
|
||||
|
||||
|
||||
# Register
|
||||
@app.route('/api/register', methods=['POST'])
|
||||
def register():
|
||||
post_email = str(request.form['email'])
|
||||
post_login = str(request.form['login'])
|
||||
post_hashPass = str(request.form['hashPass'])
|
||||
post_role = str(request.form['role'])
|
||||
|
||||
if post_email and post_login and post_hashPass and post_role:
|
||||
user = Users.query.filter(
|
||||
Users.email == post_email or Users.login == post_login
|
||||
).first()
|
||||
if user:
|
||||
return send_message(f"{post_email} ({post_login}) already exist.", None)
|
||||
user = Users(
|
||||
email=post_email,
|
||||
login=post_login,
|
||||
hashPass=post_hashPass,
|
||||
role=post_role
|
||||
)
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
return send_message('User registered.', user)
|
||||
|
||||
else:
|
||||
return send_error(400, 'POST Request Error : Need email, login, hashPass and role fields.')
|
||||
|
||||
|
||||
# Logout
|
||||
@app.route('/api/logout', methods=['POST'])
|
||||
def logout():
|
||||
return send_message('Logout not implemented', None)
|
||||
|
||||
|
||||
# Update User
|
||||
@app.route('/api/user/update', methods=['PUT'])
|
||||
def user_update():
|
||||
return send_message('User.update not implemented', None)
|
||||
|
||||
|
||||
# Delete User
|
||||
@app.route('/api/user/delete', methods=['DELETE'])
|
||||
def user_delete():
|
||||
return send_message('User.delete not implemented', None)
|
||||
|
||||
|
||||
# Admin : Create User
|
||||
@app.route('/api/user/create', methods=['POST'])
|
||||
def user_create():
|
||||
return send_message('User.create not implemented', None)
|
||||
|
||||
|
||||
# Admin : Change User password
|
||||
@app.route('/api/admin/update/user/password', methods=['PUT'])
|
||||
def admin_update_user_pwd():
|
||||
return send_message('Admin.update.user.password not implemented', None)
|
||||
|
||||
|
||||
# Admin : Change User role
|
||||
@app.route('/api/admin/update/user/role', methods=['PUT'])
|
||||
def admin_update_user_role():
|
||||
return send_message('Admin.update.user.role not implemented', None)
|
||||
|
||||
|
||||
# Admin : Delete User
|
||||
@app.route('/api/admin/delete/user', methods=['DELETE'])
|
||||
def admin_delete_user():
|
||||
return send_message('Admin.delete.user not implemented', None)
|
||||
|
||||
|
||||
# List of User (must be authenticated)
|
||||
@app.route('/api/users', methods=['GET'])
|
||||
def users():
|
||||
return send_message('Users not implemented', None)
|
||||
|
||||
|
||||
# Search User
|
||||
@app.route('/api/users/search', methods=['POST'])
|
||||
def users_search():
|
||||
return send_message('Users.search not implemented', None)
|
||||
26
backend/application/users_model.py
Normal file
26
backend/application/users_model.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
from . import db
|
||||
|
||||
|
||||
class Users(db.Model):
|
||||
__bind_key__ = 'users'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
email = db.Column(db.String())
|
||||
login = db.Column(db.String())
|
||||
hashPass = db.Column(db.String())
|
||||
role = db.Column(db.String())
|
||||
|
||||
def __init__(self, email, login, hash_pass, role):
|
||||
self.email = email
|
||||
self.login = login
|
||||
self.hashPass = hash_pass
|
||||
self.role = role
|
||||
|
||||
def __repr__(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'email': self.email,
|
||||
'login': self.login,
|
||||
'hashPass': self.hashPass,
|
||||
'role': self.role
|
||||
}
|
||||
36
backend/config.py
Normal file
36
backend/config.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import os
|
||||
|
||||
basedir = os.path.abspath(os.path.dirname(__file__))
|
||||
|
||||
|
||||
class Config(object):
|
||||
DEBUG = False
|
||||
TESTING = False
|
||||
CSRF_ENABLED = True
|
||||
|
||||
PORT = int(os.environ.get('PORT', 33507))
|
||||
SECRET_KEY = os.environ.get('SECRET_KEY', None)
|
||||
FLASK_APP = os.environ.get('FLASK_APP', None)
|
||||
FLASK_ENV = os.environ.get('FLASK_ENV', None)
|
||||
|
||||
SQLALCHEMY_ECHO = False
|
||||
SQLALCHEMY_TRACK_MODIFICATIONS = False
|
||||
SQLALCHEMY_DATABASE_URI_1 = os.environ.get('DATABASE_URL_1', None)
|
||||
SQLALCHEMY_DATABASE_URI_2 = os.environ.get('DATABASE_URL_2', None)
|
||||
SQLALCHEMY_BINDS = {
|
||||
'users': SQLALCHEMY_DATABASE_URI_1,
|
||||
'logs': SQLALCHEMY_DATABASE_URI_2
|
||||
}
|
||||
|
||||
|
||||
class ProductionConfig(Config):
|
||||
DEBUG = False
|
||||
|
||||
|
||||
class StagingConfig(Config):
|
||||
STAGING = True
|
||||
|
||||
|
||||
class DevelopmentConfig(Config):
|
||||
DEVELOPMENT = True
|
||||
DEBUG = True
|
||||
14
backend/manage.py
Normal file
14
backend/manage.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
from flask_script import Manager
|
||||
from flask_migrate import Migrate, MigrateCommand
|
||||
|
||||
from flask import current_app as app
|
||||
from . import db
|
||||
|
||||
migrate = Migrate(app, db)
|
||||
manager = Manager(app)
|
||||
|
||||
manager.add_command('db', MigrateCommand)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
manager.run()
|
||||
5
backend/requirements.txt
Normal file
5
backend/requirements.txt
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Flask_Migrate==3.1.0
|
||||
ddtrace==0.56.1
|
||||
Flask_SQLAlchemy==2.5.1
|
||||
Flask_Script==2.0.6
|
||||
Flask==2.0.2
|
||||
Loading…
Add table
Add a link
Reference in a new issue