Esempio n. 1
0
def save_products(data):
    for i in data:
        try:
            if db.session.query(
                    Product.query.filter(
                        Product.name == i['name']).exists()).scalar():
                return
            b_id = Brand.query.with_entities(Brand.id).filter(
                Brand.name == i['manufacturer']).limit(1).scalar()
            try:
                if not b_id:
                    b = Brand()
                    b.name = i['manufacturer']
                    db.session.add(b)
                    db.session.commit()
                    b_id = b.id
            except (IntegrityError, OperationalError,
                    InvalidRequestError) as e:
                db.session.rollback()
                print(e)

            product = Product()
            product.name = i['name']
            product.url = i['url']
            product.brand_id = b_id
            product.therapeutic_name = i['genericname']
            product.price = i['price']['INR']['default']
            product.drug_type = i['drug_type']
            product.formulation_types = i['formulation_types']
            product.dosage = i['dosage'] if 'dosage' in i else ""
            db.session.add(product)
            db.session.commit()

            if 'categories_without_path' and i['categories_without_path']:
                for x in i['categories_without_path']:
                    c_id = Category.query.with_entities(Category.id).filter(
                        Category.name == x).limit(1).scalar()
                    try:
                        if not c_id:
                            c = Category()
                            c.name = x
                            db.session.add(c)
                            db.session.commit()
                            c_id = c.id
                        if c_id:
                            pc = ProductCategory()
                            pc.product_id = product.id
                            pc.category_id = c_id
                            db.session.add(pc)
                            db.session.commit()

                    except (IntegrityError, OperationalError,
                            InvalidRequestError) as e:
                        db.session.rollback()
                        print(e)
        except (IntegrityError, OperationalError, InvalidRequestError) as e:
            db.session.rollback()
            print(e)
    pass
Esempio n. 2
0
def save_med_products(data):
    for i in data:
        if 'productName' not in i:
            continue
        try:
            if db.session.query(
                    Product.query.filter(
                        func.lower(Product.name) == func.lower(
                            i['productName'])).exists()).scalar():
                continue
            b_id = Brand.query.with_entities(Brand.id).filter(
                func.lower(Brand.name) == func.lower(i['manufacturer'])).limit(
                    1).scalar()
            try:
                print(i['productName'])
                if not b_id:
                    b = Brand()
                    b.name = i['manufacturer']
                    db.session.add(b)
                    db.session.commit()
                    b_id = b.id
            except (IntegrityError, OperationalError,
                    InvalidRequestError) as e:
                db.session.rollback()
                print(e)

            product = Product()
            product.name = i['productName']
            product.brand_id = b_id
            # product.therapeutic_name = i['genericname']
            product.price = i['packSizeMrp']
            # product.drug_type = i['drug_type']
            product.formulation_types = i['productFormName']
            # product.dosage = i['dosage'] if 'dosage' in i else ""
            db.session.add(product)
            db.session.commit()

        except (IntegrityError, OperationalError, InvalidRequestError) as e:
            db.session.rollback()
            print(e)
    pass