Example #1
0
def view_tutor(tutor_id):
    """
    View a single tutor.
    """
    # Find the tutor.
    tutor = select_user(tutor_id, role='TUT')
    # Check the user exists.
    if tutor is not None:
        return render_template(
            'staff/view_tutor.html', tutor=tutor
        )
    else:
        # If the tutor isn't found, return a 404.
        abort(404)
Example #2
0
def view_staff(staff_id):
    """
    View a single staff member.
    """
    # Find the staff member.
    staff_member = select_user(staff_id, role='STA')
    # Check the user exists.
    if staff_member is not None:
        return render_template(
            'staff/view_staff.html', staff_member=staff_member
        )
    else:
        # If the staff_member isn't found, return a 404.
        abort(404)
Example #3
0
def view_student(student_id):
    """
    View a single student.
    """
    # Find the student.
    student = select_user(student_id, role='STU')
    # Check the user exists.
    if student is not None:
        return render_template(
            'staff/view_student.html', student=student
        )
    else:
        # If the student isn't found, return a 404.
        abort(404)
Example #4
0
def view_staff(staff_id):
    """
    View Staff Member.
    """
    # Select the staff member with the given staff_id.
    staff_member = select_user(staff_id, role='STA')
    # Ensure the tutor is found.
    if not staff_member:
        # Abort with 404 error code.
        abort(404)
    # Render the template passing in the staff member.
    return render_template(
        'student/view_staff.html', staff_member=staff_member
    )
Example #5
0
def view_tutor(tutor_id):
    """
    View Tutor.
    """
    # Select the tutor with the given tutor_id.
    tutor = select_user(tutor_id, role='TUT')
    # Ensure the tutor is found.
    if not tutor:
        # Abort with 404 error code.
        abort(404)
    # Render the template passing in the tutor object.
    return render_template(
        'student/view_tutor.html', tutor=tutor
    )
Example #6
0
def edit_lesson(lesson_id):
    """
    Edit a lesson.
    """
    # Create a new edit lesson form.
    edit_lesson_form = EditLessonForm()
    # Add the rooms.
    edit_lesson_form.lesson_room_id.choices = [
        (room.room_id, room.get_location()) for room in Room.query.all()
    ]
    # Select all users.
    all_users = select_users_by_roles(('STU', 'TUT', 'STA'))
    # Set the choices for the users that can be selected for the new users.
    edit_lesson_form.add_users.choices = [
        (user.user_id, user.get_full_name() + " (" + user.get_role(pretty=True) + ")") for user in all_users
    ]

    # Find the lesson with the given ID.
    lesson = Lesson.query.filter(Lesson.lesson_id == lesson_id).first()

    # All the users that can be removed are the users of the lesson.
    edit_lesson_form.remove_users.choices = [
        (user.user_id, user.get_full_name() + " (" + user.get_role(pretty=True) + ")") for user in lesson.users
    ]

    if request.method == 'POST' and edit_lesson_form.validate_on_submit():
        # Create the datetime object.
        lesson_datetime = datetime.combine(
            edit_lesson_form.lesson_date.data,
            time(
                edit_lesson_form.lesson_hour.data,
                edit_lesson_form.lesson_minute.data
            )
        )
        # Update the lesson.
        lesson.update_lesson_details(
            lesson_datetime=lesson_datetime,
            lesson_duration=edit_lesson_form.lesson_duration.data*60,
            lesson_notes=edit_lesson_form.lesson_notes.data,
            room_id=edit_lesson_form.lesson_room_id.data
        )

        if app.config['UPDATE_ON_EDIT_LESSON']:
            # Iterate through the users and send updates.
            for user in lesson.users:
                if user.get_role() == 'STU':
                    # Send an email update.
                    html = 'Your lesson on: ' + lesson.get_lesson_date() + \
                        ' has been updated.'

                    # Send a lesson update.
                    send_lesson_update(
                        user, html,
                        url_for(
                            'student.view_lesson',
                            lesson_id=lesson.lesson_id,
                            _external=True
                        )
                    )

        # Iterate through the users to add.
        for user_id in edit_lesson_form.add_users.data:
            # Select the user object.
            user_object = select_user(user_id)
            # If the user is not already going to the lesson.
            if user_object not in lesson.users:
                # Append it to the lessons users.
                lesson.users.append(user_object)

                # Send an email update.
                html = 'You have been added to a lesson on: ' + lesson.get_lesson_date()

                # Send a lesson update.
                send_lesson_update(
                    user_object, html,
                    url_for(
                        'student.view_lesson',
                        lesson_id=lesson.lesson_id,
                        _external=True
                    )
                )
        # Iterate through the users to remove.
        for user_id in edit_lesson_form.remove_users.data:
            # Delete the user lesson association for this user/lesson.
            db.session.delete(
                UserLessonAssociation.query.filter(
                    UserLessonAssociation.lesson_id == lesson_id
                ).filter(
                    UserLessonAssociation.user_id == user_id
                ).first()
            )
            # Send an email update.
            html = 'You have been removed from the lesson on: ' + lesson.get_lesson_date() \
                + ' this means your attendance is no longer required.'

            # Send a lesson update.
            send_lesson_update(
                User.query.filter(User.user_id == user_id).first(), html,
                url_for('student.lessons', _external=True)
            )

        # Commit Changes.
        db.session.commit()

        # Flash a success message.
        flash("Successfully updated lesson.")

    # Set the defaults.
    edit_lesson_form.lesson_date.default = lesson.lesson_datetime.date()
    edit_lesson_form.lesson_notes.default = lesson.get_lesson_notes()
    edit_lesson_form.lesson_room_id.default = lesson.room.room_id
    # Process the form.
    edit_lesson_form.process()

    return render_template(
        'staff/edit_lesson.html', edit_lesson_form=edit_lesson_form, lesson=lesson
    )
