Ejemplo n.º 1
0
def get_remaning_products(school_data, product_type):
    data = DatabaseManager.get_product_no(session.get('program_id'), product_type)
    schools = DatabaseManager.get_all_schools_with_contract(session.get('program_id'))
    products = DatabaseManager.get_products(session.get('program_id'), product_type)

    for school in schools:
        current_contract = DatabaseManager.get_current_contract(school.id, session.get('program_id'))
        if not current_contract:
            continue
        kids_no = current_contract.dairy_products

        if not kids_no or kids_no == 0:
            continue

        # Prepare empty statistics
        if not school_data.get(school.nick, None):
            school_data[school.nick] = list()
            for product in products:
                school_data[school.nick].append(ProductStats(product, kids_no))

        for d in data:
            if school == d[0]:
                for product_stats in school_data[school.nick]:
                    if product_stats.product_name == d[
                        1].get_name_mapping():  # @TODO this comparizon should be moved to Product, check how to compare to objects
                        product_stats.decrease_product(d[2])

    product_remaining = dict()
    for key, value in school_data.items():
        for v in value:
            if not product_remaining.get(v.product_name, None):
                product_remaining[v.product_name] = 0
            product_remaining[v.product_name] += v[3]
    return product_remaining
Ejemplo n.º 2
0
def create_records():
    if not str(session.get('program_id')):
        return redirect(url_for('program'))
    all_weeks = DatabaseManager.get_weeks(session.get('program_id'))
    all_schools = DatabaseManager.get_all_schools_with_contract(session.get('program_id'))
    if request.method == 'POST':
        school_id = request.form.get('wz_school', None)
        if school_id:
            return redirect(url_for('school_records', school_id=school_id))
    return render_template("create_records.html", Weeks=all_weeks, School=all_schools)
Ejemplo n.º 3
0
def program_form(program_id=INVALID_ID):
    config_parser.read(config_file)
    if program_id == INVALID_ID:
        id_of_program_being_added = DatabaseManager.id_of_program_being_added(FILL_STR)
        if not id_of_program_being_added:
            default_date = DateConverter.to_string(datetime.datetime.now())
            new_program = Program(semester_no=FILL_STR, school_year=FILL_STR, fruitVeg_price=FILL_STR,
                                  dairy_price=FILL_STR, start_date=DateConverter.to_date(default_date),
                                  end_date=DateConverter.to_date(default_date),
                                  dairy_min_per_week=FILL_STR, fruitVeg_min_per_week=FILL_STR, dairy_amount=FILL_STR,
                                  fruitVeg_amount=FILL_STR)
            if DatabaseManager.add_row(new_program):
                return redirect(url_for('program_form', program_id=new_program.id))
            else:
                return redirect(url_for('program_form', program_id=INVALID_ID))
        else:
            return redirect(url_for('program_form', program_id=id_of_program_being_added.id))

    current_program = DatabaseManager.get_program(program_id)
    school_without_summary = None
    summaries_data = None
    if is_current_program_set(current_program, config_parser):
        schools_with_contract = DatabaseManager.get_all_schools_with_contract(current_program.id)
        available_summary = get_current_summaries(current_program.id)
        summaries_data = dict()
        if not available_summary:
            school_without_summary = schools_with_contract
        else:
            school_without_summary = [school for school in schools_with_contract
                                      if not any(
                    [DatabaseManager.get_application(school.id, summary.id) for summary in available_summary])]
            summaries_data = {
                k: ", ".join(
                    [application.school.nick for application in DatabaseManager.get_school_with_summary(k.id) if
                     application.school])
                for k in available_summary}

    if request.method == 'POST':
        data_to_update = {"semester_no": empty_if_none(request.form["semester_no"]),
                          "school_year": empty_if_none(request.form["school_year"]),
                          "fruitVeg_price": empty_if_none(request.form["fruitVeg_price"]),
                          "dairy_price": empty_if_none(request.form["dairy_price"]),
                          "start_date": empty_if_none(DateConverter.to_date(request.form["start_date"])),
                          "end_date": empty_if_none(DateConverter.to_date(request.form["end_date"])),
                          "dairy_min_per_week": empty_if_none(request.form["dairy_min_per_week"]),
                          "fruitVeg_min_per_week": empty_if_none(request.form["fruitVeg_min_per_week"]),
                          "dairy_amount": empty_if_none(request.form["dairy_amount"]),
                          "fruitVeg_amount": empty_if_none(request.form["fruitVeg_amount"])}
        program_id = DatabaseManager.update_program_data(current_program, **data_to_update)
        return redirect(url_for('program_form', program_id=program_id))
    return render_template("program_form.html", Program=current_program, Schools=school_without_summary,
                           Summary=summaries_data)
