-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
65 lines (50 loc) · 1.96 KB
/
Copy path__init__.py
File metadata and controls
65 lines (50 loc) · 1.96 KB
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
import os
from flask import Flask, render_template
from db import conn
from glob import glob
from blueprints.hotels import hotels_bp
from blueprints.cities import cities_bp
from blueprints.offices import offices_bp
from blueprints.tours import tours_bp
from blueprints.companies import companies_bp
from blueprints.prices import prices_bp
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key-123'
app.register_blueprint(hotels_bp)
app.register_blueprint(cities_bp)
app.register_blueprint(offices_bp)
app.register_blueprint(tours_bp)
app.register_blueprint(companies_bp)
app.register_blueprint(prices_bp)
@app.route('/')
def index():
return render_template('base.html')
check_and_initialize_db()
return app
def check_and_initialize_db():
with conn.cursor() as cur:
cur.execute("""
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_name = 'hotels'
)
""")
exists = cur.fetchone()[0]
if not exists:
print("Таблицы не найдены, создаём и заполняем данными...")
run_sql_file(os.path.join(os.path.dirname(__file__), 'sql_scripts', 'tables', 'create_tables.sql'))
run_sql_file(os.path.join(os.path.dirname(__file__), 'sql_scripts', 'tables', 'insert_data.sql'))
run_sql_scripts(os.path.join(os.path.dirname(__file__), 'sql_scripts'))
print("Инициализация завершена.")
def run_sql_file(path):
with open(path, 'r', encoding='utf-8') as f, conn.cursor() as cur:
sql = f.read()
cur.execute(sql)
conn.commit()
def run_sql_scripts(folder):
for subdir in ['triggers', 'procedure', 'view_functions']:
files = sorted(glob(os.path.join(folder, subdir, '*.sql')))
for file in files:
print(f"Выполняем {file} ...")
run_sql_file(file)