def add_normal_item_to_cart(user_id, item_id): result = db.get(user_id) item_id = int(item_id) if item_id not in mapping: return jsonify({"error": "invalid item"}) for i in range(len(result['shopping']['items'])): if result['shopping']['items'][i]['id'] == item_id: result['shopping']['items'][i]['status'] = 'in_cart' result['shopping']['subtotal'] = calculate_shopping_price( result['shopping']['items']) result['shopping']['total'] = calculate_shopping_price( result['shopping']['items']) db.update(user_id, result) return jsonify(result['shopping']) item = mapping[item_id] item['status'] = 'in_cart' item['qty'] = 1 result['shopping']['items'].append(item) result['shopping']['subtotal'] = calculate_shopping_price( result['shopping']['items']) result['shopping']['total'] = calculate_shopping_price( result['shopping']['items']) db.update(user_id, result) return jsonify(result['shopping'])
def remove_item_from_list(user_id, list_id, item_id): data = db.get(user_id) result = data['lists'] if list_id in result: if int(item_id) in mapping: # item = mapping[int(item_id)] pass else: return jsonify({"error": "item not found"}) added = False for i in range(len(data['lists'][list_id]['items'])): if data['lists'][list_id]['items'][i]['id'] == int(item_id): if data['lists'][list_id]['items'][i]['qty'] == 1: del data['lists'][list_id]['items'][i] else: data['lists'][list_id]['items'][i]['qty'] -= 1 added = True break if not added: return jsonify({"error": "item not in list"}) data['lists'][list_id]['subtotal'] = calculate_price( data['lists'][list_id]['items']) data['lists'][list_id]['total'] = calculate_price( data['lists'][list_id]['items']) db.update(user_id, data) result = {"status": "item removed"} else: return jsonify({"error": "list not found"}) return jsonify(result)
def add_item_to_list(user_id, list_id, item_id): data = db.get(user_id) result = data['lists'] if list_id in result: if int(item_id) in mapping: item = mapping[int(item_id)] else: return jsonify({"error": "item not found"}) added = False for i in range(len(data['lists'][list_id]['items'])): if data['lists'][list_id]['items'][i]['id'] == int(item_id): data['lists'][list_id]['items'][i]['qty'] += 1 added = True break if not added: item['qty'] = 1 data['lists'][list_id]['items'].append(item) data['lists'][list_id]['subtotal'] = calculate_price( data['lists'][list_id]['items']) data['lists'][list_id]['total'] = calculate_price( data['lists'][list_id]['items']) db.update(user_id, data) result = {"status": "item added"} else: return jsonify({"error": "list not found"}) return jsonify(result)
def add_list_to_shopping(user_id, list_id): result = db.get(user_id) if list_id not in result['lists']: return jsonify({"error": "list not found"}) list_to_add = result['lists'][list_id]['items'] for i in range(len(list_to_add)): list_to_add[i]['status'] = 'searching' result['shopping']['items'] += list_to_add db.update(user_id, result) return jsonify(result['shopping'])
def get_user_list(user_id, list_id): data = db.get(user_id) result = data['lists'] if list_id in result: result = result[list_id] else: data['lists'][list_id] = { "items": [], "subtotal": 0, "discount": 0, "total": 0 } db.update(user_id, data) result = {"items": []} return jsonify(result)
def reset_user(user_id): db.get(user_id) data = db.empty_json_data() db.update(user_id, data) return jsonify(data)
def alarm_off(): user_id = 'alarm' json_data = db.get_alarm() json_data['switch'] = 0 db.update(user_id, json_data) return jsonify(json_data)
def submitform(): user_id = 'form' data = request.get_json() db.get(user_id) db.update(user_id, data) return jsonify(data)