forked from Emoretal/gradebook
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gradebook.py
155 lines (119 loc) · 4.54 KB
/
gradebook.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
from flask import Flask
from flask import render_template
from flask import redirect
from flask import url_for
from flask import request
from flask_sqlalchemy import SQLAlchemy
from flask_login import login_user, LoginManager, UserMixin, logout_user, login_required, current_user
from werkzeug.security import check_password_hash, generate_password_hash
#from flask_mysqldb import MySQL
#from datetime import datetime
#from flask_migrate import Migrate
app = Flask(__name__)
app.config["DEBUG"] = True
# Set up the database:
SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
username="ESIS668",
password="python123",
hostname="ESIS668.mysql.pythonanywhere-services.com",
databasename="ESIS668$668gradebook",
)
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
# Set up the login manager:
app.secret_key = "umbc2020"
login_manager = LoginManager()
login_manager.init_app(app)
class User(UserMixin):
def __init__(self, username, password_hash):
self.username = username
self.password_hash = password_hash
def check_password(self, password):
return check_password_hash(self.password_hash, password)
def get_id(self):
return self.username
# Object to hold all users
all_users = {
"admin": User("admin", generate_password_hash("secret")),
"bob": User("bob", generate_password_hash("less-secret")),
"caroline": User("caroline", generate_password_hash("completely-secret")),
"testUser": User("testUser", generate_password_hash("is668")),
}
@login_manager.user_loader
def load_user(user_id):
return all_users.get(user_id)
# Gradebook table
class Gradebook(db.Model):
__tablename__ = "gradebook"
s_id = db.Column(db.Integer, primary_key=True)
fname = db.Column(db.String(128))
lname = db.Column(db.String(128))
major = db.Column(db.String(128))
email = db.Column(db.String(128))
a1 = db.Column(db.Integer) #represents each assignment's grade
a2 = db.Column(db.Integer)
a3 = db.Column(db.Integer)
a4 = db.Column(db.Integer)
# ROUTES
# Login
@app.route("/", methods=["GET", "POST"]) # Login is our homepage
def login():
# Load the initial page
if request.method == "GET":
return render_template("login_page.html")
# Variables for holding the login form and the User class
form = request.form
attempt = load_user(form["username"])
# Validate user for login and redirect
if attempt: # attempt will be NULL and fail if not a valid user
if User.check_password(attempt, form["password"]):
login_user(attempt)
return redirect(url_for("gradebook"))
# If invalid user, reload with error message
return render_template("login_page.html", error=True)
# Logout
@app.route("/logout/")
@login_required
def logout():
logout_user()
return redirect(url_for("/"))
# Gradebook
@app.route("/gradebook", methods=["GET"])
@login_required
def gradebook():
return render_template("gradebook.html", gradebk=Gradebook.query.all())
# Add student
@app.route("/addstudent", methods=["GET", "POST"])
@login_required
def addstudent():
if request.method == "GET":
return render_template("add_student.html")
studentdata = Gradebook(fname=request.form["first_name"], lname=request.form["last_name"], s_id=request.form["student_id"], major=request.form["major_add"], email=request.form["email_address"])
db.session.add(studentdata)
db.session.commit()
return redirect(url_for('gradebook'))
# Remove student
@app.route("/removestudent", methods=["GET", "POST"])
@login_required
def removestudent():
if request.method == "GET":
return render_template("remove_student.html", gradebk=Gradebook.query.all())
student_id = request.form["selected_student"]
studentdata = Gradebook.query.get(student_id)
db.session.delete(studentdata)
db.session.commit()
return redirect(url_for('gradebook'))
# Change a Grade
@app.route("/changegrade", methods=["GET", "POST"])
@login_required
def changegrade():
if request.method == "GET":
return render_template("change_grade2.html", gradebk=Gradebook.query.all())
updatedassign = request.form["assign"]
student_id = request.form["student_id"]
new_grade = request.form["new_grade"]
db.session.query(Gradebook).filter_by(s_id=student_id).update({ updatedassign:new_grade })
db.session.commit()
return redirect(url_for('gradebook'))