def messages(section_id, id):
    section = SectionRepository.find_by_id(section_id)
    if not section:
        abort(404)
    thread = MessageRepository.find_by_id(id)
    if not thread:
        abort(404)
    if thread.section_id != section.id:
        abort(404)
    messages = MessageRepository.find_messages_of_thread(id)
    form = NewMessageForm()
    if form.validate_on_submit():
        message = Message()
        message.course_id = section.course_id
        message.section_id = section.id
        message.user_id = current_user.id
        message.section_only = thread.section_only
        message.title = thread.title
        message.thread_id = thread.id
        message.message = form.message.data
        message = MessageRepository.create(message)
        return redirect(
            url_for('section.messages', section_id=section_id, id=id))
    return render_template("section/messages.html",
                           section=section,
                           messages=messages,
                           form=form,
                           current_user=current_user,
                           thread=thread)
def threads(id):
    course = CourseRepository.find_by_id(id)
    if not course:
        abort(404)
    threads = MessageRepository.find_threads_of_course(course.id)
    form = NewThreadForCourseForm()
    if form.validate_on_submit():
        message = Message()
        message.course_id = course.id
        message.user_id = current_user.id
        message.title = form.title.data
        message.message = form.message.data
        message = MessageRepository.create(message)
        return redirect(url_for('course.threads', id=id))
    return render_template("course/threads.html", course=course, threads=threads, form=form, current_user=current_user)
def messages(course_id, id):
    course = CourseRepository.find_by_id(course_id)
    if not course:
        abort(404)
    thread = MessageRepository.find_by_id(id)
    if not thread:
        abort(404)
    if thread.course_id != course.id:
        abort(404)
    messages = MessageRepository.find_messages_of_thread(id)
    form = NewMessageForm()
    if form.validate_on_submit():
        message = Message()
        message.course_id = course.id
        message.user_id = current_user.id
        message.title = thread.title
        message.thread_id = thread.id
        message.message = form.message.data
        message = MessageRepository.create(message)
        return redirect(url_for('course.messages', course_id=course_id, id=id))
    return render_template("course/messages.html", course=course, messages=messages, form=form, current_user=current_user, thread=thread)
def threads(id):
    section = SectionRepository.find_by_id(id)
    if not section:
        abort(404)
    threads = MessageRepository.find_threads_of_section(section.id)
    form = NewThreadForSectionForm()
    if form.validate_on_submit():
        message = Message()
        message.course_id = section.course_id
        message.section_id = section.id
        message.user_id = current_user.id
        message.section_only = form.section_only.data
        message.title = form.title.data
        message.message = form.message.data
        message = MessageRepository.create(message)
        return redirect(url_for('section.threads', id=id))
    return render_template("section/threads.html",
                           section=section,
                           threads=threads,
                           form=form,
                           current_user=current_user)