-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path__init__.py
87 lines (61 loc) · 2.27 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
__author__ = 'StockManager_Crew'
# import configparser
from config_deployment import DB
from database import db
from flask import Flask
from flask_bootstrap import Bootstrap
from flask_login import LoginManager
from os import path
from sqlalchemy_utils import database_exists, create_database
from sqlalchemy.exc import IntegrityError
from werkzeug.security import generate_password_hash
from sqlalchemy.orm.mapper import configure_mappers
import sqlalchemy as sa
from sqlalchemy_searchable import make_searchable
def init_admin():
user = User()
user.username = "admin"
user.password = generate_password_hash('admin', method='sha256')
user.email = '[email protected]'
user.admin = True
user.poweruser = True
try:
db.session.add(user)
db.session.commit()
except IntegrityError as e:
print('W bazie istnieje już użytkownik o nazwie: '+user.username)
app = Flask(__name__)
app.static_path = path.join(path.abspath(__file__), 'static')
app.config['SQLALCHEMY_DATABASE_URI'] = DB.SQLALCHEMY_DATABASE_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = DB.SQLALCHEMY_TRACK_MODIFICATIONS
app.config['SECRET_KEY'] = DB.SECRET_KEY
Bootstrap(app)
db.init_app(app)
make_searchable(db.metadata)
with app.test_request_context():
if not database_exists(DB.SQLALCHEMY_DATABASE_URI):
create_database(DB.SQLALCHEMY_DATABASE_URI)
from auth.models import User, WorkingTimeRecord, LeaveApplication
from invoices.models import Products, Customers, Invoices, Basket, Quantities, Suppliers, Orders
sa.orm.configure_mappers()
db.create_all()
db.session.commit()
if not User.query.filter_by(username='admin').first():
init_admin()
from home.views import home_blueprint
from auth.views import auth_blueprint
from invoices.views import invoices_blueprint
from stock.views import stock_blueprint
app.register_blueprint(home_blueprint)
app.register_blueprint(auth_blueprint)
app.register_blueprint(invoices_blueprint)
app.register_blueprint(stock_blueprint)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'auth.login'
from auth.models import User
@login_manager.user_loader
def load_user(user_id):
return User.query.get(int(user_id))
if __name__ == '__main__':
app.run(debug=True)