예제 #1
0
    def setUp(self):
        user = My_user.objects.create_user("cyril", "*****@*****.**",
                                           "my_password")
        user.last_name = "simonin"
        user.first_name = "cyril"
        user.save()

        main_categorie = Main_categorie.objects.create(name="boisson")
        produit1 = Product(id=5449000000996,
                           main_categorie=main_categorie,
                           nom="coca",
                           indice="e",
                           url="http://url.fr/openfoodfact",
                           url_img="http://url.fr/openfoodfact/image")
        produit1.save()

        produit2 = Product(id=5449000133335,
                           main_categorie=main_categorie,
                           nom="Coca Cola Zero",
                           indice="e",
                           url="http://url.fr/openfoodfact",
                           url_img="http://url.fr/openfoodfact/image")
        produit2.save()

        Substitute.objects.create(product_initial=produit1,
                                  product_substitute=produit2,
                                  myuser=user)

        self.client.login(username="******", password="******")
예제 #2
0
    def insert_data(self, receiv_data):
        """Insert the data after sorting in the database."""
        list_sub_cat = self.formating_data(receiv_data.pop(4))
        sorting_nutriment = self.sorting_nutriment(receiv_data.pop(6))

        if Product.objects.filter(id=receiv_data[0]).exists() is False:
            # ici on creer le produit car l'id n'existe pas
            produit = Product(id=receiv_data[0],
                              main_categorie=receiv_data[1],
                              nom=receiv_data[2],
                              indice=receiv_data[3],
                              url=receiv_data[4],
                              url_img=receiv_data[5])
            produit.save()
        else:
            # ici on récupère le produit car il existe
            produit = Product.objects.get(id=receiv_data[0])

        for name, quantity in sorting_nutriment.items():
            nutriment = Nutriment.objects.get_or_create(nutriment_name=name,
                                                        quantity=quantity)
            nutriment[0].product.add(produit)

        for sub_cat in list_sub_cat:
            sub_categorie = Sub_categorie.objects.get_or_create(name=sub_cat)
            if Sub_categorie.objects.filter(
                    name=sub_cat,
                    product__id=receiv_data[0]).exists() is False:
                sub_categorie[0].product.add(produit)
예제 #3
0
def mongo(request):
    db = me.connect('testdb', host='mongomock://localhost')

    for product in list_products:
        db_product = Product(**product)
        db_product.save()

    yield db
    db.drop_database('testdb')
    db.close()
예제 #4
0
    def handle(self, *args, **options):
        a = 0
        new_categories_1 = Categories(name="Petit déjeuné",
                                      url="https://world.openfoodfacts.org/"
                                      "category/fr:petit-dejeune")
        new_categories_2 = Categories(name='Pâtes à tartiner',
                                      url="https://world.openfoodfacts.org/"
                                      "category/"
                                      "fr:P%C3%A2tes%20%C3%A0%20tartiner")
        new_categories_3 = Categories(name='Produits origine Végetal',
                                      url="https://world.openfoodfacts.org/"
                                      "category/fr:origine-vegetal")
        new_categories_1.save()
        new_categories_2.save()
        new_categories_3.save()

        while a < 1:
            all_Categories = Categories.objects.all()
            for save_products in all_Categories:
                id_s = requests.get(
                    "https://world.openfoodfacts.org/api/v0/product/" +
                    '3017620422003' + ".json")
                id_r = id_s.json()
                if 'product' in id_r:
                    save = id_r['product']
                    if 'product_name' not in save:
                        save['product_name'] = ''
                    if 'nutrition_grades' not in save:
                        save['nutrition_grades'] = ''
                    if 'image_url' not in save:
                        save['image_url'] = ''
                    if 'ingredients_text_fr' in save:
                        save['ingredients_text_fr'] = \
                            save['ingredients_text_fr']
                    if 'ingredients_text_fr' not in save:
                        save['ingredients_text_fr'] = ''

                    if save['id'] not in all_Categories:
                        Categories_id = \
                            Categories.objects.get(pk=save_products.pk)

                        url = "https://fr.openfoodfacts.org/" \
                              "product/" + save['id'] + "/" + \
                              save['product_name']

                        a += 1
                        new_products = Product(
                            name=save['product_name'],
                            image_url=save['image_url'],
                            code=save['id'],
                            nutrition_grade=save['nutrition_grades'],
                            ingredients=save['ingredients_text_fr'],
                            url=url,
                            categories=Categories_id)
                        new_products.save()
