from random import choice
from string import ascii_letters, digits
from flask import flash
from flask_security import current_user
from advlabdb import db
from advlabdb.models import Semester
def makeTable(headerAndDataList, rows, tableId="table"):
def cellString(cell):
cell = str(cell)
if cell == "[]":
return "None"
excludeChars = """<>"'[]"""
for c in excludeChars:
if c in cell:
cell = cell.replace(c, "")
return cell
def td(cell):
return "
" + cellString(cell) + " | "
def th(cell):
return '' + cellString(cell) + " | "
table = (
'''
| """
)
for i in headerAndDataList:
table += th(i[0])
table += """
\n"""
for row in rows:
table += " | "
for i in headerAndDataList:
table += td(eval(i[1]))
table += "
\n"
table += """
"""
return table
def appointmentDate(date):
return date.strftime("%a %d.%m.%Y")
def randomPassword():
return "".join(choice(ascii_letters + digits) for i in range(12))
def titleToTemplate(page):
return page.lower().replace(" ", "_")
def userActiveSemester(flashWarning=False):
lastSemesterId = Semester.query.all()[-1].id
if current_user.active_semester_id is None:
current_user.active_semester_id = lastSemesterId
db.session.commit()
elif current_user.active_semester_id != lastSemesterId:
activeSemester = Semester.query.get(current_user.active_semester_id)
if activeSemester:
if flashWarning:
flash(f"You are in the old semester {activeSemester.label}!", "warning")
else:
current_user.active_semester_id = lastSemesterId
db.session.commit()
flash("Semester changed!", "warning")
return Semester.query.get(current_user.active_semester_id)
def partFromLabelInUserActiveSemester(partLabel):
for part in userActiveSemester().parts:
if part.label == partLabel:
return part
def setActiveSemester(semesterId):
try:
current_user.active_semester_id = Semester.query.get(int(semesterId)).id
except Exception as err:
flash(str(err))
return False
db.session.commit()
flash(f"Active semester changed to {Semester.query.get(semesterId).label}!", "warning")
return True