Example #7
0
def add_lesson():
    """
    Add a new lesson.
    """
    # Create empty error object.
    error = None

    # Create form.
    add_lesson_form = AddLessonForm()
    # Add the rooms.
    add_lesson_form.lesson_room_id.choices = [
        (room.room_id, room.get_location()) for room in Room.query.all()
    ]
    # Select all users.
    all_users = select_users_by_roles(('STU', 'TUT', 'STA'))
    # Update the form choices.
    add_lesson_form.users.choices = [
        (user.user_id, user.get_full_name() + " (" + user.get_role(pretty=True) + ")") for user in all_users
    ]

    if request.method == 'POST' and add_lesson_form.validate_on_submit():
        # Create a new lesson object.
        new_lesson = Lesson()
        # Create the datetime object.
        lesson_datetime = datetime.combine(
            add_lesson_form.lesson_date.data,
            time(
                add_lesson_form.lesson_hour.data,
                add_lesson_form.lesson_minute.data
            )
        )
        # Update the lesson details.
        new_lesson.update_lesson_details(
            lesson_datetime=lesson_datetime,
            lesson_duration=add_lesson_form.lesson_duration.data*60,
            lesson_notes=add_lesson_form.lesson_notes.data,
            room_id=add_lesson_form.lesson_room_id.data
        )
        # Iterate through the users.
        for user_id in add_lesson_form.users.data:
            # Select the user object.
            user_object = select_user(user_id)
            # Append it to the lessons users.
            new_lesson.users.append(user_object)

            # Send an update.
            if app.config['UPDATE_ON_NEW_LESSON']:
                # Send an email update.
                html = 'A new lesson has been created on: ' + new_lesson.get_lesson_date()

                # Send a lesson update.
                send_lesson_update(
                    user_object, html,
                    url_for('student.lessons', _external=True)
                )

        # Flash a success message.
        flash("Successfully added new lesson.")

        # Add the lesson to the db.
        db.session.add(new_lesson)
        # Commit changes.
        db.session.commit()

        return redirect(url_for('staff.add_lesson'))

    return render_template(
        'staff/add_lesson.html', add_lesson_form=add_lesson_form, error=error
    )