Exemplo n.º 1
0
def contact():
    """
    Contact a member of staff.
    """

    # Create an empty error.
    error = None

    # Create a form object.
    contact_form = ContactForm()

    # Select all the staff and tutors.
    contact_form.user.choices = [
        (user.user_id, user.get_full_name()) for user in select_users_by_roles(('TUT', 'STA'))
    ]

    if request.method == 'POST' and contact_form.validate_on_submit():
        # Form is valid.
        # Check the staff members is not the default
        if contact_form.user.data == '0':
            error = 'A staff member must be chosen.'
        else:
            # Find the user.
            user = User.query.filter(User.user_id == contact_form.user.data).first()
            # Create a new email message.
            message = Message(contact_form.subject.data, recipients=[user.get_email_address()])
            # Assign the message html to the rendered message template.
            message.html = render_template(
                'email/message.html', user=user, subject=contact_form.subject.data,
                message=contact_form.message.data
            )
            # Send the message.
            mail.send(message)
            # Flash a success message.
            flash('Successfully sent message.')
            # Redirect to the dashboard.
            return redirect(url_for('student.dashboard'))

    return render_template('student/contact.html', contact_form=contact_form, error=error)
Exemplo n.º 2
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
    )
Exemplo n.º 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
    ]

    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
    )