/
app.py
181 lines (137 loc) · 5.36 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
# coding: utf-8
import os
from datetime import timedelta
from flask import Flask, render_template
from flask.ext.triangle import Triangle
from assets import assets
from werkzeug.contrib.fixers import ProxyFix
from db import db
from log import init_logging, debug
from config import admin_imap, admin_pass, DATABASE_URI, SECRET_KEY, IS_PROD, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, FLASK_ASSETS_USE_S3
__author__ = 'StasEvseev'
os.environ['TZ'] = 'Europe/Moscow'
def create_app():
if IS_PROD:
app = Flask(__name__)
else:
app = Flask(__name__)
app.config.update(
CELERY_BROKER_URL='redis://localhost:6379/0',
CELERY_RESULT_BACKEND='redis://localhost:6379/0'
)
app.config['MAIL_SERVER'] = 'smtp.gmail.com'
app.config['MAIL_PORT'] = 587
app.config['MAIL_USE_TLS'] = True
app.config['MAIL_USERNAME'] = admin_imap
app.config['MAIL_PASSWORD'] = admin_pass
app.config['MAIL_DEFAULT_SENDER'] = 'server-error@example.com'
return app
def init_app(application):
from resources import api
from assets import assets
from applications.security.auth import login_manager
from security import security
application.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URI
application.config['SECURITY_TOKEN_AUTHENTICATION_HEADER'] = (
"AUTHORIZATION")
application.config['SECURITY_REMEMBER_SALT'] = "SALT123123123"
# application.config['SQLALCHEMY_ECHO'] = True
application.config['SECRET_KEY'] = SECRET_KEY
application.config['FLASK_ASSETS_USE_S3'] = FLASK_ASSETS_USE_S3
application.config['FLASKS3_BUCKET_NAME'] = os.environ.get('FLASKS3_BUCKET_NAME', 'evfam.com')
application.config['FLASKS3_FORCE_MIMETYPE'] = True
application.config['FLASKS3_ONLY_MODIFIED'] = True
application.config['AWS_ACCESS_KEY_ID'] = AWS_ACCESS_KEY_ID
application.config['AWS_SECRET_ACCESS_KEY'] = AWS_SECRET_ACCESS_KEY
application.permanent_session_lifetime = timedelta(minutes=30)
Triangle(application)
assets.init_app(application)
api.init_app(application)
api.application = application
db.init_app(application)
login_manager.init_app(application)
security.init_app(application)
application.db = db
application.api = api
if IS_PROD:
init_logging(application)
with application.app_context():
assets.debug = False
assets.auto_build = False
application.config['ASSETS_DEBUG'] = False
return application
def initialize_blueprints(app):
from applications.commodity import blueprint as ComBl
from applications.point_sale import blueprint as PSBl
from applications.inventory import blueprint as InvBl
from applications.provider_app import blueprint as PrBl
from applications.waybill import blueprint as WbBl
from applications.acceptance import blueprint as AcBl
from applications.receiver import blueprint as RcBl
from applications.good import blueprint as GdBl
from applications.invoice import blueprint as InBl
from applications.price import blueprint as PriceBl
from applications.settings import blueprint as SetBl
from applications.order import blueprint as OrBl
from applications.return_app import blueprint as ReBl
from applications.waybill_return import blueprint as WRBl
from applications.seller import blueprint as SelBl
from applications.collection import blueprint as ColBl
from applications.security.bl import blueprint as UsBl
app.register_blueprint(ComBl)
app.register_blueprint(PSBl)
app.register_blueprint(InvBl)
app.register_blueprint(PrBl)
app.register_blueprint(WbBl)
app.register_blueprint(AcBl)
app.register_blueprint(RcBl)
app.register_blueprint(GdBl)
app.register_blueprint(InBl)
app.register_blueprint(PriceBl)
app.register_blueprint(SetBl)
app.register_blueprint(OrBl)
app.register_blueprint(ReBl)
app.register_blueprint(WRBl)
app.register_blueprint(SelBl)
app.register_blueprint(ColBl)
app.register_blueprint(UsBl)
app.wsgi_app = ProxyFix(app.wsgi_app)
return app
app = create_app()
app = init_app(app)
app = initialize_blueprints(app)
@app.route('/admin/')
def newindex():
return render_template("index.html")
@app.route('/')
def index():
return render_template('home.html')
def upload_static_files(app):
import flask_s3
flask_s3.create_all(app)
# commands
def create_superuser():
from services.userservice import UserService
with app.app_context():
if UserService.check_duplicate(login='admin'):
user = UserService.registration(
login='admin', email='a@a.ru', password='admin', is_superuser=True,
first_name='Админов', last_name='Админ', role=['admin']
)
db.session.add(user)
db.session.commit()
else:
debug(u'Error - has admin.')
raise Exception(u"Has admin.")
def change_password(username, password):
from services.userservice import UserService
with app.app_context():
user = UserService.change_password(username, password)
db.session.add(user)
db.session.commit()
app.create_superuser = create_superuser
app.change_password = change_password
app.upload_static_files = upload_static_files
if __name__ == "__main__":
debug(u"Запуск системы.")
app.run(debug=not IS_PROD)