2021-04-01 12:08:42 +00:00
|
|
|
from random import choice
|
2021-06-02 21:43:41 +00:00
|
|
|
from string import ascii_letters, digits
|
|
|
|
|
2021-04-27 13:50:03 +00:00
|
|
|
from flask import flash
|
2021-06-02 21:43:41 +00:00
|
|
|
from flask_security import current_user
|
2021-04-26 19:31:15 +00:00
|
|
|
|
2021-04-27 21:28:47 +00:00
|
|
|
from advlabdb import db
|
2021-06-02 21:43:41 +00:00
|
|
|
from advlabdb.models import Semester
|
2021-03-19 12:41:52 +00:00
|
|
|
|
2021-05-17 20:36:24 +00:00
|
|
|
|
2021-04-18 15:24:40 +00:00
|
|
|
def makeTable(headerAndDataList, rows, tableId="table"):
|
2021-03-19 17:09:07 +00:00
|
|
|
def cellString(cell):
|
2021-03-19 20:15:43 +00:00
|
|
|
cell = str(cell)
|
2021-03-20 14:41:22 +00:00
|
|
|
if cell == "[]":
|
|
|
|
return "<em>None</em>"
|
|
|
|
|
2021-03-19 20:15:43 +00:00
|
|
|
excludeChars = """<>"'[]"""
|
|
|
|
for c in excludeChars:
|
|
|
|
if c in cell:
|
|
|
|
cell = cell.replace(c, "")
|
|
|
|
return cell
|
2021-03-19 17:09:07 +00:00
|
|
|
|
2021-03-19 15:30:02 +00:00
|
|
|
def td(cell):
|
2021-06-02 21:43:41 +00:00
|
|
|
return "<td>" + cellString(cell) + "</td>"
|
2021-03-19 12:41:52 +00:00
|
|
|
|
2021-03-19 15:30:02 +00:00
|
|
|
def th(cell):
|
2021-06-02 21:43:41 +00:00
|
|
|
return '<th data-sortable="true" data-field="' + titleToTemplate(cell) + '">' + cellString(cell) + "</th>"
|
2021-03-19 17:09:07 +00:00
|
|
|
|
2021-06-02 21:43:41 +00:00
|
|
|
table = (
|
|
|
|
'''<table
|
|
|
|
id="'''
|
|
|
|
+ tableId
|
|
|
|
+ """"
|
2021-03-19 17:09:07 +00:00
|
|
|
data-classes="table table-bordered table-striped"
|
|
|
|
data-toggle="table"
|
|
|
|
data-thead-classes="table-dark"
|
|
|
|
data-search="true"
|
|
|
|
data-show-toggle="true"
|
|
|
|
data-show-columns="true"
|
2021-04-17 00:19:38 +00:00
|
|
|
data-click-to-select="true"
|
2021-03-19 17:09:07 +00:00
|
|
|
data-show-export="true"
|
|
|
|
data-export-types="['json', 'xml', 'csv', 'txt', 'sql', 'pdf']">
|
|
|
|
<thead>
|
2021-04-17 00:19:38 +00:00
|
|
|
<tr>
|
2021-06-02 21:43:41 +00:00
|
|
|
<th data-field="state" data-checkbox="true"></th>"""
|
|
|
|
)
|
2021-03-19 15:30:02 +00:00
|
|
|
for i in headerAndDataList:
|
|
|
|
table += th(i[0])
|
2021-06-02 21:43:41 +00:00
|
|
|
table += """</tr>
|
2021-03-19 17:09:07 +00:00
|
|
|
</thead>
|
2021-06-02 21:43:41 +00:00
|
|
|
<tbody>\n"""
|
2021-03-19 15:30:02 +00:00
|
|
|
|
|
|
|
for row in rows:
|
2021-06-02 21:43:41 +00:00
|
|
|
table += "<tr><td></td>"
|
2021-03-19 15:30:02 +00:00
|
|
|
|
|
|
|
for i in headerAndDataList:
|
|
|
|
table += td(eval(i[1]))
|
|
|
|
|
2021-06-02 21:43:41 +00:00
|
|
|
table += "</tr>\n"
|
2021-03-19 15:30:02 +00:00
|
|
|
|
2021-06-02 21:43:41 +00:00
|
|
|
table += """</tbody>
|
2021-03-19 17:09:07 +00:00
|
|
|
</table>
|
|
|
|
<br>
|
|
|
|
<hr>
|
2021-06-02 21:43:41 +00:00
|
|
|
<br>"""
|
2021-03-19 15:30:02 +00:00
|
|
|
return table
|
2021-03-19 20:15:43 +00:00
|
|
|
|
2021-04-26 19:31:15 +00:00
|
|
|
|
2021-03-19 20:15:43 +00:00
|
|
|
def appointmentDate(date):
|
|
|
|
return date.strftime("%a %d.%m.%Y")
|
2021-03-31 23:04:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def randomPassword():
|
2021-06-02 21:43:41 +00:00
|
|
|
return "".join(choice(ascii_letters + digits) for i in range(12))
|
2021-04-01 12:08:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
def titleToTemplate(page):
|
|
|
|
return page.lower().replace(" ", "_")
|
2021-04-26 19:31:15 +00:00
|
|
|
|
|
|
|
|
2021-05-17 14:55:04 +00:00
|
|
|
def userActiveSemester(flashWarning=False):
|
2021-04-26 19:31:15 +00:00
|
|
|
lastSemesterId = Semester.query.all()[-1].id
|
|
|
|
if current_user.active_semester_id is None:
|
|
|
|
current_user.active_semester_id = lastSemesterId
|
2021-04-27 13:50:03 +00:00
|
|
|
db.session.commit()
|
2021-04-26 19:31:15 +00:00
|
|
|
elif current_user.active_semester_id != lastSemesterId:
|
|
|
|
activeSemester = Semester.query.get(current_user.active_semester_id)
|
|
|
|
if activeSemester:
|
2021-05-17 14:55:04 +00:00
|
|
|
if flashWarning:
|
|
|
|
flash(f"You are in the old semester {activeSemester.label}!", "warning")
|
2021-04-26 19:31:15 +00:00
|
|
|
else:
|
|
|
|
current_user.active_semester_id = lastSemesterId
|
2021-04-27 13:50:03 +00:00
|
|
|
db.session.commit()
|
|
|
|
flash("Semester changed!", "warning")
|
2021-04-26 19:31:15 +00:00
|
|
|
return Semester.query.get(current_user.active_semester_id)
|
2021-04-27 13:50:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def partFromLabelInUserActiveSemester(partLabel):
|
|
|
|
for part in userActiveSemester().parts:
|
|
|
|
if part.label == partLabel:
|
|
|
|
return part
|
2021-05-18 12:54:30 +00:00
|
|
|
|
|
|
|
|
2021-06-25 23:59:21 +00:00
|
|
|
def setUserActiveSemester(semesterId):
|
2021-07-11 11:48:13 +00:00
|
|
|
if current_user.active_semester_id != semesterId:
|
|
|
|
if Semester.query.get(semesterId):
|
|
|
|
try:
|
|
|
|
current_user.active_semester_id = semesterId
|
|
|
|
db.session.commit()
|
|
|
|
except Exception as ex:
|
|
|
|
flash(str(ex), "error")
|
|
|
|
|
|
|
|
db.session.rollback()
|
|
|
|
else:
|
|
|
|
flash(f"Active semester changed to {Semester.query.get(semesterId).label}!", "warning")
|
|
|
|
else:
|
|
|
|
flash(f"No semester with ID {semesterId}!", "error")
|