def get(self):
        """
        This method handles get requests for setAvailable.html
        Input: only self
        Output: renders template for setAvailable.html with the session id of profDashboard.html
        """
        if not validate_professor():
            return display_access_control_error()

        session_id = request.args.get('session_id')
        return render_template('setAvailable.html',
                               error=None,
                               session_id=session_id)
    def get(self):
        """
        This method handles get requests go setDate.html
        Input: only self
        Output: rendering the addSession.html template with session id
                from profDashboard.html
        """
        if not validate_professor():
            return display_access_control_error()

        session_id = request.args.get('session_id')
        return render_template('setDate.html',
                               error=None,
                               session_id=session_id)
    def get(self):
        """
        This method handles get requests go removeTeam.html
        Input: only self
        Output: rendering the addSession.html template with session id
                and team name from profDashboard.html
        """
        if not validate_professor():
            return display_access_control_error()

        team_name = request.args.get('data')
        session_id = request.args.get('session_id')
        return render_template('removeTeam.html',
                               team_name=team_name,
                               session_id=session_id)
    def get(self):
        """
        This method handles get requests go addStudent.html
        Input: only self
        Output: rendering the addSession.html template with session id
                name of the team from profDashboard.html
        """
        if not validate_professor():
            return display_access_control_error()

        team_name = request.args.get('data')
        session_id = request.args.get('session_id')
        return render_template('addStudent.html',
                               team_name=str(team_name),
                               session_id=session_id,
                               error=None)
    def get(self):
        """
        This method handled get requests for assignTeam.html
        Input: self
        Output: none
        """
        if not validate_professor():
            return display_access_control_error()

        s_id = request.args.get('session_id')
        students_table = gbmodel.students()
        team_table = gbmodel.teams()
        unassigned_students = students_table.get_unassigned_students(s_id)
        if unassigned_students is None:
            error = "No students unassigned to a team."
            return render_template('errorPage.html', msg=error)
        sessions = team_table.dashboard(s_id)
        return render_template('assignTeam.html',
                               lists=unassigned_students,
                               sessions=sessions,
                               session_id=s_id,
                               error=None)
    def get(self):
        """
        This method handles get requests go addSession.html
        Input: only self
        Output: rendering the addSession.html template with session id
                from profDashboard.html
        """
        if not validate_professor():
            return display_access_control_error()

        # Canceling creating a session, render to the current session
        old_session_id = request.args.get('session_id')
        session = gbmodel.capstone_session()
        # Session id for new session
        session_id = session.get_max()
        professors = gbmodel.professors()
        prof_list = professors.get_all_professors()
        return render_template('addSession.html',
                               error=None,
                               session_id=session_id,
                               old_session_id=old_session_id,
                               prof_list=prof_list)
    def get(self):
        """
        Get session_id from the previous selected session
        If None returned then request for a selection.
        Otherwise, display the current session_id
        """
        if not validate_professor():
            return display_access_control_error()

        session = gbmodel.capstone_session()
        team = gbmodel.teams()
        session_id = request.args.get('session_id')
        if session_id is None:
            user_session = request.args.get('selected_session')
            # When professor first login, user_session = None
            if user_session is None:
                session_id = ""
            else:
                term = str(user_session[:user_session.index("-")].strip())
                year = int(user_session[user_session.index("-") +
                                        1:user_session.index("(")].strip())
                prof = str(user_session[user_session.index("(") +
                                        1:user_session.index(")")].strip())
                session_id = session.get_session_id(term, year, prof)
        # Lists - a list of teams and students of a selected session to display on the dashboard
        # Sessions - a list of sessions to display in drop downs
        lists, sessions = team.dashboard(session_id)
        # If theres no team in the session, do not display teams
        if lists is None:
            return render_template('profDashboard.html',
                                   sessions=sessions,
                                   session_id=session_id)
        return render_template('profDashboard.html',
                               lists=lists,
                               sessions=sessions,
                               session_id=session_id)