예제 #5
0
def import_db(request):
    if request.method == 'POST' and request.FILES['my_file']:
        my_file = request.FILES['my_file'].read().decode('utf-8')
        data = json.loads(my_file)
        for item in data['result']:
            shop = Shop(name=item['shop']['name'], address=item['shop']['address'],
                        phone=item['shop']['phone'], email=item['shop']['email'],
                        longitude=item['shop']['location']['longitude'], latitude=item['shop']['location']['latitude'],
                        shop_src_id=item['shop']['shopId'])
            shop.save()
            product = Product(name=item['name'], price=float(item['price']), image_link=item['image'],
                              category=item['category'], product_link=item['link'],
                              product_src_id=item['productId'], description=item['description'],
                              shop=shop)
            product.save()
        return render(request, 'success.html')
    return render(request, 'import.html')
예제 #6
0
파일: fill_db.py 프로젝트: flenoir/foodswap
    def handle(self, *args, **options):

        CATEGORIES_ARRAY = [
            'petit-dejeuners', 'plats-prepares', 'snacks-sales',
            'biscuits-et-gateaux', 'snacks-sucres', 'produits-laitiers',
            'epicerie', 'desserts', 'charcuteries', 'cereales-et-derives'
        ]

        for index, value in enumerate(CATEGORIES_ARRAY):

            temp_var = "var" + str(index)

            temp_var = requests.get(
                "https://fr.openfoodfacts.org/cgi/search.pl?action=process&tagtype_0=labels&tag_contains_0=contains&tag_0=sans%20gluten&tagtype_1=categories&tag_contains_1=contains&tag_1={}&sort_by=unique_scans_n&page_size=100&axis_x=energy&axis_y=products_n&action=display&json=1"
                .format(value)).json()

            for x, i in enumerate(temp_var['products']):

                try:
                    single_brand = re.findall("^([^,]*)", str(i['brands']))
                except KeyError:
                    single_brand = "none"

                try:
                    x = Product(product_name=i['product_name_fr'].casefold(),
                                brands=str(single_brand),
                                description=i['generic_name_fr'].casefold(),
                                product_url=i['url'],
                                product_code=i['code'],
                                product_image=i['image_ingredients_url'],
                                nutriscore=i['nutrition_grades'],
                                stores=i['stores_tags'],
                                quantity=i['quantity'],
                                nova_groups=i['nova_groups'],
                                categories=i['categories'])
                    x.save()
                    self.stdout.write(str(x.id))
                except KeyError as e:
                    print(e)
                    self.stdout.write(str(e))
예제 #7
0
    def handle(self, *args, **options):
        list_total_products = []
        total_products = int()
        result = requests.get("https://fr.openfoodfacts.org/categories.json")
        response = result.json()
        i = 0
        a = 0
        while i != len(response['tags']):
            new_categories = Categories(name=response['tags'][i]['name'],
                                        url=response['tags'][i]['url'])
            list_total_products.append(int(response['tags'][i]['products']))
            total_products = sum(list_total_products)
            i += 1
            new_categories.save()

        while a != total_products:
            print(total_products)
            all_Categories = Categories.objects.all()
            for save_products in all_Categories:
                result_products = requests.get(save_products.url + ".json")
                try:
                    response_products = result_products.json()
                except JSONDecodeError:
                    continue
                for save in response_products['products']:
                    if 'id' in save:
                        id_s = requests.get(
                            "https://world.openfoodfacts.org/api/v0/product/" +
                            save['id'] + ".json")
                        id_r = id_s.json()

                        if 'product' in id_r:
                            save = id_r['product']
                            if 'product_name' not in save:
                                save['product_name'] = ''
                            if 'nutrition_grades' not in save:
                                save['nutrition_grades'] = ''
                            if 'image_url' not in save:
                                save['image_url'] = ''
                            if 'ingredients_text_fr' in save:
                                save['ingredients_text_fr'] = \
                                    save['ingredients_text_fr']
                            if 'ingredients_text_fr' not in save:
                                save['ingredients_text_fr'] = ''

                            if save['id'] not in all_Categories:
                                Categories_id = \
                                    Categories.objects.get(pk=save_products.pk)

                                url = "https://fr.openfoodfacts.org/" \
                                      "product/" + save['id'] + "/" + \
                                      save['product_name']

                                a += 1
                                new_products = Product(
                                    name=save['product_name'],
                                    image_url=save['image_url'],
                                    code=save['id'],
                                    nutrition_grade=save['nutrition_grades'],
                                    ingredients=save['ingredients_text_fr'],
                                    url=url,
                                    categories=Categories_id)
                                new_products.save()
