def welcome():

    records = Record.query.filter_by(user_id=current_user.id).order_by(Record.date_time.desc()).all()
    training_list = []
    for record in records:
        completed_by_duedate = record.date_time - return_datetime(due_date[record.training_name][0])

        print record.training_name, ": ", completed_by_duedate
        if completed_by_duedate.days >= -30:
            training_status = "Completed"
        else:
            training_status = "Incomplete"

        training_list.append((record.training_name, record.date_time.strftime('%Y-%m-%d'),
                              due_date[record.training_name][0], training_status))

    return render_template('welcome.html', title='Hello again', training_list=training_list)
def completed_training_list(training_name=None):

    # This section queries the User and Record tables for all users that have completed <training_name>
    user_list = Record.query.filter_by(training_name=training_name).order_by(Record.date_time.desc()).all()

    # Create new list of all users who are currently active
    active_user_list = []
    for x in user_list:
        if x.user.active:
            active_user_list.append(x)
    # Create list of users who have completed training
    user_list_on_time = []
    user_list_late = []
    for user in active_user_list:
        completed_by_duedate = user.date_time - return_datetime(due_date[training_name][0])
        if completed_by_duedate.days >= -30:
            user_list_on_time.append(user)
        else:
            user_list_late.append(user)

    # Create a list of all Users who have completed training.
    user_id_in_records = []
    for user in active_user_list:
        if user.user_id not in user_id_in_records:
            user_id_in_records.append(user.user_id)

    # Create list of all Users who have not completed any training.
    all_users = User.query.filter_by(active=True).all()
    user_not_doing_training = []  # List of Users who have not done any training
    for user in all_users:
        if user.id not in user_id_in_records:
            user_not_doing_training.append(user)

    for each in user_list:
        print each.user

    return render_template('training_list.html', training_name=training_name, user_list=user_list, user_list_on_time=user_list_on_time, user_list_late=user_list_late, user_not_doing_training=user_not_doing_training)