-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.py
102 lines (75 loc) · 3.09 KB
/
init.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from flask import Flask, redirect, url_for, render_template, request, jsonify
import requests
from models.init import *
from flask_cors import CORS
# -------------APP CONFIGURATION----------------#
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
app.config['secret_key'] = 'secret_key'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
CORS(app, origins="*", methods=["GET", "POST",
"PUT", "DELETE", "PATCH"], allow_headers="*")
with app.app_context():
db.init_app(app)
# -------------BluePrints-----------------------#
with app.app_context():
from routes.doctors.views import doctors_blueprint
from routes.clinicitems.views import clinicitems_blueprint
from routes.sercretaries.views import secretaries_blueprint
from routes.patients.views import patients_blueprint
from routes.appointments.views import appointments_blueprint
from routes.records.views import records_blueprint
from routes.auth.views import auth_blueprint
from routes.reports.views import reports_blueprint
app.register_blueprint(secretaries_blueprint, url_prefix='/secretaries')
app.register_blueprint(doctors_blueprint, url_prefix='/doctors')
app.register_blueprint(clinicitems_blueprint, url_prefix='/clinicitems')
app.register_blueprint(patients_blueprint, url_prefix='/patients')
app.register_blueprint(appointments_blueprint, url_prefix='/appointments')
app.register_blueprint(records_blueprint, url_prefix='/records')
app.register_blueprint(auth_blueprint, url_prefix='/auth')
app.register_blueprint(reports_blueprint, url_prefix='/reports')
# -----------------------------------------------#
# this endpoint avoids errors that arise when the database
# in concurrently created by multiple gunicorn workers in build
# time and it should be removed in production
class DatabaseConnection:
__instance = None
@staticmethod
def get_instance():
if DatabaseConnection.__instance is None:
DatabaseConnection()
return DatabaseConnection.__instance
def __init__(self):
if DatabaseConnection.__instance is not None:
raise Exception("DatabaseConnection class is a Singleton! Use get_instance() method instead.")
else:
DatabaseConnection.__instance = self
self.db = SQLAlchemy()
def create_all(self):
self.db.create_all()
import db_initialization_script
db = DatabaseConnection.get_instance().db
@app.route('/init')
def init():
db.drop_all()
try:
db.engine.execute(
"SELECT 'drop table ' || name || ';' FROM sqlite_master WHERE type = 'table';").fetchall()
except:
pass
db.create_all()
import db_initialization_script
return jsonify({
'success': True
}), 200
@app.route('/')
def index():
doctors = Doctor.query.all()
secretaries = Secretary.query.all()
return jsonify({
'data': [d.format() for d in doctors] + [s.format() for s in secretaries]
}), 200
if __name__ == '__main__':
# DEBUG is SET to TRUE. CHANGE FOR PROD
app.run(port=5000, debug=True)