-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
184 lines (157 loc) · 5.72 KB
/
app.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import importlib
import json
import os
import sys
from flask import Flask
# from flask import redirect
from flask import url_for
from flask_login import current_user
from flask_wtf.csrf import CSRFProtect
sys.path.append(".")
import jinja2
from flask_uploads import configure_uploads
from config import app_config
from shopyoapi.enhance import get_setting
from shopyoapi.init import categoryphotos
from shopyoapi.init import db
from shopyoapi.init import login_manager
from shopyoapi.init import ma
from shopyoapi.init import migrate
from shopyoapi.init import productphotos
from shopyoapi.init import subcategoryphotos
from shopyoapi.init import idcardphotos
from shopyoapi.init import brnphotos
from shopyoapi.init import addressphotos
from shopyoapi.init import storelogophotos
from shopyoapi.init import storebannerphotos
base_path = os.path.dirname(os.path.abspath(__file__))
def create_app(config_name):
app = Flask(__name__)
configuration = app_config[config_name]
app.config.from_object(configuration)
migrate.init_app(app, db)
db.init_app(app)
ma.init_app(app)
login_manager.init_app(app)
csrf = CSRFProtect(app) # noqa
configure_uploads(app, categoryphotos)
configure_uploads(app, subcategoryphotos)
configure_uploads(app, productphotos)
configure_uploads(app, idcardphotos)
configure_uploads(app, brnphotos)
configure_uploads(app, addressphotos)
configure_uploads(app, storelogophotos)
configure_uploads(app, storebannerphotos)
available_everywhere_entities = {}
#
# load blueprints
#
for folder in os.listdir(os.path.join(base_path, "modules")):
if folder.startswith("__"): # ignore __pycache__
continue
if folder.startswith("box__"):
# boxes
for sub_folder in os.listdir(
os.path.join(base_path, "modules", folder)
):
if sub_folder.startswith("__"): # ignore __pycache__
continue
elif sub_folder.endswith(".json"): # box_info.json
continue
sys_mod = importlib.import_module(
"modules.{}.{}.view".format(folder, sub_folder)
)
try:
mod_global = importlib.import_module(
"modules.{}.{}.global".format(folder, sub_folder)
)
available_everywhere_entities.update(
mod_global.available_everywhere
)
except ImportError as e:
print(e)
pass
app.register_blueprint(
getattr(sys_mod, "{}_blueprint".format(sub_folder))
)
else:
# apps
mod = importlib.import_module("modules.{}.view".format(folder))
try:
mod_global = importlib.import_module(
"modules.{}.global".format(folder)
)
available_everywhere_entities.update(
mod_global.available_everywhere
)
except ImportError as e:
print(e)
pass
app.register_blueprint(getattr(mod, "{}_blueprint".format(folder)))
#
# custom templates folder
#
with app.app_context():
front_theme_dir = os.path.join(
app.config["BASE_DIR"], "static", "themes", "front"
)
back_theme_dir = os.path.join(
app.config["BASE_DIR"], "static", "themes", "back"
)
my_loader = jinja2.ChoiceLoader(
[
app.jinja_loader,
jinja2.FileSystemLoader([front_theme_dir, back_theme_dir]),
]
)
app.jinja_loader = my_loader
#
# global vars
#
@app.context_processor
def inject_global_vars():
# theme_dir = os.path.join(
# app.config["BASE_DIR"], "themes", get_setting("ACTIVE_FRONT_THEME")
# )
# info_path = os.path.join(theme_dir, "info.json")
# with open(info_path) as f:
# info_data = json.load(f)
APP_NAME = get_setting("APP_NAME")
SECTION_NAME = get_setting("SECTION_NAME")
SECTION_ITEMS = get_setting("SECTION_ITEMS")
# ACTIVE_FRONT_THEME = get_setting("ACTIVE_FRONT_THEME")
# ACTIVE_FRONT_THEME_VERSION = info_data["version"]
# ACTIVE_FRONT_THEME_STYLES_URL = url_for(
# "resource.active_theme_css",
# active_theme=ACTIVE_FRONT_THEME,
# v=ACTIVE_FRONT_THEME_VERSION,
# )
base_context = {
"APP_NAME": APP_NAME,
"SECTION_NAME": SECTION_NAME,
"SECTION_ITEMS": SECTION_ITEMS,
# "ACTIVE_FRONT_THEME": ACTIVE_FRONT_THEME,
# "ACTIVE_FRONT_THEME_VERSION": ACTIVE_FRONT_THEME_VERSION,
# "ACTIVE_FRONT_THEME_STYLES_URL": ACTIVE_FRONT_THEME_STYLES_URL,
"len": len,
"current_user": current_user,
}
base_context.update(available_everywhere_entities)
# print('\nav everywhere entities\n', available_everywhere_entities)
return base_context
# end of func
return app
# app.jinja_env.globals.update(x=x)
# if app.config["DEBUG"]:
# @app.after_request
# def after_request(response):
# response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0"
# response.headers["Expires"] = 0
# response.headers["Pragma"] = "no-cache"
# return response
with open(os.path.join(base_path, 'config.json')) as f:
config_json = json.load(f)
environment = config_json['environment']
app = create_app(environment)
if __name__ == "__main__":
app.run(debug=False, host="0.0.0.0")