Ejemplo n.º 4
0
def get_fruitVeg_summary(weeks, state):
    fruitVeg_summary = dict()
    fruitVeg_summary['apple_all'] = 0
    fruitVeg_summary['pear_all'] = 0
    fruitVeg_summary['plum_all'] = 0
    fruitVeg_summary['strawberry_all'] = 0
    fruitVeg_summary['juice_all'] = 0
    fruitVeg_summary['carrot_all'] = 0
    fruitVeg_summary['radish_all'] = 0
    fruitVeg_summary['pepper_all'] = 0
    fruitVeg_summary['tomato_all'] = 0
    fruitVeg_summary['kohlrabi_all'] = 0

    schools = DatabaseManager.get_all_schools_with_contract(session.get('program_id'))
    for school in schools:
        fruitVeg = dict()
        fruitVeg['apple'] = DatabaseManager.get_product_amount(session.get('program_id'), school.id, ProductName.APPLE,
                                                               weeks, state)
        fruitVeg['pear'] = DatabaseManager.get_product_amount(session.get('program_id'), school.id, ProductName.PEAR,
                                                              weeks, state)
        fruitVeg['plum'] = DatabaseManager.get_product_amount(session.get('program_id'), school.id, ProductName.PLUM,
                                                              weeks, state)
        fruitVeg['strawberry'] = DatabaseManager.get_product_amount(session.get('program_id'), school.id,
                                                                    ProductName.STRAWBERRY, weeks, state)
        fruitVeg['juice'] = DatabaseManager.get_product_amount(session.get('program_id'), school.id, ProductName.JUICE,
                                                               weeks, state)
        fruitVeg['carrot'] = DatabaseManager.get_product_amount(session.get('program_id'), school.id,
                                                                ProductName.CARROT, weeks, state)
        fruitVeg['radish'] = DatabaseManager.get_product_amount(session.get('program_id'), school.id,
                                                                ProductName.RADISH, weeks, state)
        fruitVeg['pepper'] = DatabaseManager.get_product_amount(session.get('program_id'), school.id,
                                                                ProductName.PEPPER, weeks, state)
        fruitVeg['tomato'] = DatabaseManager.get_product_amount(session.get('program_id'), school.id,
                                                                ProductName.TOMATO, weeks, state)
        fruitVeg['kohlrabi'] = DatabaseManager.get_product_amount(session.get('program_id'), school.id,
                                                                  ProductName.KOHLRABI, weeks, state)

        fruitVeg_summary['apple_all'] += fruitVeg['apple']
        fruitVeg_summary['pear_all'] += fruitVeg['pear']
        fruitVeg_summary['plum_all'] += fruitVeg['plum']
        fruitVeg_summary['strawberry_all'] += fruitVeg['strawberry']
        fruitVeg_summary['juice_all'] += fruitVeg['juice']
        fruitVeg_summary['carrot_all'] += fruitVeg['carrot']
        fruitVeg_summary['radish_all'] += fruitVeg['radish']
        fruitVeg_summary['pepper_all'] += fruitVeg['pepper']
        fruitVeg_summary['tomato_all'] += fruitVeg['tomato']
        fruitVeg_summary['kohlrabi_all'] += fruitVeg['kohlrabi']

        fruitVeg_summary[school.nick] = fruitVeg
    return fruitVeg_summary
Ejemplo n.º 5
0
def get_dairy_summary(weeks, state):
    dairy_summary = dict()
    dairy_summary['milk_all'] = 0
    dairy_summary['yoghurt_all'] = 0
    dairy_summary['kefir_all'] = 0
    dairy_summary['cheese_all'] = 0
    schools = DatabaseManager.get_all_schools_with_contract(session.get('program_id'))
    for school in schools:
        dairy = dict()
        dairy['milk'] = DatabaseManager.get_product_amount(session.get('program_id'), school.id, ProductName.MILK,
                                                           weeks, state)
        dairy['yoghurt'] = DatabaseManager.get_product_amount(session.get('program_id'), school.id, ProductName.YOGHURT,
                                                              weeks, state)
        dairy['kefir'] = DatabaseManager.get_product_amount(session.get('program_id'), school.id, ProductName.KEFIR,
                                                            weeks, state)
        dairy['cheese'] = DatabaseManager.get_product_amount(session.get('program_id'), school.id, ProductName.CHEESE,
                                                             weeks, state)
        dairy_summary['milk_all'] += dairy['milk']
        dairy_summary['yoghurt_all'] += dairy['yoghurt']
        dairy_summary['kefir_all'] += dairy['kefir']
        dairy_summary['cheese_all'] += dairy['cheese']
        dairy_summary[school.nick] = dairy
    return dairy_summary
