def create_checklist():
    """Create new checklist."""

    template_id = request.form.get('template_id')

    who_for = request.form.get('who_for')

    time_frame = request.form.get('time_frame')

    preparer_id = request.form.get('preparer_id')

    reviewer_id = request.form.get('reviewer_id')

    crud.create_checklist(template_id, who_for, time_frame, preparer_id,
                          reviewer_id)

    flash('Checklist created!')

    checklists = crud.get_checklists()

    users = crud.get_users()

    return render_template('/all_checklists.html',
                           checklists=checklists,
                           users=users)
def all_checklists():
    """View all checklists."""

    checklists = crud.get_checklists()
    users = crud.get_users()

    return render_template('all_checklists.html',
                           checklists=checklists,
                           users=users)
def show_stats():

    checklist_count = Checklist.query.filter(
        Checklist.checklist_id == checklist_id).filter(
            Checklists.date_complete.isnot(None)).count()

    checklists = crud.get_checklists()

    return render_template('report.html',
                           checklists=checklists,
                           checklist_count=checklist_count)
def mark_date_review_completed():
    """Mark date review completed."""

    checklist_id = request.form.get('checklist_id')

    date_review_completed = request.form.get('date_review_completed')

    crud.mark_datereviewcompleted(checklist_id, date_review_completed)

    flash('Date review completed registered! Select checklist desired.')

    checklists = crud.get_checklists()

    return render_template('all_checklists.html', checklists=checklists)
def mark_date_sent_to_review():
    """Mark date sent to review."""

    checklist_id = request.form.get('checklist_id')

    date_sent_to_review = request.form.get('date_sent_to_review')

    crud.mark_datesenttoreview(checklist_id, date_sent_to_review)

    flash('Date sent to review registered! Select checklist desired.')

    checklists = crud.get_checklists()

    return render_template('all_checklists.html', checklists=checklists)
def mark_complete():
    """Mark checklist complete."""

    checklist_id = request.form.get('checklist_id')

    date_complete = request.form.get('date_complete')

    crud.mark_complete(checklist_id, date_complete)

    flash('Checklist marked complete! Select checklist desired.')

    checklists = crud.get_checklists()

    return render_template('all_checklists.html', checklists=checklists)
def choose_recipient():
    """Choose pre-existing recipient."""

    checklist_id = request.form.get('checklist_id')

    recipient_id = request.form.get('recipient_id')

    crud.choose_recipient(checklist_id, recipient_id)

    flash('Recipient attached to checklist!')

    checklists = crud.get_checklists()

    return render_template('/all_checklists.html', checklists=checklists)
def register_recipient():
    """Create a new recipient user."""

    user_full_name = request.form.get('user_full_name')

    email = request.form.get('email')

    password = request.form.get('password')

    crud.create_recipient(user_full_name, email, password)

    flash('Account created!')

    checklists = crud.get_checklists()

    users = crud.get_users()

    return render_template('all_checklists.html',
                           users=users,
                           checklists=checklists)
def create_answer():

    checklist_id = request.form.get('checklist_id'),

    question_id = request.form.get('question_id'),

    preparer_answer = request.form.get('preparer_answer'),

    preparer_comment = request.form.get('preparer_comment'),

    preparer_time = request.form.get('preparer_time')

    crud.create_answer(checklist_id, question_id, preparer_answer,
                       preparer_time, preparer_comment)

    flash('Preparer answer added! Select checklist.')

    checklists = crud.get_checklists()

    return render_template('all_checklists.html')
def update_reviewer_answer():
    """Create new reviewer answer."""

    checklist_id = request.form.get('checklist_id')

    question_id = request.form.get('question_id')

    reviewer_ready = request.form.get('reviewer_ready')

    reviewer_time = request.form.get('reviewer_time')

    reviewer_comment = request.form.get('reviewer_comment')

    crud.update_revieweranswer(checklist_id, question_id, reviewer_ready,
                               reviewer_time, reviewer_comment)

    flash('Reviewer answer posted! Select checklist.')

    checklists = crud.get_checklists()

    return render_template('all_checklists.html', checklists=checklists)
def update_preparer_answer():
    """Create new reviewer answer."""

    checklist_id = request.form.get('checklist_id')

    question_id = request.form.get('question_id')

    preparer_answer = request.form.get('preparer_answer')

    preparer_time = request.form.get('preparer_time')

    preparer_comment = request.form.get('preparer_comment')

    crud.update_prepareranswer(checklist_id, question_id, preparer_answer,
                               preparer_time, preparer_comment)

    flash('Preparer answer updated! Select checklist.')

    checklists = crud.get_checklists()

    return render_template('all_checklists.html', checklists=checklists)
def all_checklistsstats():
    """View all checklists stats."""

    checklists = crud.get_checklists()

    answer = crud.get_answers()

    users = crud.get_users()

    questions = crud.get_questions()

    # Chart stats check - initial

    started_count = Checklist.query.filter(
        Checklist.date_complete.is_(None),
        Checklist.date_review_completed.is_(None),
        Checklist.date_sent_to_review.is_(None)).count()

    in_review_count = Checklist.query.filter(
        Checklist.date_complete.is_(None),
        Checklist.date_review_completed.is_(None),
        Checklist.date_sent_to_review.isnot(None)).count()

    sent_count = Checklist.query.filter(
        Checklist.date_complete.isnot(None),
        Checklist.date_complete.isnot(None),
        Checklist.date_complete.isnot(None)).count()

    reviewed_count = Checklist.query.filter(
        Checklist.date_complete.is_(None),
        Checklist.date_review_completed.isnot(None),
        Checklist.date_sent_to_review.isnot(None)).count()

    questions_with_corrections = [
        q[0] for q in db.session.query(Answer.question_id).filter(
            Answer.reviewer_ready == 'c').group_by(Answer.question_id).all()
    ]

    corrections_by_question_count = [
        q[0] for q in db.session.query(db.func.count(Answer.answer_id)).filter(
            Answer.reviewer_ready == 'c').group_by(Answer.answer_id).all()
    ]

    questions_requiring_training = [
        r.question for r in db.session.query(TemplateQuestion).outerjoin(
            Answer, Answer.question_id == TemplateQuestion.question_id).filter(
                Answer.reviewer_ready == 'c').all()
    ]
    # 3.0 after grad version

    # go back and apply to group by preparer sorted by most to least

    return render_template(
        'all_checklistsstats.html',
        checklists=checklists,
        users=users,
        answer=answer,
        questions=questions,
        questions_with_corrections=questions_with_corrections,
        corrections_by_question_count=corrections_by_question_count,
        questions_requiring_training=questions_requiring_training,
        sent_count=sent_count,
        reviewed_count=reviewed_count,
        in_review_count=in_review_count,
        started_count=started_count)