Ejemplo n.º 1
0
def addNewProduct():
    current_id = get_jwt_identity()
    img = request.json.get("image", None)
    sellerid = request.json.get("sellerid", None)
    productName = request.json.get("productname", None)
    description = request.json.get("description", None)
    category = request.json.get("category", None)
    price = request.json.get("price", None)
    item_status = request.json.get("itemstatus", None)

    if not img:
        return jsonify({"msg": "No image was uploaded!."}), 400
    if not description:
        return jsonify({"msg": "Please enter a valid description"}), 400
    if not category:
        return jsonify({"msg": "Please enter a valid category"}), 400
    if not price:
        return jsonify({"msg": "Please enter a valid price"}), 400
    if not item_status:
        return jsonify({"msg": "Please enter a valid item status"}), 400
    else:
        new_product = Product()
        new_product.productName = productName
        new_product.fk_id = sellerid
        new_product.description = description
        new_product.category = category
        new_product.price = price
        new_product.item_status = item_status
        new_product.img = img

        db.session.add(new_product)
        db.session.commit()
        return jsonify(
            {"msg": "The product has being successfully registered."}), 200
def main():

    # read the json data
    with open('data.json') as json_data:
        data = json.load(json_data)

    cats = {}
    i = 1

    # find the categories, and replace them with the
    # new category id (created below)
    for prod in data['data'].values():
        if prod['category'] not in cats:
            cats[prod['category']] = i
            i += 1
        prod['category'] = cats[prod['category']]

    # create the categories
    for cat_name, cat_id in cats.items():
        new_cat = Category()
        new_cat.id = cat_id
        new_cat.title = cat_name
        new_cat.save()

    # create the products
    for prod in data['data'].values():
        p = Product()
        p.name = prod['name']
        p.description = prod['description']
        p.price = prod['price']
        p.filename = prod['filename']
        p.category = Category.objects.get(id=prod['category'])
        p.save()
Ejemplo n.º 3
0
def main():
    with open('products.json') as json_file:
        data = json.load(json_file)
    products = data['products']

    for p in products:
        print(p['name'])

        # category
        cat, created = Category.objects.get_or_create(title=p['category'])

        # product
        if Product.objects.filter(name=['name']).count() == 0:
            product = Product()
            product.category = cat
            product.name = p['name']
            product.filename = p['filename']
            product.id = int(p['id'])
            product.description = p['description']
            product.price = p['price']
            product.save()
def main():

    with open('products.json') as json_file:
        data = json.load(json_file)
    products = data['products']
    cats = []

    for prod in products:
        if prod["category"] in cats:
            dbcat = Category.objects.get(title=prod["category"])
            print('skip')
        else:
            dbcat = Category()
            dbcat.title = prod["category"]
            dbcat.save()
            cats.append(dbcat.title)
        dbprod = Product()
        dbprod.category = dbcat
        dbprod.name = prod["name"]
        dbprod.description = prod["description"]
        dbprod.price = prod["price"]
        dbprod.filename = prod["filename"]
        dbprod.save()
Ejemplo n.º 5
0
def main():

    with open('products.json') as json_file:
        data = json.load(json_file)

    products = data['products']
    cats = []
    for prod in products:
        if prod['category'] not in cats:
            cats.append(prod['category'])

    for c in cats:
        dbcat = Category()
        dbcat.title = c
        dbcat.save()
    for prod in products:
        dbprod = Product()
        dbprod.name = prod['name']
        dbprod.description = prod['description']
        dbprod.filename = prod['filename']
        dbprod.price = prod['price']
        dbprod.category = Category.objects.get(title=prod['category'])
        dbprod.save()
Ejemplo n.º 6
0
def main():
    with open('products.json') as json_file:
        data = json.load(json_file)

    products = data['products']
    cats = []

    # Set up categories
    for prod in products:
        #dbcat = Category()
        if prod['category'] not in cats:
            cats.append(prod['category'])

    for cat in cats:
        dbcat = Category()
        dbcat.title = cat
        dbcat.save()

    for prod in products:
        #dbcat = Category()

        dbprod = Product()
        dbprod.name = prod['name']
        dbprod.description = prod['description']
        dbprod.price = prod['price']
        dbprod.filename = prod['filename']
        dbprod.category = Category.object.get(title=prod['category'])

        # for cat in Category.object.all():
        #     if cat.title == prod['category']:
        #         dbprod.category = cat
        dbprod.save()

    for cat in Category.object.all():
        print(cat.id, cat.title)
    for prod in Product.object.all():
        print(prod.id, prod.name, prod.category)