Attendance/app.py

192 lines
6.7 KiB
Python
Raw Permalink Normal View History

2021-10-06 07:44:08 -07:00
from functions import *
2021-12-26 05:08:50 -08:00
from manage.manage import manage
2021-09-26 06:37:34 -07:00
from upload import upload
2021-12-12 02:38:10 -08:00
from login import login
2021-12-27 06:34:19 -08:00
load_dotenv()
2021-09-09 06:29:31 -07:00
app = Flask(__name__)
2021-12-12 02:38:10 -08:00
babel = Babel(app)
2021-09-26 06:37:34 -07:00
app.register_blueprint(manage)
app.register_blueprint(upload)
2021-12-12 02:38:10 -08:00
app.register_blueprint(login)
2021-09-09 06:29:31 -07:00
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
2021-12-27 06:34:19 -08:00
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://' + os.environ.get('MYSQL_USER') + ':' + os.environ.get(
'MYSQL_PASSWORD') + '@' + os.environ.get('MYSQL_HOST') + '/attendance'
2021-12-12 02:38:10 -08:00
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['BABEL_DEFAULT_LOCALE'] = 'zh_TW'
app.jinja_env.add_extension('jinja2.ext.loopcontrols')
sdb = SQLAlchemy(app)
2021-12-27 06:34:19 -08:00
2021-12-12 02:38:10 -08:00
class DefaultModelView(ModelView):
restricted = True
2021-12-27 06:34:19 -08:00
2021-12-12 02:38:10 -08:00
def __init__(self, model, session, restricted=True, name=None, category=None, endpoint=None, url=None, **kwargs):
self.restricted = restricted
2021-12-26 05:08:50 -08:00
self.column_default_sort = ('id', True)
2021-12-12 02:38:10 -08:00
for k, v in kwargs.items():
setattr(self, k, v)
setattr(self, 'can_export', True)
super(DefaultModelView, self).__init__(model, session, name=name, category=category, endpoint=endpoint, url=url)
2021-12-27 06:34:19 -08:00
2021-12-12 02:38:10 -08:00
def is_accessible(self):
2021-12-27 06:34:19 -08:00
if self.restricted:
return (not check_login_status()) and is_admin() and check_permission()
return (not check_login_status()) and is_admin()
2021-12-12 02:38:10 -08:00
def inaccessible_callback(self, name, **kwargs):
return redirect('/')
2021-12-27 06:34:19 -08:00
2021-12-12 02:38:10 -08:00
class MyAdminIndexView(AdminIndexView):
def is_accessible(self):
2021-12-27 06:34:19 -08:00
return (not check_login_status()) and is_admin()
2021-12-12 02:38:10 -08:00
def inaccessible_callback(self, name, **kwargs):
return redirect('/')
2021-09-09 06:29:31 -07:00
2021-12-27 06:34:19 -08:00
2021-12-12 02:38:10 -08:00
admin = Admin(
2021-12-27 06:34:19 -08:00
app,
name='Attendance 點名系統 後台管理',
template_mode='bootstrap3',
index_view=MyAdminIndexView(),
)
2021-12-12 02:38:10 -08:00
class Users(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
email = sdb.Column(sdb.Text)
name = sdb.Column(sdb.Text)
oldUsername = sdb.Column(sdb.Text)
role = sdb.Column(sdb.CHAR)
password = sdb.Column(sdb.Text)
2021-12-27 06:34:19 -08:00
2021-12-12 02:38:10 -08:00
class Students(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
email = sdb.Column(sdb.INT)
grade = sdb.Column(sdb.INT)
class_ = sdb.Column(sdb.INT)
num = sdb.Column(sdb.INT)
name = sdb.Column(sdb.Text)
ename = sdb.Column(sdb.Text)
classes = sdb.Column(sdb.Text)
password = sdb.Column(sdb.Text)
2021-12-27 06:34:19 -08:00
2021-12-12 02:38:10 -08:00
class Schedule(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
grade = sdb.Column(sdb.INT)
class_ = sdb.Column(sdb.INT)
dow = sdb.Column(sdb.INT)
period = sdb.Column(sdb.CHAR)
subject = sdb.Column(sdb.Text)
teacher = sdb.Column(sdb.Text)
2021-12-27 06:34:19 -08:00
2021-12-12 02:38:10 -08:00
class SpecSchedule(sdb.Model):
__tablename__ = 'specschedule'
id = sdb.Column(sdb.INT, primary_key=True)
grade = sdb.Column(sdb.INT)
class_ = sdb.Column(sdb.INT)
date = sdb.Column(sdb.VARCHAR(11))
period = sdb.Column(sdb.CHAR)
subject = sdb.Column(sdb.Text)
teacher = sdb.Column(sdb.Text)
2021-12-27 06:34:19 -08:00
2021-12-12 02:38:10 -08:00
class GPClasses(sdb.Model):
__tablename__ = 'gpclasses'
id = sdb.Column(sdb.INT, primary_key=True)
category = sdb.Column(sdb.Text)
subclass = sdb.Column(sdb.Text)
about = sdb.Column(sdb.Text)
accs = sdb.Column(sdb.Text)
2021-12-27 06:34:19 -08:00
2021-12-12 02:38:10 -08:00
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)
2021-12-27 06:34:19 -08:00
2021-12-12 02:38:10 -08:00
class Submission(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
grade = sdb.Column(sdb.INT)
class_ = sdb.Column(sdb.INT)
date = sdb.Column(sdb.VARCHAR(11))
period = sdb.Column(sdb.CHAR)
signature = sdb.Column(sdb.Text)
ds1 = sdb.Column(sdb.INT)
ds2 = sdb.Column(sdb.INT)
ds3 = sdb.Column(sdb.INT)
ds4 = sdb.Column(sdb.INT)
ds5 = sdb.Column(sdb.INT)
ds6 = sdb.Column(sdb.INT)
ds7 = sdb.Column(sdb.INT)
notes = sdb.Column(sdb.Text)
2021-12-27 06:34:19 -08:00
2021-12-12 02:38:10 -08:00
class DS(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
grade = sdb.Column(sdb.INT)
class_ = sdb.Column(sdb.INT)
num = sdb.Column(sdb.INT)
date = sdb.Column(sdb.VARCHAR(11))
period = sdb.Column(sdb.CHAR)
note = sdb.Column(sdb.Text)
status = sdb.Column(sdb.CHAR, default='X')
2021-12-27 06:34:19 -08:00
2021-12-12 02:38:10 -08:00
class Dates(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
date = sdb.Column(sdb.VARCHAR(11))
dow = sdb.Column(sdb.INT)
2021-12-27 06:34:19 -08:00
2021-12-12 02:38:10 -08:00
class Absent(sdb.Model):
id = sdb.Column(sdb.INT, primary_key=True)
grade = sdb.Column(sdb.INT)
class_ = sdb.Column(sdb.INT)
num = sdb.Column(sdb.INT)
date = sdb.Column(sdb.VARCHAR(11))
period = sdb.Column(sdb.CHAR)
status = sdb.Column(sdb.CHAR)
note = sdb.Column(sdb.Text)
2021-12-27 06:34:19 -08:00
2021-12-12 02:38:10 -08:00
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))
2021-12-27 06:34:19 -08:00
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']))
2021-12-12 02:38:10 -08:00
admin.add_link(MenuLink(name='Back to Home 返回一般管理', category='', url='/manage'))
admin.add_link(MenuLink(name='Logout 登出', category='', url='/logout'))
2021-09-09 06:29:31 -07:00
if __name__ == '__main__':
2021-12-26 05:08:50 -08:00
app.run(debug=True, host='0.0.0.0', port=5050)