# pip install flask
# flask вэб ажиллуулах
from flask import Flask, render_template, request, redirect
import json
import random
import string
def get_random_string(length=3):
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(length))
return result_str
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def home():
if request.method == 'POST':
student_name = request.form['lastName']
print(f'student_name = {student_name}')
Student_Class = request.form['Class']
print(f'Student_Class = {Student_Class}')
student_dob = request.form['Student_DoB']
print(f'Student_DoB = {student_dob}')
grade = request.form['Grade']
print(f'Student_Grade = {grade}')
# суралцагч утга авах хэсэг
student = {
'index': get_random_string(),
'lastName': student_name,
'Class': Student_Class,
'Student_Dob': student_dob,
'Grade': grade
}
# print('student =', student)
students = read_info()['Students']
# print('students =', students)
students.append(student)
# print('students =', students)
teacher_fname = request.form['firstname']
print(f'teacher_fname = {teacher_fname}')
teacher_lname = request.form['lastname']
print(f'teacher_lname = {teacher_lname}')
teacher_dob = request.form['teacher_DoB']
print(f'teacher_dob = {teacher_dob}')
degree = request.form['Degree']
print(f'degree = {degree}')
experience = request.form['Experience']
print(f'experience = {experience}')
# багшийн мэдээлэл авах хэсэг
teacher = {
'index': get_random_string(),
'firstname': teacher_fname,
'lastname': teacher_lname,
'teacher_DoB': teacher_dob,
'Degree': degree,
'Experience': experience
}
# print('teacher =', teacher)
teachers = read_info()['Teachers']
# print('teachers =', teachers)
teachers.append(teacher)
# print('teachers =', teachers)
create_enrol(students, teachers)
return redirect('/')
else:
return render_template('index.html')
def create_enrol(student_info, teacher_info):
info = {
'Students': student_info,
'Teachers': teacher_info
} # тусдаа хоёр утгуудаа нэгтгэж байгаа хэсэг тэгээд нэг файлд Датабаз хийнэ
with open('db.txt', 'w') as db:
json.dump({'Information': info}, db)
def read_info():
with open('db.txt') as db:
data = json.load(db)
# print(f'data = {data}')
info_data = data['Information']
return info_data
@app.route('/list', methods=['GET'])
def info_list():
return render_template('list.html', information_list = read_info())
@app.route('/update/<index>', methods=['GET', 'POST'])
def info_update(index):
print('before index = ', index)
stu = get_info(index)
print('before info = ', stu)
if stu is None:
return redirect('/not-found')
if request.method == 'POST':
print("request.form['lastName'] =", request.form['lastName'])
print("request.form['Class'] =", request.form['Class'])
print("request.form['Student_Dob'] =", request.form['Student_Dob'])
print("request.form['Grade'] =", request.form['Grade'])
stu['lastName'] = request.form['lastName']
stu['Class'] = request.form['Class']
stu['Student_Dob'] = request.form['Student_Dob']
stu['Grade'] = request.form['Grade']
update_student(stu)
return redirect('/list')
else:
return render_template('update.html', stu = stu)
@app.route('/update_t/<index>', methods=['GET', 'POST'])
def info_update_t(index):
print('before index = ', index)
teacher = get_info_t(index)
print('before info = ', teacher)
if teacher is None:
return redirect('/not-found')
if request.method == 'POST':
print("request.form['lastname'] =", request.form['lastname'])
print("request.form['firstname'] =", request.form['firstname'])
print("request.form['teacher_DoB'] =", request.form['teacher_DoB'])
print("request.form['Degree'] =", request.form['Degree'])
print("request.form['lastname'] =", request.form['Experience'])
teacher['lastname'] = request.form['lastname']
teacher['firstname'] = request.form['firstname']
teacher['teacher_DoB'] = request.form['teacher_DoB']
teacher['Degree'] = request.form['Degree']
teacher['Experience'] = request.form['Experience']
update_teachers(teacher)
return redirect('/list')
else:
return render_template('update_t.html', teacher = teacher)
def get_info(get_par):
info_datas = read_info()
students = info_datas['Students']
index_s = []
for item in students:
if item['index'] == get_par:
index_s.append(item)
if len(index_s) > 0:
return index_s[0]
def get_info_t(get_par):
info_datas = read_info()
teachers = info_datas['Teachers']
index_s = []
for item in teachers:
if item['index'] == get_par:
index_s.append(item)
if len(index_s) > 0:
return index_s[0]
def update_student(up_Stu):
students = read_info()['Students']
teachers = read_info()['Teachers']
for item in students:
if item['index'] == up_Stu['index']:
item['lastName'] = up_Stu['lastName']
item['Student_Dob'] = up_Stu['Student_Dob']
item['Class'] = up_Stu['Class']
item['Grade'] = up_Stu['Grade']
break
info = {
'Students': students,
'Teachers': teachers
}
with open('db.txt', 'w') as db:
json.dump({'Information':info}, db)
def update_teachers(up_Teacher):
students = read_info()['Students']
teachers = read_info()['Teachers']
for item in teachers:
if item['index'] == up_Teacher['index']:
item['firstname'] = up_Teacher['firstname']
item['lastname'] = up_Teacher['lastname']
item['teacher_DoB'] = up_Teacher['teacher_DoB']
item['Degree'] = up_Teacher['Degree']
item['Experience'] = up_Teacher['Experience']
break
info = {
'Students': students,
'Teachers': teachers
}
with open('db.txt', 'w') as db:
json.dump({'Information':info}, db)
@app.route('/list/<index>', methods=['GET', 'POST']) # устгах хэсэг
def delete_info(index):
info_datas = read_info()
students = info_datas['Students']
teachers = info_datas['Teachers']
if index is None:
return redirect('/not-found')
# if request.method == 'POST':
index_s = []
for item in students:
index_s.append(item['index'])
index_t = []
for item in teachers:
index_t.append(item['index'])
print(index_s)
print(index_t)
if index in index_s:
student_index = index_s.index(index)
students.pop(student_index)
if index in index_t:
teacher_index = index_t.index(index)
teachers.pop(teacher_index)
info = {
'Students': students,
'Teachers': teachers
}
with open('db.txt', 'w') as db:
json.dump({'Information':info}, db)
return redirect('/list')
@app.route('/not-found')
def not_found():
return 'Хоосон байна, буцна уу!!!'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)
# тус flask код ажиллуулахын тулд хэд хэдэн файлийн систем байх болно.
#Тиймээс уг файлуудыг энд зип файлаар оруулья. Энд дарж татаж авна уу.