1+ """This is file contains the application factory that contains necessary functions for the app to start.
2+ It also contains necessary inititalizations for the app.This function is run by WSGI to start our application on the web."""
13from flask import Flask
24from views import routes
3- from extensions import db
5+ from extensions import db , admin , encrypt
46from flask_login import LoginManager
57from models import User
8+ from config import SECRET_KEY , PASSWORD
9+ import os
10+ from admin import bp as admin_bp
611
7-
12+ #The application factory
813def create_app (settings = "config.py" ):
914 """Factory function to start the web app when everthing(i.e configurations are set up in place.)"""
10- app = Flask (__name__ )#Creating app instance
11- app .config .from_pyfile (settings )
15+ myapp = Flask (__name__ )#Creating app instance
16+ myapp .config ["SECRET_KEY" ]= SECRET_KEY
17+ myapp .config ["SQLALCHEMY_DATABASE_URI" ]= 'mysql+pymysql://soupnazi:librarysystem@soupnazi.mysql.pythonanywhere-services.com/soupnazi$lib_data'
18+ myapp .config ["SQLALCHEMY_TRACK_MODIFICATIONS" ]= False
19+ myapp .config ['SQLALCHEMY_POOL_RECYCLE' ] = 299
20+ myapp .config ['SQLALCHEMY_POOL_TIMEOUT' ] = 20
21+ DEBUG = False
1222
1323
1424 """Initializing extensions"""
15- db .init_app (app )
25+ db .init_app (myapp )
26+
27+ from models import User , Book
28+ with myapp .app_context ():
29+ db .create_all ()
30+ db .session .commit ()
1631
32+ admin .init_app (myapp )#The administrators instance
33+ encrypt .init_app (myapp )#Instance of bcrypt password hashing utility
34+ #client.on_connect=on_connect
35+ #client.on_message=on_message
36+ #client.connect('broker.hivemq.com', 1883)
37+ #client.loop_forever()
1738
1839
19- app .register_blueprint (routes )
40+ #Registering routes
41+ myapp .register_blueprint (routes )
42+ myapp .register_blueprint (admin_bp )
2043
44+
45+ #Managing logins
2146 login_manager = LoginManager ()
2247 login_manager .login_view = 'routes.login'
23- login_manager .init_app (app )
48+ login_manager .init_app (myapp )
2449
2550 @login_manager .user_loader
2651 def load_user (id ):
2752 return User .query .get (int (id ))
2853
29- return app .run (port = 8080 ,debug = True )
54+ return myapp
55+
0 commit comments