コード例 #1
0
def result(request):

    if request.method == "GET":
        code = request.GET.get('query')
        """ GET CATEGORY AND NUTRISCORE FROM SELECTED PRODUCT """
        datas = build_data(code, "code")
        """ GET SUBSTITUTES """
        data_product = [
            datas["products"][0]["category"],
            datas["products"][0]["nutriscore"],
        ]
        datas["substitutes"] = build_data(data_product, "substitute")

        return render(request, 'results.html', datas)
コード例 #2
0
def test_if_builder_by_substitute_return_expected_datas(monkeypatch):

    # MOCK RESPONSE
    class MockResponse:
        def json(*args, **kwargs):
            mock_url = (
                "P8_PurBeurre/webapp/modules/api/tests/mocks/mock_by_categ.json"  # noqa
            )
            with open(mock_url, encoding="utf-8") as json_file:
                mock_resp = json.load(json_file)
            return mock_resp

    # PATCH URL
    def mock_requests_url_answer(url):
        return MockResponse()

    # PATCH REQUEST
    monkeypatch.setattr(
        "webapp.modules.api.requests_library.request_categ.requests.get",
        mock_requests_url_answer,
    )

    response = build_data(["mock_categorie", "e"], "substitute")
    # TEST TYPE OF RESPONSE
    assert isinstance(response[0], dict)
    # TEST NUMB OF DATAS
    assert len(response) == 6
コード例 #3
0
def test_if_builder_by_name_return_expected_datas(monkeypatch):

    # MOCK RESPONSE
    class MockProducts:
        def json(*args, **kwargs):
            mock_url = (
                "P8_PurBeurre/webapp/modules/api/tests/mocks/mock_products.json"  # noqa
            )
            with open(mock_url, encoding="utf-8") as json_file:
                mock_resp = json.load(json_file)
            return mock_resp

    # PATCH URL
    def mock_requests_url_answer_prod(url):
        return MockProducts()

    # PATCH REQUEST
    monkeypatch.setattr(
        "webapp.modules.api.requests_library.request_products.requests.get",  # noqa
        mock_requests_url_answer_prod,
    )
    """ TEST - WITH NAME """
    test_products = build_data("mock_name", "prod_name")

    # TEST TYPE OF RESPONSE
    assert isinstance(test_products, dict)
    # TEST NUMB OF PRODUCT IN RESPONSE
    assert len(test_products["products"]) == 6
コード例 #4
0
def test_if_builder_by_code_return_expected_datas(monkeypatch):

    # MOCK RESPONSE
    class MockResponse:
        def json(*args, **kwargs):
            mock_url = (
                "P8_PurBeurre/webapp/modules/api/tests/mocks/mock_code.json"  # noqa
            )
            with open(mock_url, encoding="utf-8") as json_file:
                mock_resp = json.load(json_file)
            return mock_resp

    # PATCH URL

    def mock_requests_url_answer(url):

        return MockResponse()

    # PATCH REQUEST
    monkeypatch.setattr(
        "webapp.modules.api.requests_library.request_code.requests.get",
        mock_requests_url_answer,
    )
    """ TEST - WITH CODE """
    test_products = build_data("mock_code", "code")

    assert isinstance(test_products, dict)
    assert len(test_products["products"][0]) == 15
コード例 #5
0
def myfood(request):

    products = Save.objects.filter(user__iregex=f"{request.user.email}")
    list_of_products = list()
    list_of_substitute = list()
    dict_product = {}

    for product in products:
        prod_code = product.substitute
        datas = build_data(prod_code, "code")
        list_of_products.append(datas["products"][0])

        sub_code = product.product_substitued
        datas = build_data(sub_code, "code")
        list_of_substitute.append(datas["products"][0])

    dict_product["products"] = list_of_products
    dict_product["substitutes"] = list_of_substitute

    return render(request, "myfood.html", dict_product)
コード例 #6
0
def selection(request):

    if request.method == "POST":
        query = request.POST.get('user_text')
        query_clnd = remove_special_char(query, "add_space")
        if query_clnd != "":
            datas = build_data(query_clnd, "prod_name")
            return render(request, 'selection.html', datas)
        else:
            return redirect('index')
    else:
        return redirect('index')
コード例 #7
0
def save(request):
    if request.method == "GET":
        data = request.GET.get('query')
        datas = data.replace(",", " ").split()

        if request.user.is_authenticated:
            for code in datas:
                products = build_data(code, "code")
                product = products["products"][0]
                try:
                    data_prod = [
                        product["product_name"],
                        product["product_url"],
                        product["product_img"],
                        product["category"],
                        product["nutriscore"],
                        product["code"],
                    ]
                    data_nutr = [
                        product["code"],
                        product["energy_100g"],
                        product["energy_unit"],
                        product["proteins_100g"],
                        product["fat_100g"],
                        product["saturated_fat_100g"],
                        product["carbohydrates_100g"],
                        product["sugars_100g"],
                        product["fiber_100g"],
                        product["salt_100g"],
                    ]

                    add_data(data_prod)
                    add_nutr_data(data_nutr)

                except KeyError as error:
                    print(KeyError, error)
                    pass

            datas.append(request.user.email)
            save_research(datas)
            return redirect('myfood')

        else:
            return redirect('signin')
コード例 #8
0
def test_if_builder_return_None():

    response = build_data("mock_data", "mock_wrong_parameter")
    assert response is None