Ejemplo n.º 6
0
def test_get_all_school_withContract_is25():
    assert (len(DatabaseManager.get_all_schools_with_contract("1")) == 25)
Ejemplo n.º 7
0
def test_product_info():
    program_id = 1
    school_ids = [i for i in range(2, len(DatabaseManager.get_all_schools_with_contract(program_id)))]
    weeks = set([1, 2, 3, 4, 5, 6, 13])
    assert (DatabaseManager.get_program(program_id).school_year == '2018/2019')
    assert (DatabaseManager.get_program(program_id).semester_no == 1)
    assert (DatabaseManager.get_product_amount(program_id, school_ids[0], ProductName.MILK, weeks) == 5820)
    assert (DatabaseManager.get_product_amount(program_id, school_ids[0], ProductName.APPLE, weeks) == 582)
    assert (DatabaseManager.get_maxKids_perWeek(program_id, school_ids[0], ProductType.FRUIT_VEG, weeks) == 291)
    assert (DatabaseManager.get_maxKids_perWeek(program_id, school_ids[0], ProductType.DAIRY, weeks) == 291)
    assert ((len(DatabaseManager.get_records(program_id, school_ids[0], ProductType.FRUIT_VEG, weeks))) == 22)

    program_id = 5
    school_id = 1
    assert (DatabaseManager.get_product_amount(program_id, school_id, ProductName.APPLE, weeks) == 765)
    assert (DatabaseManager.get_product_amount(program_id, school_id, ProductName.MILK, weeks) == 1530)
    assert (DatabaseManager.get_maxKids_perWeek(program_id, school_id, ProductType.FRUIT_VEG, weeks) == 400)
    assert (DatabaseManager.get_maxKids_perWeek(program_id, school_id, ProductType.DAIRY, weeks) == 600)

    start_week = 1
    end_week = 12
    assert (DatabaseManager.get_dates(program_id, start_week) == "28.09-04.10\n2020")
    assert (DatabaseManager.get_dates(program_id, end_week) == "12.01-18.01\n2021")
    assert (DatabaseManager.get_maxKids_perWeek(program_id, school_id, ProductType.FRUIT_VEG,
                                                weeks=set([start_week])) == 310)
    assert (DatabaseManager.get_portion_perWeek(program_id, school_id, ProductType.DAIRY, 13) == 2)
    assert (DatabaseManager.get_portion_perWeek(program_id, school_id, ProductType.FRUIT_VEG, 13) == 3)
    assert (DatabaseManager.get_maxKids_perWeek(program_id, school_id, ProductType.FRUIT_VEG,
                                                weeks=set([end_week])) if end_week in weeks else "-" == "-")
    assert (DatabaseManager.get_portion_perWeek(program_id, school_id, ProductType.FRUIT_VEG,
                                                start_week) if start_week in weeks else "-" == 1)
    assert (DatabaseManager.get_portion_perWeek(program_id, school_id, ProductType.DAIRY,
                                                start_week) if start_week in weeks else "-" == 1)

    expected_weeks = prepare_weeks(program_id)
    cpi = CommonData(program_id, school_id, weeks)
    assert (cpi.get_week_numbers() == set([1, 2, 4, 13]))
    assert (cpi.length() == 4)
    cpi.prepare()
    for i in range(1, 13):
        assert (cpi.get()[f"week_date_{i}"] == expected_weeks[f"week_date_{i}"])

    fruits = Fruit(cpi)
    expected_data_fruits = prepare_fruit_data(program_id, school_id, weeks)
    assert (expected_data_fruits[0] == fruits.get_sum())
    assert (expected_data_fruits[2][ProductName.APPLE] == fruits.get_amount(ProductName.APPLE))
    assert (expected_data_fruits[2][ProductName.PEAR] == fruits.get_amount(ProductName.PEAR))
    assert (expected_data_fruits[2][ProductName.STRAWBERRY] == fruits.get_amount(ProductName.STRAWBERRY))
    assert (expected_data_fruits[2][ProductName.PLUM] == fruits.get_amount(ProductName.PLUM))
    assert (expected_data_fruits[2][ProductName.JUICE] == fruits.get_amount(ProductName.JUICE))
    vegs = Veg(cpi)
    expected_data_vegs = prepare_veg_data(program_id, school_id, weeks)
    assert (expected_data_vegs[0] == vegs.get_sum())
    assert (expected_data_vegs[2][ProductName.TOMATO] == vegs.get_amount(ProductName.TOMATO))
    assert (expected_data_vegs[2][ProductName.CARROT] == vegs.get_amount(ProductName.CARROT))
    assert (expected_data_vegs[2][ProductName.RADISH] == vegs.get_amount(ProductName.RADISH))
    assert (expected_data_vegs[2][ProductName.KOHLRABI] == vegs.get_amount(ProductName.KOHLRABI))
    assert (expected_data_vegs[2][ProductName.PEPPER] == vegs.get_amount(ProductName.PEPPER))

    dairy = Dairy(cpi)
    expected_data_dairy = prepare_dairy_data(program_id, school_id, weeks)
    assert (expected_data_dairy[0] == dairy.get_sum())
    assert (expected_data_dairy[2][ProductName.MILK] == dairy.get_amount(ProductName.MILK))
    assert (expected_data_dairy[2][ProductName.YOGHURT] == dairy.get_amount(ProductName.YOGHURT))
    assert (expected_data_dairy[2][ProductName.KEFIR] == dairy.get_amount(ProductName.KEFIR))
    assert (expected_data_dairy[2][ProductName.CHEESE] == dairy.get_amount(ProductName.CHEESE))

    fruit_veg_summary = FruitVegSummary(cpi)
    assert (expected_data_fruits[1] == fruit_veg_summary.get_kids())
    assert (expected_data_vegs[1] == fruit_veg_summary.get_kids())

    dairy_summary = DairySummary(cpi)
    assert (expected_data_dairy[1] == dairy_summary.get_kids())
