Exemple #1
0
def view_exercise(exercise_id):
    """
    Page to display a single exercise event.
    Displays the event with the id = exercise_id
    """

    # Get the exercise object with the given id.
    exercise = Exercise.query.filter_by(id=exercise_id).first()

    if exercise is not None:

        # Create the edit exercise form.
        edit_exercise_form = EditExerciseForm()

        if exercise.member != current_user:
            # If you are viewing another users exercise.
            db.session.add(
                Message(datetime.utcnow(),
                        current_user.get_full_name() + " Viewed your exercise",
                        exercise.member.get_id()))
            # Commit the changes.
            db.session.commit()

        # Get all of the exercise for the member of the given exercise.
        all_exercise_data = Exercise.query.filter_by(
            member=exercise.member).order_by(Exercise.id.desc()).all()

    else:
        # The exercise ID is invalid abort with HTTP 404
        abort(404)

    # Display the view page for a specific exercise event.
    return render_template('view.html',
                           all_exercise_data=all_exercise_data,
                           exercise=exercise,
                           member=exercise.member,
                           edit_exercise_form=edit_exercise_form)
Exemple #2
0
def edit_lesson(lesson_id):
    """
    Edit a lesson.
    """
    # Find the lesson with the given ID.
    lesson = select_lessons(current_user, lesson_id=lesson_id, single=True)

    # If the lesson is not found abort.
    if not lesson:
        # HTTP not found.
        abort(404)

    # 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
    ]
    # Remove the current user.
    edit_lesson_form.add_users.choices.remove(
        (current_user.user_id, current_user.get_full_name() + " (" + current_user.get_role(pretty=True) + ")")
    )

    # 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
    ]
    # Remove the current user from these choices (as they must still be in the lesson).
    edit_lesson_form.remove_users.choices.remove(
        (current_user.user_id, current_user.get_full_name() + " (" + current_user.get_role(pretty=True) + ")")
    )

    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(
        'tutor/edit_lesson.html', edit_lesson_form=edit_lesson_form, lesson=lesson
    )
Exemple #3
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
    ]
    # Remove the current user.
    add_lesson_form.users.choices.remove(
        (current_user.user_id, current_user.get_full_name() + " (" + current_user.get_role(pretty=True) + ")")
    )

    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)
                )

        # Add the current user to the lesson.
        new_lesson.users.append(current_user)

        # 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('tutor.add_lesson'))

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