mirror of
https://codeberg.org/Mo8it/AdvLabDB.git
synced 2024-11-08 21:21:06 +00:00
Clean up
This commit is contained in:
parent
1aa6737cd0
commit
2d268811de
2 changed files with 10 additions and 128 deletions
|
@ -2,62 +2,12 @@ from flask import redirect, render_template, request, url_for, flash
|
|||
from flask_security import auth_required, current_user
|
||||
|
||||
from advlabdb import app
|
||||
from advlabdb.models import Semester
|
||||
from advlabdb.utils import setUserActiveSemester, titleToTemplate, userActiveSemester
|
||||
from advlabdb.utils import setUserActiveSemester, userActiveSemester
|
||||
|
||||
|
||||
@app.context_processor
|
||||
def util_processor():
|
||||
def semesterDropDownItems():
|
||||
semesters = Semester.query.all()
|
||||
items = []
|
||||
for semester in semesters:
|
||||
items.append(
|
||||
'<li><a class="dropdown-item" href="'
|
||||
+ url_for("set_semester")
|
||||
+ "?semester_id="
|
||||
+ str(semester.id)
|
||||
+ '">'
|
||||
+ semester.label
|
||||
+ "</a></li>"
|
||||
)
|
||||
return items
|
||||
|
||||
def navbarItems(title):
|
||||
adminPages = []
|
||||
assistantPages = []
|
||||
|
||||
if current_user.is_authenticated:
|
||||
roleNames = [role.name for role in current_user.roles]
|
||||
if "assistant" in roleNames:
|
||||
pages = assistantPages
|
||||
if "admin" in roleNames:
|
||||
for p in adminPages:
|
||||
if p not in pages:
|
||||
pages.append(p)
|
||||
elif "admin" in roleNames:
|
||||
pages = adminPages
|
||||
|
||||
pages.append("Logout")
|
||||
else:
|
||||
pages = ["Login"]
|
||||
|
||||
items = []
|
||||
for page in pages:
|
||||
if page == title:
|
||||
active = " active"
|
||||
else:
|
||||
active = ""
|
||||
|
||||
items.append('<a class="nav-link' + active + '" href="' + titleToTemplate(page) + '">' + page + "</a>")
|
||||
return items
|
||||
|
||||
return dict(
|
||||
semesterDropDownItems=semesterDropDownItems,
|
||||
userActiveSemester=userActiveSemester,
|
||||
navbarItems=navbarItems,
|
||||
current_user=current_user,
|
||||
)
|
||||
return dict(userActiveSemester=userActiveSemester)
|
||||
|
||||
|
||||
@app.route("/")
|
||||
|
|
|
@ -8,78 +8,14 @@ from advlabdb import db
|
|||
from advlabdb.models import Semester
|
||||
|
||||
|
||||
def makeTable(headerAndDataList, rows, tableId="table"):
|
||||
def cellString(cell):
|
||||
cell = str(cell)
|
||||
if cell == "[]":
|
||||
return "<em>None</em>"
|
||||
|
||||
excludeChars = """<>"'[]"""
|
||||
for c in excludeChars:
|
||||
if c in cell:
|
||||
cell = cell.replace(c, "")
|
||||
return cell
|
||||
|
||||
def td(cell):
|
||||
return "<td>" + cellString(cell) + "</td>"
|
||||
|
||||
def th(cell):
|
||||
return '<th data-sortable="true" data-field="' + titleToTemplate(cell) + '">' + cellString(cell) + "</th>"
|
||||
|
||||
table = (
|
||||
'''<table
|
||||
id="'''
|
||||
+ tableId
|
||||
+ """"
|
||||
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"
|
||||
data-click-to-select="true"
|
||||
data-show-export="true"
|
||||
data-export-types="['json', 'xml', 'csv', 'txt', 'sql', 'pdf']">
|
||||
<thead>
|
||||
<tr>
|
||||
<th data-field="state" data-checkbox="true"></th>"""
|
||||
)
|
||||
for i in headerAndDataList:
|
||||
table += th(i[0])
|
||||
table += """</tr>
|
||||
</thead>
|
||||
<tbody>\n"""
|
||||
|
||||
for row in rows:
|
||||
table += "<tr><td></td>"
|
||||
|
||||
for i in headerAndDataList:
|
||||
table += td(eval(i[1]))
|
||||
|
||||
table += "</tr>\n"
|
||||
|
||||
table += """</tbody>
|
||||
</table>
|
||||
<br>
|
||||
<hr>
|
||||
<br>"""
|
||||
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
|
||||
lastSemesterId = (
|
||||
Semester.query.order_by(Semester.year).order_by(Semester.id.desc()).first().id
|
||||
) # TODO: Check orders
|
||||
if current_user.active_semester_id is None:
|
||||
current_user.active_semester_id = lastSemesterId
|
||||
db.session.commit()
|
||||
|
@ -87,23 +23,19 @@ def userActiveSemester(flashWarning=False):
|
|||
activeSemester = Semester.query.get(current_user.active_semester_id)
|
||||
if activeSemester:
|
||||
if flashWarning:
|
||||
flash(f"You are in the old semester {activeSemester.label}!", "warning")
|
||||
flash(f"You are in the old semester {activeSemester.repr()}!", "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 setUserActiveSemester(semesterId):
|
||||
if current_user.active_semester_id != semesterId:
|
||||
if Semester.query.get(semesterId):
|
||||
semester = Semester.query.get(semesterId)
|
||||
if semester:
|
||||
try:
|
||||
current_user.active_semester_id = semesterId
|
||||
db.session.commit()
|
||||
|
@ -112,6 +44,6 @@ def setUserActiveSemester(semesterId):
|
|||
|
||||
db.session.rollback()
|
||||
else:
|
||||
flash(f"Active semester changed to {Semester.query.get(semesterId).label}!", "warning")
|
||||
flash(f"Active semester changed to {semester.repr()}!", "warning")
|
||||
else:
|
||||
flash(f"No semester with ID {semesterId}!", "error")
|
||||
|
|
Loading…
Reference in a new issue