Ejemplo n.º 8
0
def create_records_per_week(week_id):
    if not str(session.get('program_id')):
        return redirect(url_for('program'))
    selected_schools_product_view = dict()
    all_schools = DatabaseManager.get_all_schools_with_contract(
        session.get('program_id'))  # schools which don't have record for this day
    record_context = {
        'schools_with_contracts': all_schools,
        'products_dairy': DatabaseManager.get_dairy_products(session.get('program_id')),
        'products_veg': DatabaseManager.get_fruitVeg_products(session.get('program_id')),
        'current_week': DatabaseManager.get_week(week_id, session.get('program_id')),
        'datetime': datetime,
        'weeks': DatabaseManager.get_weeks(session.get('program_id')),
        'selected_schools_product_view': selected_schools_product_view,
    }
    duplicated_records_set = set()
    if request.method == 'POST':
        # @TODO clean code: this is showing the school slector
        current_date = request.form.get('school_selector', None)
        if current_date and current_date not in record_context['selected_schools_product_view'].keys():
            record_context['selected_schools_product_view'][current_date] = list()
            school_list_req = request.form.getlist('schools_' + current_date)
            if school_list_req:
                for school_id in school_list_req:
                    school = DatabaseManager.get_school(school_id)
                    record_context['selected_schools_product_view'][current_date].append(school)

            selected_schools_product_view.update(record_context['selected_schools_product_view'])
            return render_template("create_records.html", **record_context)

        if request.form['record_selector']:
            record_data = request.form.to_dict(flat=False)
            current_date = record_data.pop('record_selector')[0]
            record_list = list()
            for school_key, product_list in record_data.items():
                school_id = RecordCreator.extract_school_id(school_key)
                for product_id in product_list:
                    if not product_id:
                        continue
                    product_record: Record = DatabaseManager.get_existing_record(current_date, school_id, product_id)
                    product: Product = DatabaseManager.get_product(product_id)
                    assert (isinstance(product_record, Record) or product_record is None)
                    if not product_record:
                        rc = RecordCreator(session.get('program_id'), current_date, school_id, product_id)
                        rc.create()
                        record_list.append(rc)
                    else:
                        duplicated_records_set.add(
                            "{}: '{}' istnieje WZ dla '{}'".format(DatabaseManager.get_school(school_id).nick,
                                                                   product.get_name_mapping(),
                                                                   product_record.product.get_name_mapping()))
            RecordCreator.generate_many(record_list, RECORDS_NEW_NAME)
            # REGENERATE_FOR_ALL
            generation_date = datetime.date.today()
            existing_daily_records = DatabaseManager.get_daily_records(session.get('program_id'), current_date,
                                                                       generation_date)
            RecordCreator.regenerate_documentation(existing_daily_records)
            dup_record_msg = ""
            if duplicated_records_set:
                dup_record_msg = ", ".join(duplicated_records_set)
            return redirect(url_for('record_created', current_date=current_date, week_id=week_id,
                                    duplicated_records=dup_record_msg))
    return render_template("create_records.html", **record_context)
Ejemplo n.º 9
0
def schools_with_contract():
    if not session.get('program_id'):
        return redirect(url_for('program'))
    all_schools = DatabaseManager.get_all_schools_with_contract(session.get('program_id'))
    return render_template("schools_all.html", Schools=all_schools, program_id=session.get('program_id'),
                           invalid_school_id=INVALID_ID)