diff --git a/.gitignore b/.gitignore
index 4591ab2..b400de5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,4 +2,5 @@
*.env
test*.*
__pycache__/*
-excel/*
\ No newline at end of file
+excel/*
+venv/*
\ No newline at end of file
diff --git a/app.py b/app.py
index 4104c90..e0f76ae 100644
--- a/app.py
+++ b/app.py
@@ -2,6 +2,7 @@ from functions import *
from manage.manage import manage
from upload import upload
from login import login
+
load_dotenv()
app = Flask(__name__)
babel = Babel(app)
@@ -10,14 +11,17 @@ app.register_blueprint(upload)
app.register_blueprint(login)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
-app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://'+os.environ.get('MYSQL_USER')+':'+os.environ.get('MYSQL_PASSWORD')+'@'+os.environ.get('MYSQL_HOST')+'/attendance'
+app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://' + os.environ.get('MYSQL_USER') + ':' + os.environ.get(
+ 'MYSQL_PASSWORD') + '@' + os.environ.get('MYSQL_HOST') + '/attendance'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['BABEL_DEFAULT_LOCALE'] = 'zh_TW'
app.jinja_env.add_extension('jinja2.ext.loopcontrols')
sdb = SQLAlchemy(app)
+
class DefaultModelView(ModelView):
restricted = True
+
def __init__(self, model, session, restricted=True, name=None, category=None, endpoint=None, url=None, **kwargs):
self.restricted = restricted
self.column_default_sort = ('id', True)
@@ -25,24 +29,32 @@ class DefaultModelView(ModelView):
setattr(self, k, v)
setattr(self, 'can_export', True)
super(DefaultModelView, self).__init__(model, session, name=name, category=category, endpoint=endpoint, url=url)
+
def is_accessible(self):
- if self.restricted == True:
- return ((not check_login_status()) and is_admin() and check_permission())
- return ((not check_login_status()) and is_admin())
- def inaccessible_callback(self, name, **kwargs):
- return redirect('/')
-class MyAdminIndexView(AdminIndexView):
- def is_accessible(self):
- return ((not check_login_status()) and is_admin())
+ if self.restricted:
+ return (not check_login_status()) and is_admin() and check_permission()
+ return (not check_login_status()) and is_admin()
+
def inaccessible_callback(self, name, **kwargs):
return redirect('/')
+
+class MyAdminIndexView(AdminIndexView):
+ def is_accessible(self):
+ return (not check_login_status()) and is_admin()
+
+ def inaccessible_callback(self, name, **kwargs):
+ return redirect('/')
+
+
admin = Admin(
- app,
- name='Attendance 點名系統 後台管理',
- template_mode='bootstrap3',
- index_view=MyAdminIndexView(),
- )
+ app,
+ name='Attendance 點名系統 後台管理',
+ template_mode='bootstrap3',
+ index_view=MyAdminIndexView(),
+)
+
+
class Users(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
email = sdb.Column(sdb.Text)
@@ -50,6 +62,8 @@ class Users(sdb.Model):
oldUsername = sdb.Column(sdb.Text)
role = sdb.Column(sdb.CHAR)
password = sdb.Column(sdb.Text)
+
+
class Students(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
email = sdb.Column(sdb.INT)
@@ -60,6 +74,8 @@ class Students(sdb.Model):
ename = sdb.Column(sdb.Text)
classes = sdb.Column(sdb.Text)
password = sdb.Column(sdb.Text)
+
+
class Schedule(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
grade = sdb.Column(sdb.INT)
@@ -68,6 +84,8 @@ class Schedule(sdb.Model):
period = sdb.Column(sdb.CHAR)
subject = sdb.Column(sdb.Text)
teacher = sdb.Column(sdb.Text)
+
+
class SpecSchedule(sdb.Model):
__tablename__ = 'specschedule'
id = sdb.Column(sdb.INT, primary_key=True)
@@ -77,6 +95,8 @@ class SpecSchedule(sdb.Model):
period = sdb.Column(sdb.CHAR)
subject = sdb.Column(sdb.Text)
teacher = sdb.Column(sdb.Text)
+
+
class GPClasses(sdb.Model):
__tablename__ = 'gpclasses'
id = sdb.Column(sdb.INT, primary_key=True)
@@ -84,11 +104,15 @@ class GPClasses(sdb.Model):
subclass = sdb.Column(sdb.Text)
about = sdb.Column(sdb.Text)
accs = sdb.Column(sdb.Text)
+
+
class Homerooms(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
grade = sdb.Column(sdb.INT)
class_ = sdb.Column(sdb.INT)
accs = sdb.Column(sdb.Text)
+
+
class Submission(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
grade = sdb.Column(sdb.INT)
@@ -104,6 +128,8 @@ class Submission(sdb.Model):
ds6 = sdb.Column(sdb.INT)
ds7 = sdb.Column(sdb.INT)
notes = sdb.Column(sdb.Text)
+
+
class DS(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
grade = sdb.Column(sdb.INT)
@@ -113,10 +139,14 @@ class DS(sdb.Model):
period = sdb.Column(sdb.CHAR)
note = sdb.Column(sdb.Text)
status = sdb.Column(sdb.CHAR, default='X')
+
+
class Dates(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
date = sdb.Column(sdb.VARCHAR(11))
dow = sdb.Column(sdb.INT)
+
+
class Absent(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
grade = sdb.Column(sdb.INT)
@@ -126,22 +156,34 @@ class Absent(sdb.Model):
period = sdb.Column(sdb.CHAR)
status = sdb.Column(sdb.CHAR)
note = sdb.Column(sdb.Text)
+
+
class Forgot(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
resetID = sdb.Column(sdb.VARCHAR(11))
email = sdb.Column(sdb.Text)
reqTime = sdb.Column(sdb.VARCHAR(20))
-admin.add_view(DefaultModelView(Users, sdb.session, restricted=False, column_exclude_list = ['password'], column_searchable_list = ['name', 'email', 'role']))
-admin.add_view(DefaultModelView(Students, sdb.session, restricted=False, column_exclude_list = ['password'], column_searchable_list = ['grade', 'class_', 'num', 'email','name', 'ename', 'classes']))
-admin.add_view(DefaultModelView(Schedule, sdb.session, column_searchable_list = ['grade', 'class_', 'dow', 'period', 'subject', 'teacher']))
-admin.add_view(DefaultModelView(SpecSchedule, sdb.session, restricted=False, column_searchable_list = ['grade', 'class_', 'date', 'period', 'subject', 'teacher']))
-admin.add_view(DefaultModelView(GPClasses, sdb.session, column_searchable_list = ['category', 'subclass', 'about', 'accs']))
-admin.add_view(DefaultModelView(Homerooms, sdb.session, column_searchable_list = ['grade', 'class_', 'accs']))
-admin.add_view(DefaultModelView(Submission, sdb.session, column_exclude_list=['signature'], column_searchable_list = ['grade', 'class_', 'date', 'period', 'notes']))
-admin.add_view(DefaultModelView(DS, sdb.session, restricted=False, column_searchable_list = ['grade', 'class_', 'date', 'period', 'num', 'note', 'status']))
-admin.add_view(DefaultModelView(Dates, sdb.session, column_searchable_list = ['date', 'dow']))
-admin.add_view(DefaultModelView(Absent, sdb.session, restricted=False, column_searchable_list = ['grade', 'class_', 'date', 'period', 'num', 'status', 'note']))
-admin.add_view(DefaultModelView(Forgot, sdb.session, column_searchable_list = ['resetID', 'email', 'reqTime']))
+
+
+admin.add_view(DefaultModelView(Users, sdb.session, restricted=False, column_exclude_list=['password'],
+ column_searchable_list=['name', 'email', 'role']))
+admin.add_view(DefaultModelView(Students, sdb.session, restricted=False, column_exclude_list=['password'],
+ column_searchable_list=['grade', 'class_', 'num', 'email', 'name', 'ename', 'classes']))
+admin.add_view(DefaultModelView(Schedule, sdb.session,
+ column_searchable_list=['grade', 'class_', 'dow', 'period', 'subject', 'teacher']))
+admin.add_view(DefaultModelView(SpecSchedule, sdb.session, restricted=False,
+ column_searchable_list=['grade', 'class_', 'date', 'period', 'subject', 'teacher']))
+admin.add_view(
+ DefaultModelView(GPClasses, sdb.session, column_searchable_list=['category', 'subclass', 'about', 'accs']))
+admin.add_view(DefaultModelView(Homerooms, sdb.session, column_searchable_list=['grade', 'class_', 'accs']))
+admin.add_view(DefaultModelView(Submission, sdb.session, column_exclude_list=['signature'],
+ column_searchable_list=['grade', 'class_', 'date', 'period', 'notes']))
+admin.add_view(DefaultModelView(DS, sdb.session, restricted=False,
+ column_searchable_list=['grade', 'class_', 'date', 'period', 'num', 'note', 'status']))
+admin.add_view(DefaultModelView(Dates, sdb.session, column_searchable_list=['date', 'dow']))
+admin.add_view(DefaultModelView(Absent, sdb.session, restricted=False,
+ column_searchable_list=['grade', 'class_', 'date', 'period', 'num', 'status', 'note']))
+admin.add_view(DefaultModelView(Forgot, sdb.session, column_searchable_list=['resetID', 'email', 'reqTime']))
admin.add_link(MenuLink(name='Back to Home 返回一般管理', category='', url='/manage'))
admin.add_link(MenuLink(name='Logout 登出', category='', url='/logout'))
diff --git a/export.py b/export.py
index facb606..df929e3 100644
--- a/export.py
+++ b/export.py
@@ -9,6 +9,7 @@ side = Side(border_style='thin')
border = Border(left=side, right=side, top=side, bottom=side)
bold_bottom = Border(left=side, right=side, top=side, bottom=Side(border_style='medium', color='FF000000'))
+
def create_period_sheets(workbook, class_code):
ws = workbook.create_sheet(class_code[0] + class_code[1])
ws.merge_cells('A1:F1')
@@ -41,7 +42,7 @@ def create_period_sheets(workbook, class_code):
ws.cell(row=2, column=i).font = Font(size=14, bold=True)
ws.cell(row=2, column=i).alignment = center
ws.cell(row=2, column=i).border = border
-
+
# loop over C:G
for i in range(2, 8):
ws.column_dimensions[str(chr(ord('A') + i))].width = 13
@@ -49,7 +50,8 @@ def create_period_sheets(workbook, class_code):
# get data
db = refresh_db()
cursor = db.cursor()
- cursor.execute("SELECT dow,period,subject,teacher FROM schedule WHERE grade=%s AND class_=%s", (class_code[0], class_code[1]))
+ cursor.execute("SELECT dow,period,subject,teacher FROM schedule WHERE grade=%s AND class_=%s",
+ (class_code[0], class_code[1]))
sql = cursor.fetchall()
data = {}
subject_teacher = {}
@@ -64,7 +66,7 @@ def create_period_sheets(workbook, class_code):
if i[2] != 'GP' and i[2] != '--' and i[3] != '--' and i[2] not in subject_teacher:
subject_teacher[i[2]] = i[3]
- periods=['m', '1', '2', '3', '4', 'n', '5', '6', '7', '8', '9']
+ periods = ['m', '1', '2', '3', '4', 'n', '5', '6', '7', '8', '9']
times = {
'm': ['7:30', '8:10'],
'1': ['8:20', '9:05'],
@@ -100,25 +102,27 @@ def create_period_sheets(workbook, class_code):
if p == 'm' or p == 'n':
ws.merge_cells('C' + str(curr) + ':G' + str(curr + 1))
for i in range(1, 6):
- ws[chr(ord('C') + i-1) + str(curr)].font = std_font
- ws[chr(ord('C') + i-1) + str(curr)].alignment = center
- ws[chr(ord('C') + i-1) + str(curr)].border = border
- ws[chr(ord('C') + i-1) + str(curr + 1)].border = border
+ ws[chr(ord('C') + i - 1) + str(curr)].font = std_font
+ ws[chr(ord('C') + i - 1) + str(curr)].alignment = center
+ ws[chr(ord('C') + i - 1) + str(curr)].border = border
+ ws[chr(ord('C') + i - 1) + str(curr + 1)].border = border
if p == 'm':
ws['C' + str(curr)] = '早自習'
else:
ws['C' + str(curr)] = '午餐 / 午休'
else:
for i in range(1, 6):
- ws.merge_cells(chr(ord('C') + i-1) + str(curr) + ':' + chr(ord('C') + i-1) + str(curr + 1))
- ws[chr(ord('C') + i-1) + str(curr)].font = std_font
- ws[chr(ord('C') + i-1) + str(curr)].alignment = center
- ws[chr(ord('C') + i-1) + str(curr)].border = border
- ws[chr(ord('C') + i-1) + str(curr + 1)].border = border
+ ws.merge_cells(chr(ord('C') + i - 1) + str(curr) + ':' + chr(ord('C') + i - 1) + str(curr + 1))
+ ws[chr(ord('C') + i - 1) + str(curr)].font = std_font
+ ws[chr(ord('C') + i - 1) + str(curr)].alignment = center
+ ws[chr(ord('C') + i - 1) + str(curr)].border = border
+ ws[chr(ord('C') + i - 1) + str(curr + 1)].border = border
if i in data:
if p in data[i]:
- ws[chr(ord('C') + i-1) + str(curr)] = (data[i][p]['subject'] if data[i][p]['subject'] != 'GP'
- and data[i][p]['subject'] != '--' else '' if data[i][p]['subject'] == '--' else data[i][p]['teacher'])
+ ws[chr(ord('C') + i - 1) + str(curr)] = (data[i][p]['subject'] if data[i][p]['subject'] != 'GP'
+ and data[i][p][
+ 'subject'] != '--' else '' if
+ data[i][p]['subject'] == '--' else data[i][p]['teacher'])
curr += 2
ws.merge_cells('A26:G26')
ws['A26'] = '科任老師一覽表'
@@ -136,7 +140,7 @@ def create_period_sheets(workbook, class_code):
pos = ['D', 'E']
else:
pos = ['F', 'G']
- loc = str(27+ int(curr/3))
+ loc = str(27 + int(curr / 3))
ws.merge_cells(pos[0] + loc + ':' + pos[1] + loc)
ws[pos[0] + loc].font = std_font
ws[pos[0] + loc].alignment = center
@@ -148,6 +152,7 @@ def create_period_sheets(workbook, class_code):
curr += 1
return workbook
+
def create_student_list(workbook, class_code):
ws = workbook.create_sheet(class_code[0] + class_code[1])
ws.merge_cells('A1:J1')
@@ -186,27 +191,29 @@ def create_student_list(workbook, class_code):
ws.column_dimensions[str(chr(ord('A') + i))].width = 5.8
db = refresh_db()
cursor = db.cursor()
- cursor.execute('SELECT num,name,ename FROM students WHERE grade=%s AND class_=%s ORDER BY num ASC', (class_code[0], class_code[1]))
+ cursor.execute('SELECT num,name,ename FROM students WHERE grade=%s AND class_=%s ORDER BY num ASC',
+ (class_code[0], class_code[1]))
data = cursor.fetchall()
last = data[-1][0]
delcnt = 0
for i in range(0, last):
- ws['A' + str(3 + i)] = i+1
+ ws['A' + str(3 + i)] = i + 1
ws['A' + str(3 + i)].font = std_font
ws['A' + str(3 + i)].alignment = center
- ws['B' + str(3 + i)] = data[i - delcnt][1] if data[i - delcnt][0] == i+1 else ''
+ ws['B' + str(3 + i)] = data[i - delcnt][1] if data[i - delcnt][0] == i + 1 else ''
ws['B' + str(3 + i)].font = Font(name="DFKai-SB", size=14)
ws['B' + str(3 + i)].alignment = center
- ws['C' + str(3 + i)] = data[i - delcnt][2] if data[i - delcnt][0] == i+1 else ''
+ ws['C' + str(3 + i)] = data[i - delcnt][2] if data[i - delcnt][0] == i + 1 else ''
ws['C' + str(3 + i)].font = std_font
ws['C' + str(3 + i)].alignment = center
ws.row_dimensions[3 + i].height = 19
for j in range(0, 12):
- ws[str(chr(ord('A') + j)) + str(3 + i)].border = bold_bottom if (i+1)%5==0 else border
- if data[i - delcnt][0] != i+1:
+ ws[str(chr(ord('A') + j)) + str(3 + i)].border = bold_bottom if (i + 1) % 5 == 0 else border
+ if data[i - delcnt][0] != i + 1:
delcnt += 1
return workbook
+
def create_teacher_periods(workbook, teacher_name, orig_username=''):
ws = workbook.create_sheet(teacher_name)
ws.merge_cells('A1:E1')
@@ -241,7 +248,7 @@ def create_teacher_periods(workbook, teacher_name, orig_username=''):
ws.cell(row=2, column=i).font = Font(size=14, bold=True)
ws.cell(row=2, column=i).alignment = center
ws.cell(row=2, column=i).border = border
-
+
# loop over C:G
for i in range(2, 8):
ws.column_dimensions[str(chr(ord('A') + i))].width = 13
@@ -251,10 +258,10 @@ def create_teacher_periods(workbook, teacher_name, orig_username=''):
db = refresh_db()
cursor = db.cursor()
if orig_username is not '':
- cursor.execute('SELECT category,subclass FROM gpclasses WHERE accs LIKE %s', ('%'+orig_username+'%',))
+ cursor.execute('SELECT category,subclass FROM gpclasses WHERE accs LIKE %s', ('%' + orig_username + '%',))
gp_sql = cursor.fetchall()
for i in gp_sql:
- cursor.execute('SELECT dow,period FROM schedule WHERE teacher=%s', (i[0], ))
+ cursor.execute('SELECT dow,period FROM schedule WHERE teacher=%s', (i[0],))
tmp_sql = cursor.fetchall()
for j in tmp_sql:
if j[0] not in data:
@@ -263,7 +270,7 @@ def create_teacher_periods(workbook, teacher_name, orig_username=''):
'subject': i[0],
'class': i[1]
}
- cursor.execute("SELECT dow,period,subject,grade,class_ FROM schedule WHERE teacher=%s", (teacher_name, ))
+ cursor.execute("SELECT dow,period,subject,grade,class_ FROM schedule WHERE teacher=%s", (teacher_name,))
sql = cursor.fetchall()
# loop over data
for i in sql:
@@ -274,7 +281,7 @@ def create_teacher_periods(workbook, teacher_name, orig_username=''):
'class': str(i[3]) + str(i[4])
}
- periods=['m', '1', '2', '3', '4', 'n', '5', '6', '7', '8', '9']
+ periods = ['m', '1', '2', '3', '4', 'n', '5', '6', '7', '8', '9']
times = {
'm': ['7:30', '8:10'],
'1': ['8:20', '9:05'],
@@ -310,24 +317,27 @@ def create_teacher_periods(workbook, teacher_name, orig_username=''):
if p == 'm' or p == 'n':
ws.merge_cells('C' + str(curr) + ':G' + str(curr + 1))
for i in range(1, 6):
- ws[chr(ord('C') + i-1) + str(curr)].font = std_font
- ws[chr(ord('C') + i-1) + str(curr)].alignment = center
- ws[chr(ord('C') + i-1) + str(curr)].border = border
- ws[chr(ord('C') + i-1) + str(curr + 1)].border = border
+ ws[chr(ord('C') + i - 1) + str(curr)].font = std_font
+ ws[chr(ord('C') + i - 1) + str(curr)].alignment = center
+ ws[chr(ord('C') + i - 1) + str(curr)].border = border
+ ws[chr(ord('C') + i - 1) + str(curr + 1)].border = border
if p == 'm':
ws['C' + str(curr)] = '早自習'
else:
ws['C' + str(curr)] = '午餐 / 午休'
else:
for i in range(1, 6):
- ws.merge_cells(chr(ord('C') + i-1) + str(curr) + ':' + chr(ord('C') + i-1) + str(curr + 1))
- ws[chr(ord('C') + i-1) + str(curr)].font = std_font
- ws[chr(ord('C') + i-1) + str(curr)].border = border
- ws[chr(ord('C') + i-1) + str(curr + 1)].border = border
- ws[chr(ord('C') + i-1) + str(curr)].alignment = center + Alignment(wrapText=True)
+ ws.merge_cells(chr(ord('C') + i - 1) + str(curr) + ':' + chr(ord('C') + i - 1) + str(curr + 1))
+ ws[chr(ord('C') + i - 1) + str(curr)].font = std_font
+ ws[chr(ord('C') + i - 1) + str(curr)].border = border
+ ws[chr(ord('C') + i - 1) + str(curr + 1)].border = border
+ ws[chr(ord('C') + i - 1) + str(curr)].alignment = center + Alignment(wrapText=True)
if i in data:
if p in data[i]:
- ws[chr(ord('C') + i-1) + str(curr)] = (data[i][p]['subject'] + '\n' + data[i][p]['class'] if data[i][p]['subject'] != 'GP'
- and data[i][p]['subject'] != '--' else '' if data[i][p]['subject'] == '--' else data[i][p]['teacher'])
+ ws[chr(ord('C') + i - 1) + str(curr)] = (
+ data[i][p]['subject'] + '\n' + data[i][p]['class'] if data[i][p]['subject'] != 'GP'
+ and data[i][p][
+ 'subject'] != '--' else '' if
+ data[i][p]['subject'] == '--' else data[i][p]['teacher'])
curr += 2
- return workbook
\ No newline at end of file
+ return workbook
diff --git a/functions.py b/functions.py
index 2c5ec06..9e021af 100644
--- a/functions.py
+++ b/functions.py
@@ -20,6 +20,7 @@ from flask_admin import Admin, AdminIndexView, expose
from flask_admin.menu import MenuLink
from flask_admin.contrib.sqla import ModelView
from flask_babelex import Babel
+
load_dotenv()
tz = pytz.timezone('Asia/Taipei')
@@ -44,7 +45,7 @@ DSTEXT = [
]
DSOFFENSES = {
'A': "把玩物品、不專心聽講",
- 'B': "書寫或傳遞紙條、物品",
+ 'B': "書寫或傳遞紙條、物品",
'C': "自言自語或與同學交談",
'D': "接話、大聲笑、起哄、發出怪聲",
'E': "亂動、逗弄同學、影響教學情境",
@@ -54,17 +55,21 @@ DSOFFENSES = {
'Z': "上課睡覺"
}
+
def refresh_db():
return mysql.connector.connect(user=os.environ.get('MYSQL_USER'), password=os.environ.get('MYSQL_PASSWORD'),
host=os.environ.get('MYSQL_HOST'),
database='attendance')
+
def genHash(password):
return sha256_crypt.hash(password)
+
def verifyPassword(password, hash):
return sha256_crypt.verify(password, hash)
+
def refresh_token():
session['is_logged_in'] = True
session['loginTime'] = datetime.now(tz)
@@ -79,6 +84,7 @@ def check_login_status():
session['is_logged_in'] == False or
(datetime.now(tz) - session['loginTime']).total_seconds() > 43200)
+
def send_email(to, subject, text):
return requests.post(
"https://api.mailgun.net/v3/mg.aaronlee.tech/messages",
@@ -88,6 +94,7 @@ def send_email(to, subject, text):
"subject": subject,
"html": text})
+
def getName(grade, class_, number):
db = refresh_db()
cursor = db.cursor()
@@ -98,6 +105,7 @@ def getName(grade, class_, number):
db.close()
return name[0]
+
# LOGIN
@@ -113,18 +121,14 @@ def verify_recaptcha(response):
print(r.json())
return r.json()['success']
+
# UPLOAD
def is_admin():
return 'subuser_type' in session and session['subuser_type'] == 'admin'
+
def check_permission():
if 'subuser_type' in session and session['subuser_type'] == 'admin':
return session['showUpload']
else:
- return False
-
-# MANAGE
-def removeprefix(s, prefix):
- if s.startswith(prefix):
- return s[len(prefix):]
- return s
+ return False
\ No newline at end of file
diff --git a/login.py b/login.py
index aa55bd2..23bb5d9 100644
--- a/login.py
+++ b/login.py
@@ -1,5 +1,8 @@
from functions import *
+
login = Blueprint('login', __name__)
+
+
@login.after_request
def add_header(response):
response.headers['SameSite'] = "Strict"
@@ -13,10 +16,10 @@ def index():
return render_template('login.html')
return redirect('/select')
elif request.method == 'POST':
- email = request.form['username']
+ email = request.form['username'].strip()
if check_login_status():
try:
- if (verify_recaptcha("")):
+ if verify_recaptcha(""):
if request.form['user_type'] == 'teacher':
db = refresh_db()
cursor = db.cursor(buffered=True)
@@ -26,7 +29,7 @@ def index():
if user == None or not verifyPassword(request.form['password'], user[3]):
raise Exception('Invalid Login')
usrRole = user[1]
- if (usrRole == 'R'):
+ if usrRole == 'R':
print("RealPerson Login SUCC:", email, flush=True)
session['is_logged_in'] = True
session['email'] = email
@@ -34,7 +37,7 @@ def index():
session['oldUsername'] = user[2]
session['loginTime'] = datetime.now(tz)
return redirect('/select')
- if (usrRole == 'A' or usrRole == 'S'):
+ if usrRole == 'A' or usrRole == 'S':
print("Admin Login SUCC:", email, flush=True)
session['subuser_type'] = 'admin'
session['is_logged_in'] = True
@@ -47,7 +50,8 @@ def index():
elif request.form['user_type'] == 'student':
db = refresh_db()
cursor = db.cursor(buffered=True)
- cursor.execute("SELECT password, grade, class_, num, name FROM students WHERE email = %s", (email,))
+ cursor.execute("SELECT password, grade, class_, num, name FROM students WHERE email = %s",
+ (email,))
user = cursor.fetchone()
cursor.close()
if user == None or not verifyPassword(request.form['password'], user[0]):
@@ -83,12 +87,14 @@ def selSubUser():
flash("Timeout. 遇時,請重新登入")
return redirect('/')
refresh_token()
- if 'subuser_type' in session and session['subuser_type'] == 'admin' or 'user_type' in session and session['user_type'] == 'student':
+ if 'subuser_type' in session and session['subuser_type'] == 'admin' or 'user_type' in session and session[
+ 'user_type'] == 'student':
return redirect('/manage')
if request.method == 'GET':
db = refresh_db()
cursor = db.cursor(buffered=True)
- cursor.execute("SELECT category, subclass FROM gpclasses WHERE accs LIKE %s LIMIT 1", ('%'+session['oldUsername']+'%',))
+ cursor.execute("SELECT category, subclass FROM gpclasses WHERE accs LIKE %s LIMIT 1",
+ ('%' + session['oldUsername'] + '%',))
classes = cursor.fetchone()
cursor.close()
hasGroup = False
@@ -96,7 +102,7 @@ def selSubUser():
hasGroup = True
db = refresh_db()
cursor = db.cursor(buffered=True)
- cursor.execute("SELECT grade, class_ FROM homerooms WHERE accs LIKE %s", ('%'+session['oldUsername']+'%',))
+ cursor.execute("SELECT grade, class_ FROM homerooms WHERE accs LIKE %s", ('%' + session['oldUsername'] + '%',))
homerooms = cursor.fetchall()
cursor.close()
hrC = {}
@@ -109,11 +115,11 @@ def selSubUser():
if data == []:
return redirect('/select')
try:
- if (verify_recaptcha("")):
- if (data[0] == 'homeroom'):
+ if verify_recaptcha(""):
+ if data[0] == 'homeroom':
session['homeroom'] = data[1] + '^' + data[2]
session['subuser_type'] = 'homeroom'
- elif (data[0] == 'group'):
+ elif data[0] == 'group':
session['subuser_type'] = 'group'
return redirect('/manage')
else:
@@ -141,7 +147,7 @@ def chgPassword():
if not check_login_status():
refresh_token()
try:
- if (verify_recaptcha("")):
+ if verify_recaptcha(""):
db = refresh_db()
cursor = db.cursor(buffered=True)
if ('user_type' in session and session['user_type'] == 'student'):
@@ -167,21 +173,24 @@ def chgPassword():
raise Exception('帳號已被使用
Username already used')
db = refresh_db()
cursor = db.cursor(buffered=True)
- if ('user_type' in session and session['user_type'] == 'student'):
- cursor.execute("UPDATE students SET password = %s WHERE email = %s", (genHash(request.form['new_password']), oldEmail))
- if (request.form['new_username'] != oldEmail and request.form['new_username'] != ''):
- cursor.execute("UPDATE students SET email = %s WHERE email = %s", (request.form['new_username'], oldEmail))
+ if 'user_type' in session and session['user_type'] == 'student':
+ cursor.execute("UPDATE students SET password = %s WHERE email = %s",
+ (genHash(request.form['new_password']), oldEmail))
+ if request.form['new_username'] != oldEmail and request.form['new_username'] != '':
+ cursor.execute("UPDATE students SET email = %s WHERE email = %s",
+ (request.form['new_username'], oldEmail))
else:
cursor.execute("UPDATE users SET password = %s WHERE email = %s", (
genHash(request.form['new_password']), oldEmail))
- if (request.form['new_username'] != oldEmail and request.form['new_username'] != ''):
- cursor.execute("UPDATE users SET email = %s WHERE email = %s", (request.form['new_username'], oldEmail))
+ if request.form['new_username'] != oldEmail and request.form['new_username'] != '':
+ cursor.execute("UPDATE users SET email = %s WHERE email = %s",
+ (request.form['new_username'], oldEmail))
db.commit()
cursor.close()
session.clear()
- if (request.form['new_username'] != oldEmail and request.form['new_username'] != ''):
+ if request.form['new_username'] != oldEmail and request.form['new_username'] != '':
send_email(oldEmail, "Email Changed 信箱已更改",
- """