예제 #8
0
    def handle(self, *args, **options):
        dic = ["Produits à tartiner", "Plats préparés", "Céréales pour petit-déjeuner", "Pizzas", "Confiseries",
               "Boissons"]
        cpt = 0
        cpt_product = 0
        cpt_boucle = 0
        while len(dic) > cpt:
            category = Category(name=dic[cpt])
            print(category.name)
            category.save()
            cpt += 1

        while cpt_boucle <= 5:
            while cpt_product < 100:
                try:
                    r = requests.get("https://fr.openfoodfacts."
                                     "org/cgi/search.pl?action=process"
                                     "&tagtype_0=categories&tag_contains_"
                                     "0=contains&tag_0=" + str(dic[cpt_boucle]) + "&json=true&page_size=200")
                    results = r.json()["products"]
                    name = results[cpt_product]["product_name"]
                    score = results[cpt_product]["nutriscore_grade"]
                    img_url = results[cpt_product]["image_small_url"]
                    url = results[cpt_product]["url"]
                    ingredients = results[cpt_product]["ingredients_text_fr"]

                    print(name, score, img_url)
                    cpt_product += 1
                    print(cpt_product)
                    try:
                        Product.objects.get(name=name)
                    except ObjectDoesNotExist:
                        cat = Category.objects.get(id=cpt_boucle + 1)
                        product_add = Product(name=name, score=score, img_url=img_url, category_product=cat, url=url,
                                              ingredient=ingredients)
                        product_add.save()

                except KeyError:
                    r = requests.get("https://fr.openfoodfacts."
                                     "org/cgi/search.pl?action=process"
                                     "&tagtype_0=categories&tag_contains_"
                                     "0=contains&tag_0=" + str(dic[cpt_boucle]) + "&json=true&page_size=200")
                    results = r.json()["products"]
                    name = results[cpt_product]["product_name"]
                    score = 'e'
                    try:
                        img_url = results[cpt_product]["image_thumb_url"]
                    except KeyError:
                        img_url = "https://upload.wikimedia.org/wikipedia/commons/a/ac/No_image_available.svg"
                    print(name, score, img_url)
                    cpt_product += 1
                    print(cpt_product)
                    try:
                        Product.objects.get(name=name)
                    except ObjectDoesNotExist:
                        cat = Category.objects.get(id=cpt_boucle + 1)
                        product_add = Product(name=name, score=score, img_url=img_url, category_product=cat, url=url,
                                              ingredient=ingredients)
                        product_add.save()

            cpt_product = 0
            cpt_boucle += 1
예제 #9
0
    def handle(self, *args, **options):

        CATEGORIES_ARRAY = [
            "petit-dejeuners",
            "plats-prepares",
            "snacks-sales",
            "biscuits-et-gateaux",
            "snacks-sucres",
            "produits-laitiers",
            "epicerie",
            "desserts",
            "charcuteries",
            "cereales-et-derives",
            "viandes",
            "aliments-et-boissons-a-base-de-vegetaux",
            "aliments-a-base-de-fruits-et-de-legumes",
            "produits-de-la-mer",
            "conserves",
            "pates-alimentaires",
            "aliments-d-origine-vegetale",
            "cereales-et-pommes-de-terre",
            "produits-a-tartiner",
        ]

        for index, value in enumerate(CATEGORIES_ARRAY):

            temp_var = "var" + str(index)

            temp_var = requests.get(
                "https://fr.openfoodfacts.org/cgi/search.pl?action=process&tagtype_0=labels&tag_contains_0=contains&tag_0=sans%20gluten&tagtype_1=categories&tag_contains_1=contains&tag_1={}&sort_by=unique_scans_n&page_size=100&axis_x=energy&axis_y=products_n&action=display&json=1"
                .format(value)).json()

            for x, i in enumerate(temp_var["products"]):

                try:
                    single_brand = re.findall("^([^,]*)", str(i["brands"]))
                except KeyError:
                    single_brand = "none"

                try:
                    x = Product(
                        product_name=i["product_name_fr"].casefold(),
                        brands=str(single_brand),
                        description=i["generic_name_fr"].casefold(),
                        product_url=i["url"],
                        product_code=i["code"],
                        product_image=i["image_front_url"],
                        nutriscore=i["nutrition_grades"],
                        stores=i["stores_tags"],
                        quantity=i["quantity"],
                        nova_groups=i["nova_groups"],
                        categories=i["categories"],
                        fat_100g=i["nutriments"]["fat_100g"],
                        saturated_fat_100g=i["nutriments"]
                        ["saturated-fat_100g"],
                        sugars_100g=i["nutriments"]["sugars_100g"],
                        salt_100g=i["nutriments"]["salt_100g"],
                    )
                    x.save()
                    self.stdout.write(str(x.product_code))
                except KeyError as e:
                    print(e)
                    self.stdout.write(str(e))