示例#1
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
    )
示例#2
0
    def setUp(self):
        """
        Authenticate as staff before each request.
        """
        # Call the super class setup method.
        BaseTestCase.setUp(self)
        # Add a student, tutor, parent and staff user to the database.
        staff_user = User()
        # Add Tutor User
        staff_user.update_user_details(
            first_name='staff',
            last_name='staff',
            email_address='*****@*****.**',
            password=staff_user.create_password_hash('password1'),
            role='STA',
        )
        db.session.add(staff_user)
        tutor_user = User()
        # Add Tutor User
        tutor_user.update_user_details(
            first_name='tutor',
            last_name='tutor',
            email_address='*****@*****.**',
            password=tutor_user.create_password_hash('password1'),
            role='TUT',
        )
        db.session.add(tutor_user)
        # Add a parent.
        parent = Parent()
        parent.update_parent_details(
            first_name='parent',
            last_name='parent',
            email_address='*****@*****.**',
            telephone_number='01234567890'
        )
        db.session.add(parent)
        student_user = User()
        # Add Student User 
        student_user.update_user_details(
            first_name='student',
            last_name='student',
            email_address='*****@*****.**',
            password=student_user.create_password_hash('password1'),
            role='STU',
            tutor_group='ABC',
            musical_instrument_type='Instrument',
            musical_instrument='Piano',
            musical_style='jazz',
            lesson_type='individual',
            parent_id=1,
        )
        db.session.add(student_user)

        # Add a new room.
        room = Room()
        room.update_room_details(
            location='Room1',
            facilities='Piano'
        )
        db.session.add(room)

        # Add a new lesson.
        lesson = Lesson()
        lesson.update_lesson_details(
            lesson_datetime=datetime.now() + timedelta(days=7),
            lesson_duration=3600,
            lesson_notes='Work Very Hard',
            room_id=1
        )
        lesson.users.append(tutor_user)
        lesson.users.append(student_user)
        db.session.add(lesson)

        # Commit the changes.
        db.session.commit()


        # Authenticate.
        with self.client:
            self.client.post(
                '/auth/login',
                data=dict(email_address='*****@*****.**', password='******'),
                follow_redirects=True
            )

        # Set the IDs to use.
        self.STAFF_ID = staff_user.user_id
        self.TUTOR_ID = tutor_user.user_id
        self.STUDENT_ID = student_user.user_id
        self.PARENT_ID = parent.parent_id