-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
163 lines (120 loc) · 5.81 KB
/
Copy pathapp.py
File metadata and controls
163 lines (120 loc) · 5.81 KB
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
import os
from flask import Flask
from flask import render_template, request, redirect, url_for
from flask_bootstrap import Bootstrap
from flask_wtf.csrf import CSRFProtect
from data import sub_codes_map, exam_code_map
from form import StudentSearchForm, StudentEditAddForm, MarksEditForm
from sql_connections import get_students, register_student, validate_new_student, \
get_student_details, delete_student_sql, update_student_details, update_student_marks
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get("SECRET_KEY", os.urandom(32))
bootstrap = Bootstrap(app)
csrf = CSRFProtect(app)
csrf.init_app(app)
app.config['SESSION_COOKIE_SECURE'] = False
@app.route("/dumb")
def dumb():
return {"status": "ok"}
@app.route("/about")
def about():
return render_template("about.html", nav_title="About Project")
@app.route("/students", methods=["GET"])
def students():
form = StudentSearchForm()
student_class = request.args.get("class")
student_name = request.args.get("name")
if student_class:
all_students = get_students(student_class=student_class)
elif student_name:
all_students = get_students(name=student_name)
else:
all_students = get_students()
return render_template("students.html", nav_title="Students", all_students=all_students, form=form)
@app.route("/students/new", methods=["GET", "POST"])
def new_students():
form = StudentEditAddForm()
if request.method == "POST":
error = "Class and Roll No combination already exists"
if form.validate_on_submit() & validate_new_student(form.grade.data, form.roll_no.data):
try:
register_student(name=form.name.data,
grade=form.grade.data,
father_name=form.father_name.data,
mother_name=form.mother_name.data,
father_phone=form.father_mobile_no.data,
mother_phone=form.mother_mobile_no.data,
roll=form.roll_no.data,
address=form.address.data)
except Exception:
print("Error")
return render_template("success.html", nav_title="New Student")
else:
if form.errors:
error = f"{[i for i in form.errors.values()][0][0]} : {[i for i in form.errors.keys()][0]}"
return render_template("student_edit_register.html", nav_title="Register Student", form=form, error=error)
return render_template("student_edit_register.html", nav_title="Register Student", form=form)
@app.route("/students/<int:student_id>", methods=["GET"])
def student(student_id):
student_details = get_student_details(student_id)
return render_template("student_profile.html",
data=student_details["details"][0],
marks_data=student_details["marks"],
exam_codes=exam_code_map,
nav_title="Student Profile")
@app.route("/students/<int:student_id>/edit", methods=["GET", "POST"])
def edit_student(student_id):
student_details = get_student_details(student_id)["details"]
form = StudentEditAddForm()
if request.method == "POST":
error = "An error occurred"
if form.validate_on_submit():
update_student_details(student_id=student_id,
name=form.name.data,
father_name=form.father_name.data,
mother_name=form.mother_name.data,
father_phone=form.father_mobile_no.data,
mother_phone=form.mother_mobile_no.data,
address=form.address.data)
return render_template("success.html", nav_title="New Student")
else:
if form.errors:
error = f"{[i for i in form.errors.values()][0][0]} : {[i for i in form.errors.keys()][0]}"
return render_template("student_edit_register.html",
nav_title="Edit Student",
form=form,
error=error,
edit_data=student_details[0])
return render_template("student_edit_register.html",
nav_title="Edit Student",
form=form,
edit_data=student_details[0])
@app.route("/students/<int:student_id>/edit-marks", methods=["GET", "POST"])
def edit_student_marks(student_id):
form = MarksEditForm()
exam = request.args.get("exam")
if exam is None:
return redirect(f"/students/{student_id}/edit-marks?exam=mt_1")
if request.method == "POST":
update_student_marks(student_id=student_id,
exam_id=exam,
marks_map=[(sub[0], form.data[sub[0]]) for sub in sub_codes_map])
return redirect(f"/students/{student_id}/edit-marks?exam={exam}")
student_details = get_student_details(student_id)
return render_template("edit_student_marks.html",
nav_title="Edit Marks",
form=form,
sub_codes=sub_codes_map,
exam_code=exam_code_map,
marks_data=student_details["marks"][str(exam)],
selected_exam=exam,
data=student_details["details"][0])
@app.route("/students/<int:student_id>/delete", methods=["GET"])
def delete_student(student_id):
delete_student_sql(student_id)
return redirect(url_for("students"))
@app.errorhandler(404)
def own_404_page(_error):
return redirect(url_for("students"))
if __name__ == "__main__":
app.run()