File size: 830 Bytes
0277ad1
 
 
 
 
 
 
adc3425
0277ad1
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from ..models import Product


def save_product(product):
    #check if product is already in the database and store_name is same
    if Product.objects.filter(product_id=product['product_id'], store_name=product['store_name']).exists():
        # update the product
        product_to_update = Product.objects.filter(product_id=product['product_id'], store_name=product['store_name'])[0]
        product_to_update.title = product['title']
        product_to_update.ingredients = product['ingredients']
        product_to_update.url = product['url']
        product_to_update.save()
        
        return False
    
    new_product = Product(title=product['title'], ingredients=product['ingredients'], product_id=product['product_id'], url=product['url'], store_name=product['store_name'])
    new_product.save()
    return True