2024-12-11 16:18:12 +00:00
|
|
|
import os
|
2024-12-18 18:09:15 +00:00
|
|
|
import sys
|
2024-12-11 16:18:12 +00:00
|
|
|
import sqlalchemy.exc
|
|
|
|
from flask import Flask, request, jsonify
|
2024-12-16 18:38:24 +00:00
|
|
|
from routes import org_bp, user_bp, file_bp, role_bp
|
2024-12-11 16:18:12 +00:00
|
|
|
from database import db_connection, db
|
|
|
|
from models import Organization, User, File, Session
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///database.db"
|
|
|
|
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
|
|
|
|
app.config["SQLALCHEMY_AUTOCOMMIT"] = False
|
|
|
|
app.config["SQLALCHEMY_AUTOFLUSH"] = False
|
|
|
|
db_connection.init_app(app)
|
|
|
|
with app.app_context():
|
|
|
|
try:
|
|
|
|
db_connection.session.query(Session).delete()
|
|
|
|
db_connection.session.commit()
|
|
|
|
except sqlalchemy.exc.OperationalError:
|
|
|
|
pass
|
|
|
|
db_connection.create_all()
|
|
|
|
|
|
|
|
app.register_blueprint(org_bp, url_prefix="/org")
|
|
|
|
app.register_blueprint(user_bp, url_prefix="/user")
|
|
|
|
app.register_blueprint(file_bp, url_prefix="/file")
|
2024-12-16 18:38:24 +00:00
|
|
|
app.register_blueprint(role_bp, url_prefix="/role")
|
2024-12-11 16:18:12 +00:00
|
|
|
|
|
|
|
|
2024-12-18 18:09:15 +00:00
|
|
|
def reset_all():
|
|
|
|
with app.app_context():
|
|
|
|
db_connection.drop_all()
|
|
|
|
db_connection.create_all()
|
|
|
|
repos = os.path.join(os.path.dirname(os.path.abspath(__file__)), "repository")
|
|
|
|
for repo in os.listdir(repos):
|
|
|
|
if os.path.isdir(os.path.join(repos, repo)):
|
|
|
|
for file in os.listdir(os.path.join(repos, repo)):
|
|
|
|
os.remove(os.path.join(repos, repo, file))
|
|
|
|
os.rmdir(os.path.join(repos, repo))
|
|
|
|
|
|
|
|
|
2024-12-11 16:18:12 +00:00
|
|
|
@app.route("/", methods=["GET"])
|
|
|
|
def index():
|
|
|
|
return jsonify({"message": "Welcome to the API"}), 200
|
|
|
|
|
|
|
|
|
|
|
|
@app.route("/reset", methods=["POST"])
|
|
|
|
def reset():
|
2024-12-17 00:39:52 +00:00
|
|
|
password = request.json.get("password")
|
2024-12-11 16:18:12 +00:00
|
|
|
if password != "123":
|
|
|
|
return jsonify({"error": "Invalid password"}), 403
|
|
|
|
try:
|
2024-12-18 18:09:15 +00:00
|
|
|
reset_all()
|
2024-12-11 16:18:12 +00:00
|
|
|
except sqlalchemy.exc.OperationalError:
|
|
|
|
return jsonify({"error": "Database error"}), 500
|
|
|
|
return jsonify({"message": "Database reset"}), 200
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2024-12-18 18:09:15 +00:00
|
|
|
args = sys.argv[1:]
|
|
|
|
for arg in args:
|
|
|
|
if arg == "--reset":
|
|
|
|
try:
|
|
|
|
reset_all()
|
|
|
|
except sqlalchemy.exc.OperationalError:
|
|
|
|
print("Database error")
|
|
|
|
sys.exit(1)
|
|
|
|
break
|
2024-12-11 16:18:12 +00:00
|
|
|
app.run(debug=True)
|