Skip to content

Commit ec0eb08

Browse files
committed
second update
1 parent 2804ffe commit ec0eb08

16 files changed

Lines changed: 169 additions & 91 deletions

.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
SECRET_KEY='superSECRET'
2+
PASSWORD='4321@hello'

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
create_db

__pycache__/admin.cpython-38.pyc

1.9 KB
Binary file not shown.

__pycache__/app.cpython-38.pyc

813 Bytes
Binary file not shown.

__pycache__/config.cpython-38.pyc

283 Bytes
Binary file not shown.
191 Bytes
Binary file not shown.

__pycache__/models.cpython-38.pyc

152 Bytes
Binary file not shown.

__pycache__/views.cpython-38.pyc

-504 Bytes
Binary file not shown.

admin.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""This file manages the administrators page. We define the administrator models here."""
2+
from extensions import admin,db
3+
from models import User, Book
4+
from flask_admin.contrib.sqla import ModelView
5+
from flask_admin import AdminIndexView
6+
from flask import Blueprint, redirect, url_for,request,render_template, flash
7+
from views import routes
8+
from flask_login import current_user, login_user
9+
10+
11+
bp=Blueprint("admin_bp",__name__)#Creating route for this accesspoint.
12+
13+
class MyAdminIndexView(AdminIndexView):
14+
def is_accessible(self):
15+
return redirect(url_for("admin_bp.Manage"))
16+
17+
class UserModelView(ModelView):
18+
column_list=["email"]
19+
20+
21+
@bp.route("/Manage", methods=["GET","POST"])
22+
def Manage():
23+
"""Administrators route. admin can register new books and enable registered users borrow books"""
24+
if request.method == "POST":
25+
email=request.form.get('email')
26+
password=request.form.get('password')
27+
user=User.query.get('admin@gmail.com')
28+
if user==email:
29+
if user.password==password:
30+
flash("Logged in successfully!", category='success')
31+
login_user(user, remember=True)
32+
return redirect(url_for('admin'))
33+
else:
34+
"""Password did not match the hash, access is denied with error message"""
35+
flash("Incorrect details. Try again.", category='error')
36+
else:
37+
"""Email does not belong to the admin"""
38+
flash("You are not admin",category="error")
39+
return render_template("administrator.html",user=current_user)
40+
admin.add_view(UserModelView(User,db.session))
41+
admin.add_view(ModelView(Book,db.session))

app.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,55 @@
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."""
13
from flask import Flask
24
from views import routes
3-
from extensions import db
5+
from extensions import db,admin,encrypt
46
from flask_login import LoginManager
57
from 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
813
def 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

Comments
 (0)