Пример #8
0
    def post(self):
        """
        Determines how the class will handle POST requests
        INPUT: self
        OUTPUT: It looks like it will return a rendering of the viewReview.html file. The information
                included in this rendering depends on the POST request parameters.
                Used as reference: http://flask.pocoo.org/docs/1.0/api/#flask.render_template
        """
        # Validate that the one making the request is a professor
        if not validate_professor():
            return display_access_control_error()

        # Get the database object we will need
        reports = gbmodel.reports()
        teams = gbmodel.teams()
        students = gbmodel.students()

        # Validate that the one making the request is a professor
        # Might want to consider noting things like these or forcefuly logging the person out
        if not validate_professor():
            return self.display_error(("A student (or someone else) tried to access the view_review page (a "
                                       "page that professors should only have access to)"))

        try:
            # Get data from the POST request
            # This helped: https://stackoverflow.com/questions/23205577/python-flask-immutablemultidict
            session_id = request.form.getlist('session_id')[0]
            reviewer_id = request.form.getlist('reviewer_id')[0]
            reviewee_id = request.form.getlist('reviewee_id')[0]
            is_final = request.form.getlist('is_final')[0]

            # Query the database for the the reviewing student
            reviewer = students.get_student_in_session(reviewer_id, session_id)
            if reviewer is None:
                return self.display_error("Reviewer not found in the database")

            # Query the database for the student being reviewed
            reviewee = students.get_student_in_session(reviewee_id, session_id)
            if reviewee is None:
                return self.display_error("Reviewee not found in the database")

            # Verify the reviewer and reviewee are are on the same team
            if reviewer.tid != reviewee.tid:
                return self.display_error("Reviewer and reviewee don't appear to be on the same team")

            # Get the the name of the team the reviewer and reviewee are on
            team = teams.get_team_from_id(reviewer.tid)
            if team is None:
                return self.display_error("Reviewer and reviewee's team not found in database")

            # Finally, get the review and parse it (if we find it in the database)
            report = reports.get_report(reviewer_id, reviewee_id, reviewer.tid, is_final)
            if report is not None:
                # Get some general report details
                review_details = {"time": report.time,
                                  "reviewer": reviewer.name,
                                  "reviewee": reviewee.name,
                                  "team_name": team.name,
                                  "is_late": report.is_late,
                                  "is_final": is_final}

                # Get the main part of the review
                parsed_review = [
                        {"category": "Technical Skill",
                         "content": self.interperate_rating(report.tech_mastery)},
                        {"category": "Work Ethic",
                         "content": self.interperate_rating(report.work_ethic)},
                        {"category": "Communication Skills",
                         "content": self.interperate_rating(report.communication)},
                        {"category": "Cooperation",
                         "content": self.interperate_rating(report.cooperation)},
                        {"category": "Initiative",
                         "content": self.interperate_rating(report.initiative)},
                        {"category": "Team Focus",
                         "content": self.interperate_rating(report.team_focus)},
                        {"category": "Contribution",
                         "content": self.interperate_rating(report.contribution)},
                        {"category": "Leadership",
                         "content": self.interperate_rating(report.leadership)},
                        {"category": "Organization",
                         "content": self.interperate_rating(report.organization)},
                        {"category": "Delegation",
                         "content": self.interperate_rating(report.delegation)},
                        {"category": "Points",
                         "content": report.points},
                        {"category": "Strengths",
                         "content": report.strengths},
                        {"category": "Weaknesses",
                         "content": report.weaknesses},
                        {"category": "Trait work on to become a better team member",
                         "content": report.traits_to_work_on}]

                # Final self reviews will have some extra details
                if reviewer_id == reviewee_id:
                    parsed_review.append({"category": "What the student learned from the experience",
                                          "content": report.what_you_learned})

                    if report.is_final:
                        parsed_review.append({"category": "If the student is proud of their accomplishment",
                                              "content": report.proud_of_accomplishment})

                # Send the data off to the client
                return render_template("viewReview.html", details=review_details, review_data=parsed_review)
            else:
                # Might consider adding more detail so the problem can be addressed more easly
                return self.display_error("We couldn't find the report we were looking for in the database")

        # https://stackoverflow.com/questions/47719838/how-to-catch-all-exceptions-in-try-catch-block-python
        except Exception as error:
            return self.display_error(error)
    def post(self):
        """
        A function that determines how the viewStudent class handles POST requests
        INPUT: self
        OUTPUT: a rendering of the viewStudent.html file. The information included in the rendering depends on
                the information we get from the POST request
        """
        # Get the database object we will need
        students = gbmodel.students()
        teams = gbmodel.teams()
        reports = gbmodel.reports()

        # Validate that the one making the request is a professor
        # Might want to consider noting things like these or forcefuly logging the person out
        if not validate_professor():
            return display_access_control_error()

        # Otherwise, load the student page
        try:
            # Get the student and session id from the post request, and try to find the student in the db
            student_id = request.form.getlist('student_id')[0]
            session_id = request.form.getlist('session_id')[0]
            student = students.get_student_in_session(student_id, session_id)

            # If the student is found, find all of the student's team members and see if the student filled
            # out reviews for those team members
            if student is not None:
                # Get team name
                team = teams.get_team_from_id(student.tid)
                if team is None:
                    return self.display_error((
                        "Students' team not found in database (when searching via team"
                        " id)"))

                # Record it, along with some other information about the student
                student_details = {
                    "name": student.name,
                    "id": student.id,
                    "team_name": team.name
                }

                # See if the student completed a midterm and final review for their team members and
                # record it
                reviews = []
                team_members = students.get_team_members(student.tid)
                for team_member in team_members:
                    midterm_review_completed = self.check_review_done(
                        reports, student.id, team_member.id, student.tid,
                        False)
                    final_review_completed = self.check_review_done(
                        reports, student.id, team_member.id, student.tid, True)
                    reviews.append({
                        "reviewee_name":
                        team_member.name,
                        "reviewee_id":
                        team_member.id,
                        "completed":
                        (midterm_review_completed, final_review_completed)
                    })

                # Combine the recorded data with the viewStudent.html template and render the viewStudent
                # page
                return render_template('viewStudent.html',
                                       student=student_details,
                                       review_data=reviews,
                                       session_id=session_id)
            else:
                return self.display_error(
                    ("Student was not found in the database "
                     "(when we searched via student ID)"))

        # https://stackoverflow.com/questions/47719838/how-to-catch-all-exceptions-in-try-catch-block-python
        except Exception as error:
            return self.display_error(error)
    def post(self):
        """
        This method handles all the functionalities from proDashboard
        includes add/remove students/teams, add new session, set review
        midterm/final start/end dates, setting reviews to be open/closed,
        and set team lead
        """
        if not validate_professor():
            return display_access_control_error()

        session = gbmodel.capstone_session()
        student = gbmodel.students()
        team = gbmodel.teams()
        professor = gbmodel.professors()
        # Get current session id from dropdowns in profDashboard.html
        session_id = request.form['session_id']
        if 'student_name' in request.form:
            # Add New Student (student name, student id and student email)
            # Get team name and session id from profDashboard.html,
            # new student id, name, email from addStudent.html
            team_name = request.form.get('team_name')
            if not student.check_dup_student(request.form['student_id'],
                                             session_id):
                # If student id in a current session already exists
                # Return to addStudent.html with error msg and request a new form
                error = "Student id " + str(
                    request.form['student_id']) + " already exists"
                return render_template('addStudent.html',
                                       team_name=team_name,
                                       session_id=session_id,
                                       error=error)
            if request.form['student_email'] != '':
                # If new email is invalid, return to addStudent.html
                # with error msg and request a new form
                if self.valid_email(str(
                        request.form['student_email'])) is False:
                    error = "Invalid Email Address"
                    return render_template('addStudent.html',
                                           team_name=team_name,
                                           session_id=session_id,
                                           error=error)
            # Insert new student information into the database
            student.insert_student(request.form['student_name'],
                                   request.form['student_email'],
                                   request.form['student_id'], session_id,
                                   team_name)
            # Update new list of students to reflect on profDashboard.html
            lists, sessions = team.dashboard(session_id)
            return render_template('profDashboard.html',
                                   lists=lists,
                                   sessions=sessions,
                                   session_id=session_id)
        elif 'team' in request.form:
            # Remove a student/students from a team
            # get list of students and team name from profDashboard.html
            students = request.form.getlist('removed_student')
            team_name = request.form.get('team')
            # Remove student/students from database
            student.remove_student(students, team_name, session_id)
            lists, sessions = team.dashboard(session_id)
            return render_template('profDashboard.html',
                                   lists=lists,
                                   sessions=sessions,
                                   session_id=session_id)
        elif 'removed_team' in request.form:
            # Remove a team in a session
            # Get team name in current session from profDashboard.html
            team_name = request.form.get('removed_team')
            # There was a problem removing teams with blank names, so (in remove team requests) a '_'
            # character was added to the beginning of the name.
            # We will want to remove it before we continue
            # https://stackoverflow.com/questions/4945548/remove-the-first-character-of-a-string
            team_name = team_name[1:]
            # Remove team and students in the team from database
            team.remove_team(team_name, session_id)
            lists, sessions = team.dashboard(session_id)
            return render_template('profDashboard.html',
                                   lists=lists,
                                   sessions=sessions,
                                   session_id=session_id)
        elif 'start_term' in request.form:
            # Add a new session to the profDashboard
            # Gets all professors in DB and stores into prof_list
            professors = gbmodel.professors()
            prof_list = professors.get_all_professors()
            while not session.check_term_name(request.form['start_term']):
                error = "Enter a valid term (Example: Summer)"
                return render_template('addSession.html',
                                       error=error,
                                       session_id=session_id,
                                       prof_list=prof_list)
            while not session.check_term_year(request.form['start_year']):
                error = "Enter a valid year (Example: 2019)"
                return render_template('addSession.html',
                                       error=error,
                                       session_id=session_id,
                                       prof_list=prof_list)
            while not professor.check_professor(request.form['professor_id']):
                error = "Enter a valid professor ID"
                return render_template('addSession.html',
                                       error=error,
                                       session_id=session_id,
                                       prof_list=prof_list)
            while not session.check_dup_session(request.form['start_term'],
                                                request.form['start_year'],
                                                request.form['professor_id']):
                error = "Session already exists"
                return render_template('addSession.html',
                                       error=error,
                                       session_id=session_id,
                                       prof_list=prof_list)
            start_term = request.form.get('start_term')
            start_year = request.form.get('start_year')
            start_term = start_term.replace("_", " ")
            start_year = start_year.replace("_", " ")
            professor_id = request.form.get('professor_id')
            professor_id = professor_id.replace("_", " ")
            session_id = session.insert_session(start_term, start_year,
                                                professor_id)
            lists, sessions = team.dashboard(session_id)
            return render_template('profDashboard.html',
                                   lists=lists,
                                   sessions=sessions,
                                   session_id=session_id)
        # If REMOVE SESSION was submitted (removed_session)
        elif 'removed_session' in request.form:
            while not session.check_session_id_valid(
                    request.form['removed_session']):
                error = "Invalid session ID"
                return render_template('profDashboard.html',
                                       lists=lists,
                                       sessions=sessions,
                                       session_id=session_id)
            remove_session = request.form.get('removed_session')
            remove_session = remove_session.replace("_", " ")
            session.remove_session(session_id)
            session_id = session.get_max() - 1
            lists, sessions = team.dashboard(session_id)
            return render_template('profDashboard.html',
                                   lists=lists,
                                   sessions=sessions,
                                   session_id=session_id)
        # If ADD TEAM was submitted (addTeam)
        elif 'team_name' in request.form:
            # Add a new team to a current session
            # Request new team name from addTeam.html
            if not team.check_dup_team(request.form['team_name'], session_id):
                # If new name already exists in current session
                # Rentering the addTeam.html with given error message
                error = "Team name already exists"
                return render_template('addTeam.html',
                                       error=error,
                                       session_id=session_id)
            team_name = request.form.get('team_name')
            # Add new team to the given session from profDashboard.html
            team.insert_team(session_id, team_name)
            # Update new list of sessions, teams, students to reflect on profDashboard.html
            lists, sessions = team.dashboard(session_id)
            return render_template('profDashboard.html',
                                   lists=lists,
                                   sessions=sessions,
                                   session_id=session_id)
        # if ASSIGNED TEAMS for to place new students on teams was submitted
        elif 'assigned_teams' in request.form:
            size = request.form.get('size')
            size = int(size)
            unassigned_students = student.get_unassigned_students(session_id)
            team_names = []
            i = 1
            while i <= size:
                team_name = (request.form.get('assigned_team' + str(i)))
                if team.check_dup_team(team_name, session_id) is False:
                    t_id = team.get_tid_from_name(team_name, session_id)
                    student.update_team(unassigned_students[i - 1].name,
                                        session_id, t_id)
                else:
                    team.insert_team(session_id, team_name)
                    t_id = team.get_tid_from_name(team_name, session_id)
                    student.update_team(unassigned_students[i - 1].name,
                                        session_id, t_id)
                team_names.append(team_name)
                i += 1
            lists, sessions = team.dashboard(session_id)
            return render_template('profDashboard.html',
                                   lists=lists,
                                   sessions=sessions,
                                   session_id=session_id)
        # If IMPORT STUDENTS was submitted (addTeamCSV)
        elif 'student_data_csv' in request.files:
            session_id = int(request.form['session_id'])
            teams_table = gbmodel.teams()  # Accessor to the teams table
            students_table = gbmodel.students(
            )  # Accessor to the students table
            file = request.files['student_data_csv']
            # If 'Create from File' was selected with no file
            # return back to import student page.
            if (file.filename == ''):
                return render_template('csvAddTeam.html',
                                       session_id=session_id,
                                       error="Please select a file to upload")
            stream = io.StringIO(file.stream.read().decode("UTF8"),
                                 newline=None)
            csv_reader = csv.reader(stream, delimiter=',')
            uninserted_students = []
            for row in csv_reader:
                if len(row) > 3:
                    return render_template('csvAddTeam.html',
                                           session_id=session_id,
                                           error="Incorrect csv Format")
                try:
                    student_name = row[0]
                    student_id = row[1]
                    team_name = row[2]
                except IndexError:
                    logging.warning(
                        "CSV Add Students/Team - Problem parsing csv")
                    return render_template('csvAddTeam.html',
                                           session_id=session_id,
                                           error="Incorrect csv Format")

                # Create team if it doesn't exist, then create the student.
                try:
                    if teams_table.check_dup_team(team_name,
                                                  session_id) is True:
                        teams_table.insert_team(session_id, team_name)
                except SQLAlchemyError:
                    logging.error((
                        'CSV Add Students/Team - Error checking for existing team and/or'
                        ' inserting a new one'))
                    return render_template('csvAddTeam.html',
                                           session_id=session_id,
                                           error="Something went wrong")
                try:
                    if students_table.check_dup_student(
                            student_id, session_id) is True:
                        students_table.insert_student(student_name, "",
                                                      student_id, session_id,
                                                      team_name)
                    else:
                        # Keep track of what students weren't added to the database (and make a note it)
                        logging.warning(
                            "CSV Add Students/Team -"
                            " Error inserting student into the database")
                        uninserted_students.append(student_name)
                except SQLAlchemyError:
                    logging.error((
                        'CSV Add Students/Team -'
                        ' Error inserting students or checking if they exist in the database'
                    ))
                    return render_template('csvAddTeam.html',
                                           session_id=session_id,
                                           error="Something went wrong")

            # If everything went well, reload the professor dashboard
            if len(uninserted_students) == 0:
                logging.info(
                    "CSV Add Students/Team - added student data from uploaded csv file"
                )
                lists, sessions = team.dashboard(session_id)
                return render_template('profDashboard.html',
                                       lists=lists,
                                       sessions=sessions,
                                       session_id=session_id)
            # If there were some problems, let the user know
            else:
                error_str = "There was a problem inserting the following students into the database: "
                error_str = error_str + ", ".join(uninserted_students)
                error_str = error_str + ". They are already in this session."
                return render_template('csvAddTeam.html',
                                       session_id=session_id,
                                       error=error_str)

        # If SET DATE for reviews was submitted (setDate)
        elif 'midterm_start' in request.form:
            # Add midterm/final start/end dates for review form
            # Request start and end dates for midterm and final from setDate.html
            midterm_start = request.form.get('midterm_start')
            midterm_end = request.form.get('midterm_end')
            final_start = request.form.get('final_start')
            final_end = request.form.get('final_end')
            params = {
                'midterm_start': midterm_start,
                'midterm_end': midterm_end,
                'final_start': final_start,
                'final_end': final_end
            }
            if session.date_error(params) is not None:
                # Check if the dates are valid, rendering to setDate.html
                # with a error message
                error_msg = session.date_error(params)
                return render_template('setDate.html',
                                       error=error_msg,
                                       session_id=session_id)
            # Insert dates into database
            session.insert_dates(midterm_start, midterm_end, final_start,
                                 final_end, session_id)
            # Update new list of sessions, teams, students to reflect on profDashboard.html
            lists, sessions = team.dashboard(session_id)
            return render_template('profDashboard.html',
                                   lists=lists,
                                   sessions=sessions,
                                   session_id=session_id)
        elif 'team_lead' in request.form:
            # Set team lead for a team in current session
            # Get team name and lead from checkboxes in profDashboard.html
            team_name = request.form.get('team_lead')
            student_name = request.form.get('is_lead')
            # Set lead for chosen team in current sesison
            student.set_lead(session_id, team_name, student_name)
            # Update new list of sessions, teams, students to reflect on profDashboard.html
            lists, sessions = team.dashboard(session_id)
            return render_template('profDashboard.html',
                                   lists=lists,
                                   sessions=sessions,
                                   session_id=session_id)
        elif 'set_review_available' in request.form:
            # update students' review availability
            setting = request.form.get('set_review_available')
            result = student.set_active(session_id, setting)
            if result is True:
                # back to page
                lists, sessions = team.dashboard(session_id)
                return render_template('profDashboard.html',
                                       lists=lists,
                                       sessions=sessions,
                                       session_id=session_id)
            else:
                error_msg = "Error When Selecting Option"
                return render_template('setAvailable.html',
                                       error=error_msg,
                                       session_id=session_id)
    def get(self):
        if not validate_professor():
            return display_access_control_error()

        session_id = request.args.get('session_id')
        return render_template('csvAddTeam.html', session_id=session_id)