Beispiel #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
def save_category(data, parent, nsmap, status):
    collection_id = get_identifier(data, nsmap)
    if not collection_id:
        return
    category = get_category(data, parent, nsmap)
    if not category:
        category = Category()
        category.uid = collection_id
        category.parent = parent

    category.status = status
    collection_title = data.xpath('./ns:did/ns:unittitle', namespaces=nsmap)
    collection_descr = data.xpath('./ns:scopecontent/ns:p', namespaces=nsmap)
    collection_order_id = data.xpath('./ns:did/ns:unitid', namespaces=nsmap)

    if len(collection_title) and collection_title[0].text:
        category.title = collection_title[0].text
    if len(collection_order_id) and collection_order_id[0].text:
        category.order_id = collection_order_id[0].text
    if len(collection_descr) and collection_descr[0].text:
        category.description = collection_descr[0].text.replace('<lb/>',
                                                                ' ').strip()

    category.save()
    logger.info('dataimport.eadddb.category',
                'category %s saved' % category.id)
    return category
Beispiel #3
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
Beispiel #4
0
 def form_valid(self, form):
     text = form.cleaned_data['category_name']
     if Category.objects.filter(category_name=text.capitalize()):
         messages.error(self.request,
                        'Категория с таким названием уже существует!')
         return render(self.request, 'add.html', {})
     else:
         category = Category(category_name=text.capitalize())
         category.save()
     return self.get_success_url()
def save_category(parent, title):
    category = get_category(parent, title)
    if not category:
        category = Category()
        category.uid = title
        category.parent = parent
    category.title = title
    category.save()
    logger.info('dataimport.eadddb.category',
                'category %s saved' % category.id)
    return category
Beispiel #6
0
    def save(self):
        records = csv.reader(self.cleaned_data["file"], delimiter=';')

        for line in records:
            if line[1] == "BLC_NAME":
                continue

            balance = Balance.objects.filter(name=line[1])
            if not balance.exists():
                balance = Balance()
                balance.name = line[1]
                balance.save()
            else:
                balance = balance.get()

            group = Group.objects.filter(name=line[3])
            if not group.exists():
                group = Group()
                group.name = line[3]
                group.save()
            else:
                group = group.get()

            budget_user_group = BudgetUserGroup.objects.filter(name=line[5],
                                                               group=group.pk)
            if not budget_user_group.exists():
                budget_user_group = BudgetUserGroup()
                budget_user_group.group = group
                budget_user_group.name = line[5]
                budget_user_group.save()
            else:
                budget_user_group = budget_user_group.get()

            budget_user = BudgetUser.objects.filter(name=line[7],
                                                    group=budget_user_group.pk)
            if not budget_user.exists():
                budget_user = BudgetUser()
                budget_user.group = budget_user_group
                budget_user.name = line[7]
                budget_user.save()
            else:
                budget_user = budget_user.get()

            political_branch = PoliticalBranch.objects.filter(name=line[9])
            if not political_branch.exists():
                political_branch = PoliticalBranch()
                political_branch.name = line[9]
                political_branch.save()
            else:
                political_branch = political_branch.get()

            programme = Programme.objects.filter(name=line[11])
            if not programme.exists():
                programme = Programme()
                programme.name = line[11]
                programme.save()
            else:
                programme = programme.get()

            category = Category.objects.filter(name=line[13],
                                               balance=balance.pk,
                                               budget_user=budget_user.pk,
                                               political_branch=political_branch.pk,
                                               programme=programme.pk)
            if not category.exists():
                category = Category()
                category.balance = balance
                category.budget_user = budget_user
                category.political_branch = political_branch
                category.programme = programme
                category.name = line[13]
                category.save()
            else:
                category = category.get()

            item = Item.objects.filter(category=category.pk, budget_year=self.cleaned_data["year"])
            if not item.exists():
                item = Item()
                item.category = category
                item.budget_year = self.cleaned_data["year"]
                item.value = float(line[14].replace(",", "."))
                item.save()
            else:
                item = item.get()
Beispiel #7
0
    def product_inserter():
        """Makes category and product insertion in database"""
        # setting the logger
        logger = logging.getLogger(__name__)
        # emptying the database
        Category.objects.all().delete()
        Product.objects.all().delete()
        Nutriments.objects.all().delete()

        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(" ", "-")
            # create a category in database
            category = Category(category_name=unidecode(elem))
            # save if ok
            try:
                with transaction.atomic():
                    category.save()
            # report error if not ok
            except DatabaseError as cat_error:
                logger.error(cat_error)
                pass

            # get id list using Requester class
            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