def editLab(): if not utils.check_user_lab(): 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) form = editLabForm() if request.method == 'POST': if form.validate_on_submit(): if lab.acronym != form.new_acronym.data: labWithAcr = database.getLabByAcronym(form.new_acronym.data) if labWithAcr: flash('There is already a lab with the same acronym!', 'danger') return redirect(url_for('editLab')) projectImage = lab.logo if form.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(form.new_logo.data, "labs_logo") hashed_password = bcrypt.generate_password_hash(form.new_password.data).decode('utf-8') database.updateLab(lab.id, { "name": form.new_name.data, "acronym": form.new_acronym.data, "password": hashed_password, "description": form.description.data, "website": form.website.data, "logo": projectImage }) flash('Lab was updated successfully!', 'success') return redirect(url_for('home')) else: app.logger.info('In Edit Account, form is NOT valid. form.errors:{}'.format(form.errors)) if 'csrf_token' in form.errors: flash('Error: csrf token expired, please re-enter your credentials.', 'danger') else: flash('There was an error, see details below.', 'danger') elif request.method == 'GET': form.labId.data = lab.id form.new_name.data = lab.name form.new_acronym.data = lab.acronym form.new_password.data = lab.password form.new_logo.data = lab.logo form.website.data = lab.website form.description.data = lab.description return render_template('/admin/editLab.html', title="Edit Lab", form=form, admin=admin, lab=lab) except Exception as e: app.logger.error('In editAccount, Error is: {}\n{}'.format(e, traceback.format_exc())) return redirect(url_for('errorPage'))
def manageProjects(): if not utils.check_user_lab_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) courses = database.getAllCourses() addForm = addProjectForm() editForm = editProjectForm() deleteForm = deleteProjectForm() addFormErrors = False editFormErrorProjectId = '' edit_studentForm = editStudentForm() currentSemester = utils.getRegistrationSemester() currentYear = utils.getRegistrationYear() semesterChoices = [("Winter", "Winter"), ("Spring", "Spring")] if currentSemester == "Spring": semesterChoices.reverse() addForm.new_title.choices = [ (str(s.id), s.title) for s in database.getAllProposedProjects() ] addForm.new_year.choices = [ (currentYear, currentYear), (str(int(currentYear) + 1), str(int(currentYear) + 1)), (str(int(currentYear) + 2), str(int(currentYear) + 2)) ] addForm.new_semester.choices = semesterChoices allSupervisors = database.getAllSupervisors() supervisorsChoices = [(str(s.id), s.firstNameEng + " " + s.lastNameEng) for s in allSupervisors] supervisorsChoices.insert(0, ('', '')) addForm.new_supervisor1.choices = supervisorsChoices addForm.new_supervisor2.choices = supervisorsChoices addForm.new_supervisor3.choices = supervisorsChoices editForm.year.choices = [ (currentYear, currentYear), (str(int(currentYear) + 1), str(int(currentYear) + 1)), (str(int(currentYear) + 2), str(int(currentYear) + 2)) ] editForm.semester.choices = semesterChoices editForm.supervisor1.choices = supervisorsChoices editForm.supervisor2.choices = supervisorsChoices editForm.supervisor3.choices = supervisorsChoices # get Labs allLabs = database.getAllLabs() allLabsChoices = [(str(l.id), l.acronym) for l in allLabs] editForm.lab.choices = allLabsChoices addForm.new_lab.choices = allLabsChoices if (request.method == 'POST'): formName = request.form['sentFormName'] if formName == 'editProjectForm': project = database.getProjectById(editForm.projectId.data) if not project: app.logger.error( 'In manageProjects, in editForm, tried to edit a project with id {} that does not exist in the db' .format(editForm.projectId.data)) flash( "Error: project with id {} is not in the db.".format( editForm.projectId.data), 'danger') return redirect(url_for('manageProjects')) app.logger.error("this is the students: {}".format( request.form)) if editForm.validate_on_submit(): studentsIds = request.form.getlist("students") studentsCoursesIds = request.form.getlist( "studentsCoursesIds") if studentsIds and not studentsCoursesIds: flash( "Error: students can't be added to a project without a course number.", 'danger') return redirect(url_for('manageProjects')) projectImage = project.image if editForm.image.data: # delete old image if exists app.logger.info( 'In manageProjects, in editForm, deleting old project image' ) utils.delete_project_image(projectImage) projectImage = utils.save_form_image( editForm.image.data, "projects") database.updateProject( project.id, { "title": editForm.title.data, "year": editForm.year.data, "semester": editForm.semester.data, "comments": editForm.comments.data, "grade": editForm.grade.data, "image": projectImage, "lab": editForm.lab.data }) # update students in project studentsInProject = [] for i in range(len(studentsIds)): studentsInProject.append({ "id": studentsIds[i], "courseId": studentsCoursesIds[i] }) database.updateProjectStudents(project.id, studentsInProject) # update supervisors in project supervisorsIds = set() if editForm.supervisor1.data: supervisorsIds.add(editForm.supervisor1.data) if editForm.supervisor2.data: supervisorsIds.add(editForm.supervisor2.data) if editForm.supervisor3.data: supervisorsIds.add(editForm.supervisor3.data) database.updateProjectSupervisors(project.id, supervisorsIds) # update status database.updateProjectStatus( project.id, { "requirementsDoc": editForm.requirementsDoc.data, "firstMeeting": editForm.firstMeeting.data, "halfwayPresentation": editForm.halfwayPresentation.data, "finalMeeting": editForm.finalMeeting.data, "projectReport": editForm.projectReport.data, "equipmentReturned": editForm.equipmentReturned.data, "projectDoc": editForm.projectDoc.data, "gradeStatus": editForm.gradeStatus.data }) flash('Project was updated successfully!', 'success') if request.form.get('studentsReferrer'): return redirect(url_for('manageStudents')) else: return redirect(url_for('manageProjects')) else: app.logger.info( 'In manageProjects, editForm is NOT valid. editForm.errors: {}' .format(editForm.errors)) editFormErrorProjectId = editForm.projectId.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') if request.form.get('studentsReferrer'): edit_StudentForm = editStudentForm() delete_StudentForm = deleteStudentForm() editFormErrorStudentId = '' return render_template( '/admin/students.html', title="Manage Students", editForm=edit_StudentForm, editProjectForm=editForm, courses=courses, deleteForm=delete_StudentForm, editFormErrorStudentId=editFormErrorStudentId, editProjectErrorId=editFormErrorProjectId) elif formName == 'addProjectForm': if addForm.validate_on_submit(): studentsIds = request.form.getlist("students") studentsCoursesIds = request.form.getlist( "studentsCoursesIds") if studentsIds and not studentsCoursesIds: flash( "Error: students can't be added to a project without a course number.", 'danger') return redirect(url_for('manageProjects')) # add new project projectTitle = dict(addForm.new_title.choices).get( addForm.new_title.data) newImageName = None # save project image matchingProposedProject = database.getProposedProjectByTitle( projectTitle) if matchingProposedProject: matchingImageName = matchingProposedProject.image if matchingImageName: newImageName = utils.copy_project_image_from_proposed_project( matchingImageName) newProject = { "title": projectTitle, "year": addForm.new_year.data, "semester": addForm.new_semester.data, "grade": addForm.new_grade.data, "comments": addForm.new_comments.data, "image": newImageName, "requirementsDoc": addForm.new_requirementsDoc.data, "firstMeeting": addForm.new_firstMeeting.data, "halfwayPresentation": addForm.new_halfwayPresentation.data, "finalMeeting": addForm.new_finalMeeting.data, "projectReport": addForm.new_projectReport.data, "equipmentReturned": addForm.new_equipmentReturned.data, "projectDoc": addForm.new_projectDoc.data, "gradeStatus": addForm.new_gradeStatus.data, "status": "הרשמה", "lab": addForm.new_lab.data } newProjectId = database.addProject(newProject) # add students to project studentsInProject = [] for i in range(len(studentsIds)): studentsInProject.append({ "id": studentsIds[i], "courseId": studentsCoursesIds[i] }) database.updateProjectStudents(newProjectId, studentsInProject) # add supervisors to project supervisorsIds = set() if addForm.new_supervisor1.data: supervisorsIds.add(addForm.new_supervisor1.data) if addForm.new_supervisor2.data: supervisorsIds.add(addForm.new_supervisor2.data) if addForm.new_supervisor3.data: supervisorsIds.add(addForm.new_supervisor3.data) database.updateProjectSupervisors(newProjectId, supervisorsIds) flash('Project was created successfully!', 'success') return redirect(url_for('manageProjects')) else: addFormErrors = True app.logger.info( 'In manageProjects, 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/projects.html', title="Manage Projects", courses=courses, addForm=addForm, editForm=editForm, deleteForm=deleteForm, addFormErrors=addFormErrors, editFormErrorProjectId=editFormErrorProjectId, editStudentForm=edit_studentForm, admin=admin, lab=lab) except Exception as e: app.logger.error('In manageProjects, Error is: {}\n{}'.format( e, traceback.format_exc())) return redirect(url_for('errorPage'))
def editAccount(): if not utils.check_user_student(): return redirect(url_for('login')) try: student = database.getStudentByStudentId( flask_login.current_user.userId) form = EditAccountForm() if request.method == 'POST': form.email.data = form.email.data.strip() if form.validate_on_submit(): if student.studentId != form.studentId.data: userWithSameId = database.getUserByUserId( form.studentId.data) if userWithSameId: flash('There is already a user with the same ID!', 'danger') return redirect(url_for('editAccount')) if student.email != form.email.data: studentWithSameEmail = database.getStudentByEmail( form.email.data) if studentWithSameEmail: flash('This email is already used by another student!', 'danger') return redirect(url_for('editAccount')) profilePic = student.profilePic if form.profilePic.data: # delete old profile image utils.delete_profile_image(profilePic) # save new profile image profilePic = utils.save_form_image(form.profilePic.data, "profile") hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') database.updateStudent( student.id, { "studentId": form.studentId.data, "password": hashed_password, "firstNameHeb": form.firstNameHeb.data, "lastNameHeb": form.lastNameHeb.data, "firstNameEng": form.firstNameEng.data.capitalize(), "lastNameEng": form.lastNameEng.data.capitalize(), "academicStatus": form.academicStatus.data, "faculty": form.faculty.data, "cellPhone": form.cellPhone.data, "email": form.email.data, "profilePic": profilePic }) # update userId in current session flask_login.current_user.userId = form.studentId.data app.logger.info( 'In Edit Account, commiting student changes. updated student will be: {}' .format(student)) flash('Your account was updated successfully!', 'success') return redirect(url_for('home')) else: app.logger.info( 'In Edit Account, form is NOT valid. form.errors:{}'. format(form.errors)) if 'csrf_token' in form.errors: flash( 'Error: csrf token expired, please re-enter your credentials.', 'danger') else: flash('There was an error, see details below.', 'danger') elif request.method == 'GET': form.studentId.data = student.studentId form.firstNameHeb.data = student.firstNameHeb form.lastNameHeb.data = student.lastNameHeb form.firstNameEng.data = student.firstNameEng form.lastNameEng.data = student.lastNameEng form.academicStatus.data = student.academicStatus form.faculty.data = student.faculty form.cellPhone.data = student.cellPhone form.email.data = student.email return render_template('editAccount.html', title="Edit Account", form=form, student=student) except Exception as e: app.logger.error('In editAccount, Error is: {}\n{}'.format( e, traceback.format_exc())) return redirect(url_for('errorPage'))
def register(): if flask_login.current_user.is_authenticated: return redirect(url_for('home')) try: form = RegistrationForm() projectTitleChoices = [('', 'NOT CHOSEN')] form.projectTitle.choices = projectTitleChoices registrationSemester = utils.getRegistrationSemester() registrationYear = utils.getRegistrationYear() form.semester.choices = [(registrationSemester, registrationSemester)] form.year.choices = [(registrationYear, registrationYear)] if (request.method == 'POST'): form.email.data = form.email.data.strip() if form.validate_on_submit(): picFile = None if form.profilePic.data: picFile = utils.save_form_image(form.profilePic.data, "profile") hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') database.registerStudent({ "studentId": form.studentId.data, "password": hashed_password, "firstNameHeb": form.firstNameHeb.data, "lastNameHeb": form.lastNameHeb.data, "firstNameEng": form.firstNameEng.data.capitalize(), "lastNameEng": form.lastNameEng.data.capitalize(), "academicStatus": form.academicStatus.data, "faculty": form.faculty.data, "cellPhone": form.cellPhone.data, "email": form.email.data, "semester": registrationSemester, "year": registrationYear, "profilePic": picFile }) flash('Account created successfully!', 'success') return redirect(url_for('login')) else: app.logger.info( 'In Register, form is NOT valid. form.errors:{}'.format( form.errors)) if 'csrf_token' in form.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('register.html', title="Registration", form=form) except Exception as e: app.logger.error('In register, Error is: {}\n{}'.format( e, traceback.format_exc())) return redirect(url_for('errorPage'))
def manageProposedProjects(): if not utils.check_user_lab_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 = addProposedProjectForm() editForm = editProposedProjectForm() deleteForm = deleteProposedProjectForm() addFormErrors = False editFormErrorProposedProjectId = '' # get supervisors allSupervisors = database.getAllSupervisors() activeSupervisors = database.getActiveSupervisors() allSupervisorsChoices = [(str(s.id), s.firstNameEng + " " + s.lastNameEng) for s in allSupervisors] activeSupervisorsChoices = [(str(s.id), s.firstNameEng + " " + s.lastNameEng) for s in activeSupervisors] allSupervisorsChoices.insert(0, ('', '')) activeSupervisorsChoices.insert(0, ('', '')) editForm.supervisor1.choices = allSupervisorsChoices editForm.supervisor2.choices = allSupervisorsChoices editForm.supervisor3.choices = allSupervisorsChoices addForm.newSupervisor1.choices = activeSupervisorsChoices addForm.newSupervisor2.choices = activeSupervisorsChoices addForm.newSupervisor3.choices = activeSupervisorsChoices # get Labs allLabs = database.getAllLabs() allLabsChoices = [(str(l.id), l.acronym) for l in allLabs] editForm.lab.choices = allLabsChoices addForm.newLab.choices = allLabsChoices if (request.method == 'POST'): formName = request.form['pageForm'] if formName == 'addProposedProjectForm': if addForm.validate_on_submit(): picFile = None if addForm.newImage.data: app.logger.info('In manageProposedProjects, saving image of new proposed project') picFile = utils.save_form_image(addForm.newImage.data, "proposed_projects") # create new proposed project newProposedProjectId = database.addProposedProject({ "title": addForm.newTitle.data, "description": addForm.newDescription.data, "lab": addForm.newLab.data, "image": picFile }) # save the supervisors for this proposed project supervisorsIds = set() if addForm.newSupervisor1.data: supervisorsIds.add(int(addForm.newSupervisor1.data)) if addForm.newSupervisor2.data: supervisorsIds.add(int(addForm.newSupervisor2.data)) if addForm.newSupervisor3.data: supervisorsIds.add(int(addForm.newSupervisor3.data)) database.updateProposedProjectSupervisors(newProposedProjectId, supervisorsIds) flash('Proposed project created successfully!', 'success') return redirect(url_for('manageProposedProjects')) else: app.logger.info( 'In manageProposedProjects, 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') addFormErrors = True elif formName == 'editProposedProjectForm': proposedProject = database.getProposedProjectById(editForm.proposedProjectId.data) if not proposedProject: app.logger.error( 'In manageProposedProjects, in editForm, tried to edit a proposed project with id {} that does not exist in the db'.format( editForm.proposedProjectId.data)) flash("Error: project with id {} is not in the db.".format(editForm.proposedProjectId.data), 'danger') return redirect(url_for('manageProposedProjects')) if editForm.validate_on_submit(): picFile = proposedProject.image if editForm.image.data: # delete old image if exists if picFile is not None: utils.delete_proposed_project_image(picFile) picFile = utils.save_form_image(editForm.image.data, "proposed_projects") database.updateProposedProject(proposedProject.id, { "title": editForm.title.data, "description": editForm.description.data, "image": picFile, "lab": editForm.lab.data }) newSupervisorsIds = set() if editForm.supervisor1.data: newSupervisorsIds.add(int(editForm.supervisor1.data)) if editForm.supervisor2.data: newSupervisorsIds.add(int(editForm.supervisor2.data)) if editForm.supervisor3.data: newSupervisorsIds.add(int(editForm.supervisor3.data)) database.updateProposedProjectSupervisors(proposedProject.id, newSupervisorsIds) flash('Proposed project was updated successfully!', 'success') return redirect(url_for('manageProposedProjects')) else: app.logger.info( 'In manageProposedProjects, editForm is NOT valid. editForm.errors:{}'.format(editForm.errors)) 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') editFormErrorProposedProjectId = editForm.proposedProjectId.data return render_template('/admin/proposedProjects.html', title="Manage Proposed Projects", addForm=addForm, editForm=editForm, deleteForm=deleteForm, addFormErrors=addFormErrors, editFormErrorProposedProjectId=editFormErrorProposedProjectId, admin=admin, lab=lab) except Exception as e: app.logger.error('In manageProposedProjects, Error is: {}\n{}'.format(e, traceback.format_exc())) return redirect(url_for('errorPage'))
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'))