Esempio n. 1
0
    def SaveProduct(self, item):
        # 保存地区信息
        zone = Zone()
        zone.name = item["zone"][0]
        zone.save()
        # 保存分类信息
        category = Category()
        category.category = item["category"][0]
        category.save()
        # 保存产品信息
        product = Product()
        product.name = item["product"][0]
        product.zone = zone
        product.category = category
        product.save()

        # 保存产品详细信息
        product_detail = ProductDetail()
        product_detail.product = product
        product_detail.price = float(item["price"][0])
        # 这里有问题,需要优化
        try:
            product_detail.price = float(item["price"][0])
            product_detail.star = item["star"][0]
        except Exception, e:
            product_detail.price = float(item["price"])
            product_detail.star = 0
Esempio n. 2
0
    def SaveProduct(self, item):
        # 保存地区信息
        zone = Zone()
        zone.name = item["zone"][0]
        zone.save()
        # 保存分类信息
        category = Category()
        category.category = item["category"][0]
        category.save()
        # 保存产品信息
        product = Product()
        product.name = item["product"][0]
        product.zone = zone
        product.category = category
        product.save()

        # 保存产品详细信息
        product_detail = ProductDetail()
        product_detail.product = product
        product_detail.price = float(item["price"][0])
        # 这里有问题,需要优化
        try:
            product_detail.price = float(item["price"][0])
            product_detail.star = item["star"][0]
        except Exception, e :
            product_detail.price = float(item["price"])
            product_detail.star = 0
Esempio n. 3
0
    def product_updater():
        """Makes new product insertion in database"""
        # setting the logger
        logger = logging.getLogger(__name__)
        # emptying the database

        i = 0
        # for each category in category list
        for elem in dt.CAT_LIST:
            # Replace some characters with others, so we can use it in API
            elem = elem.replace(" ", "-")
            elem = unidecode(elem)
            # get id list using Requester class
            category = Category.objects.get(category_name=elem)
            id_list = Requester(elem).product_id_list
            # for each product id in id list
            for product_id in id_list:
                # gather product data with Requester class
                product_data = Product_data(
                    Requester.product_data_requester(product_id))
                # create a product in database
                product = Product(
                    product_name=product_data.name,
                    product_url=product_data.url,
                    product_img=product_data.img,
                    product_nutriscore=product_data.nutriscore,
                    product_category_id=category,
                )
                # create nutriments data affiliated to product
                nutriments = Nutriments(
                    nutriments_product_id=product,
                    nutriments_kj=product_data.energy_kj,
                    nutriments_kcal=product_data.energy_kcal,
                    nutriments_lipids=product_data.lipids,
                    nutriments_fat=product_data.fat,
                    nutriments_carbohydrates=product_data.carbohydrates,
                    nutriments_sugar=product_data.sugar,
                    nutriments_protein=product_data.protein,
                    nutriments_salt=product_data.salt,
                    nutriments_sodium=product_data.sodium,
                )
                # save if ok
                try:
                    with transaction.atomic():
                        product.save()
                        nutriments.save()
                        i += 1
                        print(str(i) + " produit(s) ajouté(s)")
                # report error if not ok
                except DatabaseError as prod_error:
                    logger.error(prod_error)
                    pass