def getProjectsTableData(): if not utils.check_user_lab_admin(): return redirect(url_for('login')) try: sort = request.args.get('sort') order = request.args.get('order') or "desc" limit = request.args.get('limit') or 10 offset = request.args.get('offset') or 0 filters = request.args.get('filter') lab = None if utils.check_user_admin() else database.getLabByAcronym( flask_login.current_user.userId).id totalResults, results = database.getProjectsTableData( sort, order, limit, offset, filters, lab) rows = [] for result in results: supervisors = database.getProjectById( result.id).supervisorsFullNameEng lab = None if result.lab: lab = database.getLabById(result.lab).acronym rows.append({ "image": f"<img style='width:80px;height:70px;' src='/static/images/projects/{result.image}' alt='{result.image}'" if result.image else "", "year": result.year, "semester": result.semester, "title": result.title, "status": result.status, "supervisorsNames": ",<br>".join(supervisors), "lab": lab, "btnEdit": f"<button type='button' onclick='getProjectData({result.id})' name='btnEdit' class='btn btn-primary' data-toggle='modal' data-target='#editProjectModal'><i class='fa fa-edit fa-fw'></i> Edit</button>", "btnDelete": f"<button type='button' onclick='deleteProject({result.id})' name='btnDelete' class='btn btn-danger' data-toggle='modal' data-target='#deleteProjectModal'><i class='fa fa-trash fa-fw'></i> Delete</button>" }) # get filters options for the table filterOptions = database.getProjectsTableFilters() return jsonify(total=totalResults, rows=rows, filterOptions=filterOptions) except Exception as e: app.logger.error('In getProjectsTableData, error is: {}\n{}'.format( e, traceback.format_exc())) return jsonify(total=0, rows=[])
def getProjectsTableDataWithMails(): if not utils.check_user_lab_admin(): return redirect(url_for('login')) try: sort = request.args.get('sort') order = request.args.get('order') or "desc" limit = request.args.get('limit') or 10 offset = request.args.get('offset') or 0 filters = request.args.get('filter') lab = None if utils.check_user_admin() else database.getLabByAcronym( flask_login.current_user.userId).id totalResults, results = database.getProjectsTableData( sort, order, limit, offset, filters, lab) rows = [] for result in results: project = database.getProjectById(result.id) supervisors = project.supervisorsFullNameEng studentsMail = [s.email for s in project.students] lab = None if result.lab: lab = database.getLabById(result.lab).acronym rows.append({ "image": f"<img style='width:80px;height:70px;' src='/static/images/projects/{result.image}' alt='{result.image}'" if result.image else "", "year": result.year, "semester": result.semester, "title": result.title, "supervisorsNames": ",<br>".join(supervisors), "lab": lab, "id": result.id, "studentsMail": studentsMail }) # get filters options for the table filterOptions = database.getProjectsTableFilters() return jsonify(total=totalResults, rows=rows, filterOptions=filterOptions) except Exception as e: app.logger.error('In getProjectsTableData, error is: {}\n{}'.format( e, traceback.format_exc())) return jsonify(total=0, rows=[])
def deleteLab(): if not flask_login.current_user.is_authenticated or flask_login.current_user.userType != "admin": return redirect(url_for('login')) try: deleteForm = deleteLabForm() lab = database.getLabById(deleteForm.deleteLabId.data) if lab: app.logger.info('In deleteLab, deleting {}'.format(lab)) database.deleteLab(lab.id) flash('Lab was deleted successfully!', 'primary') else: app.logger.error( 'In deleteLab, could not delete lab with id {}, because there is no lab with this id'.format( deleteForm.deleteLabId.data)) flash("Error: can't delete, lab id {} is not in the db".format(deleteForm.deleteLabId.data), 'danger') return redirect(url_for('manageLabs')) except Exception as e: app.logger.error('In deleteLab, Error is: {}'.format(e)) return redirect(url_for('errorPage'))
def getLabData(id): if not flask_login.current_user.is_authenticated or flask_login.current_user.userType != "admin": return redirect(url_for('login')) try: lab = database.getLabById(id) labData = { "id": lab.id, "name": lab.name, "acronym": lab.acronym, "logo": lab.logo, "description": lab.description, "password": lab.password, "website": lab.website } return jsonify(labData) except Exception as e: app.logger.error('In getLabData, error is: {}\n{}'.format(e, traceback.format_exc())) return jsonify({})
def getCoursesTableData(): if not utils.check_user_lab_admin(): return redirect(url_for('login')) try: sort = request.args.get('sort') order = request.args.get('order') or "desc" limit = request.args.get('limit') or 10 offset = request.args.get('offset') or 0 filters = request.args.get('filter') totalResults, results = database.getCoursesTableData( sort, order, limit, offset, filters) rows = [] for result in results: lab = None if result.lab: lab = database.getLabById(result.lab).acronym rows.append({ "name": result.name, "number": result.number, "lab": lab, "btnEdit": f"<button type='button' onclick='getCourseData({result.id})' name='btnEdit' class='btn btn-primary' data-toggle='modal' data-target='#editCourseModal'><i class='fa fa-edit fa-fw'></i> Edit</button>", "btnDelete": f"<button type='button' onclick='deleteCourse({result.id})' name='btnDelete' class='btn btn-danger' data-toggle='modal' data-target='#deleteCourseModal'><i class='fa fa-trash fa-fw'></i> Delete</button>" }) return jsonify(total=totalResults, rows=rows, filterOptions={"lab": database.getLabFilter()}) except Exception as e: app.logger.error('In getCoursesTableData, error is: {}\n{}'.format( e, traceback.format_exc())) return jsonify(total=0, rows=[])
def manageLabs(): if not flask_login.current_user.is_authenticated or flask_login.current_user.userType != "admin": return redirect(url_for('login')) try: admin = utils.check_user_admin() lab = None if not utils.check_user_lab() else database.getLabByAcronym(flask_login.current_user.userId) addForm = addLabForm() editForm = editLabForm() deleteForm = deleteLabForm() addFormErrors = False editFormErrorLabId = '' if (request.method=='POST'): formName = request.form['sentFormName'] if formName == 'editLabForm': lab = database.getLabById(editForm.labId.data) if not lab: app.logger.error('In manageLabs, in editForm, tried to edit a lab with id {} that does not exist in the db'.format(editForm.labId.data)) flash("Error: Lab with id {} is not in the db.".format(editForm.labId.data), 'danger') return redirect(url_for('manageLabs')) if editForm.validate_on_submit(): if lab.acronym != editForm.new_acronym.data: labWithAcr = database.getLabByAcronym(editForm.new_acronym.data) if labWithAcr: flash('There is already a lab with the same acronym!', 'danger') return redirect(url_for('editAccount')) projectImage = lab.logo if editForm.new_logo.data: app.logger.info('In manageProjects, in editForm, deleting old project image') utils.delete_logo_image(projectImage) # TODO CHANGE THIS FUNCTIONS projectImage = utils.save_form_image(editForm.new_logo.data, "labs_logo") hashed_password = bcrypt.generate_password_hash(editForm.new_password.data).decode('utf-8') database.updateLab(lab.id,{ "name": editForm.new_name.data, "acronym": editForm.new_acronym.data, "password": hashed_password, "description": editForm.description.data, "website": editForm.website.data, "logo": projectImage }) flash('Lab was updated successfully!', 'success') return redirect(url_for('manageLabs')) else: app.logger.info( 'In managelabs, editForm is NOT valid. editForm.errors: {}'.format(editForm.errors)) editFormErrorLabId = editForm.labId.data if 'csrf_token' in editForm.errors: flash('Error: csrf token expired, please re-send the form.', 'danger') else: flash('There was an error, see details below.', 'danger') elif formName == 'addLabForm': if addForm.validate_on_submit(): picFile = None if addForm.logo.data: app.logger.info('In manageLabs, saving image of new lab logo') picFile = utils.save_form_image(addForm.logo.data, "labs_logo") hashed_password = bcrypt.generate_password_hash(addForm.new_password.data).decode('utf-8') newLab = { "name": addForm.new_name.data, "acronym": addForm.new_acronym.data, "password": hashed_password, "description": addForm.description.data, "website": addForm.website.data, "logo": picFile } database.addLab(newLab) flash('Lab was created successfully!', 'success') return redirect(url_for('manageLabs')) else: addFormErrors = True app.logger.info('In manageLabs, addForm is NOT valid. addForm.errors:{}'.format(addForm.errors)) if 'csrf_token' in addForm.errors: flash('Error: csrf token expired, please re-send the form.', 'danger') else: flash('There was an error, see details below.', 'danger') return render_template('/admin/labs.html', title="Manage Labs", addForm=addForm, editForm=editForm, deleteForm=deleteForm, addFormErrors=addFormErrors, editFormErrorLabId=editFormErrorLabId, admin=admin, lab=lab) except Exception as e: app.logger.error('In manageLabs, Error is: {}\n{}'.format(e, traceback.format_exc())) return redirect(url_for('errorPage'))