Exemplo n.º 1
0
def report_new_date2(chosen_date):
    date_info = get_date_info(chosen_date)
    form_filter = FilterField()
    chosen_date_objects = DateNew.find_activitys_by_date(chosen_date)

    meal_dict = Meal.json_meal()
    activity_symptoms = sorted(Activity.json_symptoms(),
                               key=lambda k: k['category_id'])
    activity_meals = sorted(Activity.json_meals(),
                            key=lambda k: k['category_id'])

    type_true_or_false = {
        single: (True if single == 'breakfast' else False)
        for single in ['breakfast', 'lunch', 'last_meal', 'morning', 'night']
    }
    today = DateNew.json_dict_list_of_done_activies_by_date(chosen_date)

    done_activite_ids = [
        single.activity_id
        for single in DateNew.find_activitys_by_date_and_type(
            chosen_date, **type_true_or_false)
    ]
    if form_filter.validate_on_submit():
        filter = form_filter.filter.data
        find_filtered_records = Activity.filer_activity_objects(filter)
        # print(find_filtered_records)
        return render_template('report_full_json.html',
                               form=form_filter,
                               activity_list=find_filtered_records,
                               type=type,
                               chosen_date=chosen_date,
                               chosen_date_objects=chosen_date_objects,
                               done_activite_ids=done_activite_ids,
                               meal_dict=meal_dict,
                               today=today)

    all_date_records_for_chosen_date = DateNew.json_full_info_by_date(
        chosen_date)
    data_for_prediction = DateNew.json_info_for_prediction(chosen_date)
    print(type(chosen_date), chosen_date, 'chosen_date')

    if request.method == 'POST':
        checkboxes_breakfast = request.form.getlist('checkbox_breakfast')
        checkboxes_lunch = request.form.getlist('checkbox_lunch')
        checkboxes_last_meal = request.form.getlist('checkbox_last_meal')
        prediction_choice_id = request.form.get('prediction_choice')
        if prediction_choice_id:

            data_object = DateNew.find_by_id(prediction_choice_id)
            print(data_object.id, ' im hereeeeeeeeeeeeeeeee')
            data_object.prediction = True
            db.session.commit()
            flash(
                f'{data_object.activity.name} is your prediction for stomach problems, we added it to database'
            )

        checkboxes = [{
            'activity_ids':
            request.form.getlist('checkbox_breakfast'),
            'type':
            'breakfast'
        }, {
            'activity_ids': request.form.getlist('checkbox_lunch'),
            'type': 'lunch'
        }, {
            'activity_ids':
            request.form.getlist('checkbox_last_meal'),
            'type':
            'last_meal'
        }, {
            'activity_ids': request.form.getlist('checkbox_morning'),
            'type': 'morning'
        }, {
            'activity_ids': request.form.getlist('checkbox_night'),
            'type': 'night'
        }, {
            'activity_ids': request.form.getlist('checkbox_meal'),
            'type': 'meal'
        }]

        for type_checkbox in checkboxes:
            if type_checkbox['type'] == 'breakfast':
                type_true_or_false = {
                    single:
                    (True if single == type_checkbox['type'] else False)
                    for single in
                    ['breakfast', 'lunch', 'last_meal', 'morning', 'night']
                }
            if type_checkbox['type'] == 'lunch':
                type_true_or_false = {
                    single:
                    (True if single == type_checkbox['type'] else False)
                    for single in
                    ['breakfast', 'lunch', 'last_meal', 'morning', 'night']
                }
            if type_checkbox['type'] == 'last_meal':
                type_true_or_false = {
                    single:
                    (True if single == type_checkbox['type'] else False)
                    for single in
                    ['breakfast', 'lunch', 'last_meal', 'morning', 'night']
                }
            if type_checkbox['type'] == 'morning':
                type_true_or_false = {
                    single:
                    (True if single == type_checkbox['type'] else False)
                    for single in
                    ['breakfast', 'lunch', 'last_meal', 'morning', 'night']
                }
            if type_checkbox['type'] == 'night':
                type_true_or_false = {
                    single:
                    (True if single == type_checkbox['type'] else False)
                    for single in
                    ['breakfast', 'lunch', 'last_meal', 'morning', 'night']
                }

            for activity_id in type_checkbox['activity_ids']:
                try:
                    print(
                        DateNew.find_activitys_by_date_id_and_type(
                            chosen_date, activity_id,
                            **type_true_or_false).date)
                    _k = DateNew.find_activitys_by_date_id_and_type(
                        chosen_date, activity_id, **type_true_or_false).date
                    continue
                except:
                    category_id = Category.find_by_id(
                        Activity.find_by_id(activity_id).category_id).id
                    today = DateNew(date=chosen_date,
                                    **type_true_or_false,
                                    category_id=category_id,
                                    activity_id=activity_id,
                                    username_id=current_user.id)
                    DateNew.save_to_db(today)
                    flash('Looks like you added new records into database!')

        if type_checkbox['type'] == 'meal':
            list_time_of_meal = request.form.getlist('meal_time')
            list_meal_checkboxes = request.form.getlist('checkbox_meal')
            try:
                json_list_activity_from_meal = Meal.get_activities_by_time_of_meal_and_meal_id(
                    list_time_of_meal, list_meal_checkboxes)
                for single_type in DateNew.list_of_types():
                    try:
                        DateNew.add_to_db_by_list_and_type(
                            list_of_activities=json_list_activity_from_meal[
                                single_type],
                            type=single_type,
                            chosen_date=chosen_date)
                    except KeyError as e:
                        error = ('you have to fill form correctly')
                        continue
            except UnboundLocalError as e:
                error = ('you have to fill form correctly')
            except IndexError as e:
                error = ('you have to fill form correctly')

        today = DateNew.json_dict_list_of_done_activies_by_date(chosen_date)
        return render_template(
            'report_full_json.html',
            form=form_filter,
            chosen_date=chosen_date,
            chosen_date_objects=chosen_date_objects,
            done_activite_ids=done_activite_ids,
            meal_dict=meal_dict,
            activity_symptoms=activity_symptoms,
            activity_meals=activity_meals,
            today=today,
            error=error,
            date_info=date_info,
            all_date_records_for_chosen_date=all_date_records_for_chosen_date,
            data_for_prediction=data_for_prediction)
    return render_template(
        'report_full_json.html',
        form=form_filter,
        chosen_date=chosen_date,
        chosen_date_objects=chosen_date_objects,
        done_activite_ids=done_activite_ids,
        meal_dict=meal_dict,
        activity_symptoms=activity_symptoms,
        activity_meals=activity_meals,
        today=today,
        date_info=date_info,
        all_date_records_for_chosen_date=all_date_records_for_chosen_date,
        data_for_prediction=data_for_prediction)