def getStore(self, idstore): """method class for retrieve a store according to id """ if idstore > 100: store = Store(101, "unknown") return store elif idstore == "100": store = Store(101, "unknown") return store else: cur = self.cur cur.execute("SELECT * FROM `store`" " WHERE id = '" + str(idstore) + "'") row = cur.fetchall() store = Store(row[0][0], row[0][1]) return store
def get(self): try: """ Obtains a Product given the corresponding filters on the JSON json example: { "store_name":"Buy and sell books", } :returns example: { "success": true, "stores":[ { "store_id":1, "store_name":"Buy and sell books", "store_address": "1st Avenue", }, ... ] } """ store = Store() json = request.json ans = [] if json is not None: conditions = conditions_builder(json) ans = store.get_store(conditions) return {'success': True, 'stores': ans} except Exception as e: print(e) return {'success': False, 'Error': str(e)}
def test_print_items_in_cart(self): list = self.generate_data() store = Store(list) for each_item in list: store.add_cart(each_item, 1) print_items_in_cart(store) self.assertEqual(list, store.get_cart_items())
def getStoreId(self, storename): """method class for retrieve a store according to name """ try: cur = self.cur cur.execute("SELECT * FROM `store`" " WHERE nom = '" + str(storename) + "'") row = cur.fetchall() if len(row) == 0: store = Store(101, "unknown") return store else: store = Store(row[0][0], row[0][1]) return store except mysql.connector.errors.ProgrammingError as err: print(err.msg) store = Store(101, "unknown") return store
def main(args=None): """The main routine.""" if args is None: args = sys.argv[1:] list = parse_items_from_file(args[0]) store = Store(list) store_menu(store)
def get(self): stores = [] for store_bson in self.stores_collection.find(): #TODO refactor out into mapper store = Store(str(store_bson['_id']), store_bson['store_type'], store_bson['location_quick_name'], Location(store_bson['location']['type'], store_bson['location']['coordinates'])) stores.append(store) return stores
def test_insert_store(): store = Store() store_to_db = { 'store_name': 'book seller No. 1', 'store_street': '5th Avenue', 'store_mail': '*****@*****.**', 'store_phone_number': '5522887799', 'store_city': 'City 1', 'store_state': 'State 1', 'store_zip_code': '99999', } ans = store.insert_store(store_to_db) assert ans['success']
def test_remove_item_from_cart(self): list = self.generate_data() expected_remove_list = [] for each_item in list: expected_remove_list.append(each_item) toBeRemoved = expected_remove_list[0] del expected_remove_list[0] store = Store(list) for each_item in list: store.add_cart(each_item, 1) store.remove_from_cart(toBeRemoved) self.assertEqual(expected_remove_list, store.get_cart_items())
def create_store(): # If POST then the user has visited the form already and submitted a new store # After saving the new store, the user is forwarded back to '/stores/' if request.method == 'POST': name = request.form['name'] url_prefix = request.form['url_prefix'] tag_name = request.form['tag_name'] query = json.loads(request.form['query'].replace("'", "\"")) Store(name, url_prefix, tag_name, query).save_to_mongo() return redirect(url_for('.index')) # If no POST then user is givne the new store form to complete and submit return render_template('/stores/new_store.html')
def convert_to_store(stores: List) -> List[Store]: """ Convert list of dictionnaries to list of objects. :param stores: list containing dictionnaries :return: List[Store] """ store_list: List[Store] = [] for store in stores: if (store.get('name') and store.get('url') and store.get('products') > 100): store_list.append(Store(store['name'], store['url'])) return store_list
def post(self): try: """ inserts a store on the DB given by the JSON, for more information of the model refer to this project on model/store :returns example: { "success": True, "product": store_of_product } """ store = Store() json = request.json store.insert_store(json) return {'success': True, 'store': json} except Exception as e: return {'success': False, 'Error': str(e)}
def enrollment_store(store_name, description, score, average_price, picture_url): try: add_store = Store(name=store_name, description=description, average_price=average_price, picture=picture_url, average_score=score) session.add(add_store) session.commit() return {"message": "success for create store information"} except SQLAlchemyError: session.rollback() return abort(500, "database_error")
def put(self): try: """ updates a store on the DB with the given JSON values and filtered by store_id, for more information of the model refer to this project on model/store :returns example: { "success": True, "product": store_of_product } """ store = Store() json = request.json print(request.json) conditions = conditions_builder({'store_id':json['store_id']}) json.pop('store_id', None) store.update_store(json, conditions=conditions) return {'success': True, 'store': json} except Exception as e: return {'success': False, 'Error': str(e)}
def post(self): data = request.get_json() store_bson = self.stores_collection.find_one({ 'store_type': data['store_type'], 'location.coordinates': data['location']['coordinates'] }) if store_bson: return {'message': 'item exists'}, 400 new_store = { '_id': bson.ObjectId(), 'store_type': data['store_type'], 'location': data['location'], 'location_quick_name': data['location_quick_name'] } self.stores_collection.insert_one(new_store) store = Store(str(new_store['_id']), new_store['store_type'], new_store['location_quick_name'], Location(new_store['location']['type'], new_store['location']['coordinates'])) return store
def test_update_store(): store = Store() updated_values = {'store_name': 'I completely love books'} conditions = ['store_id = 1'] ans = store.update_store(updated_values, conditions=conditions) assert ans['success']
def test_print_items_in_store(self): list = self.generate_data() store = Store(list) print_items_in_store(store) store_list = store.items self.assertEqual(list, store_list)
def search(user_id): result = {} q_arg = request.args.get("q") sort_arg = request.args.get("sort") p_arg = request.args.get("p") category_arg = request.args.get("category") location_arg = request.args.get("location") zone_arg = request.args.get("zone") area_arg = request.args.get("area") filter_arg = request.args.get("filter") category = Table("category") merchant = Table("merchant") store = Table("store") q = "" haveLocation = False if location_arg: locations = location_arg.split(",") try: locations[0] = Decimal(locations[0]) locations[1] = Decimal(locations[1]) q = ("SELECT *, (6371*acos(cos(radians(" + str(locations[0]) + "))*cos(radians(lat))*cos(radians(`long`)-radians(" + str(locations[1]) + "))+sin(radians(" + str(locations[0]) + "))*sin(radians(lat)))) AS distance") haveLocation = True except (ValueError, DecimalException): abort(400) else: q = "SELECT *" q += " FROM store" if sort_arg: if str(sort_arg) == "popular": c = "SELECT store_id, COUNT(*) as count FROM transaction GROUP BY store_id" q += " LEFT JOIN (" + c + ") AS count ON count.store_id = id" elif str(sort_arg) == "match": pass wheres = [] if category_arg: q2 = Query.from_(category).select("*").where( category.id == category_arg) cursor = get_db().cursor() cursor.execute(str(q2)) record = cursor.fetchone() cursor.close() if record == None: abort(400) else: result["category"] = Category(record) q2 = (Query.from_(merchant).select( merchant.id).where(merchant.category_id == category_arg)) cursor = get_db().cursor() cursor.execute(str(q2)) merchants = cursor.fetchall() cursor.close() wheres.append("store.merchant_id IN (" + ",".join(str(i[0]) for i in merchants) + ")") if q_arg: wheres.append("store.name LIKE '%" + q_arg + "%'") if zone_arg: wheres.append("store.zone = " + zone_arg) if area_arg: wheres.append("store.area_level_2 = '" + area_arg + "'") q += " WHERE " + wheres[0] for i in wheres[1:]: q += " AND " + i if filter_arg: filters = filter_arg.split(";") for i in filters: splits = i.split(",") if splits[0] == "distance": if haveLocation: q += (" HAVING distance BETWEEN " + str(splits[1]) + " AND " + str(splits[2])) if sort_arg: if str(sort_arg) == "popular": q += " ORDER BY count.count DESC" elif str(sort_arg) == "distance": if haveLocation: q += " ORDER BY distance ASC" if p_arg: try: p_arg = int(p_arg) q += " LIMIT " + str((p_arg - 1) * 10) + ", 10" except ValueError: return abort(400) cursor = get_db().cursor() cursor.execute(str(q)) records = cursor.fetchall() cursor.close() newStore = lambda a: Store(a, 0) if not category_arg: newStore = lambda a: Store(a, 0) q = "INSERT INTO keyword(content, category, area, user_id, time, records" if zone_arg: q += ", zone" q += (") VALUES('" + q_arg + "', '" + category_arg + "', '" + area_arg + "','" + str(user_id[0]) + "'," + str(int(time.time() * 1000)) + "," + str(len(records))) if zone_arg: q += "," + zone_arg q += ")" cursor = get_db().cursor() cursor.execute(q) cursor.close() get_db().commit() records = list(map(newStore, records)) result["stores"] = records return jsonify(**result)
def test_checkout(self): list = self.generate_data() store = Store(list) checkout(store) self.assertTrue(True)
def test_get_store(): store = Store() store_info = store.get_store(conditions=['store_id = 1']) assert 'store_id' in store_info[0] and 'store_name' in store_info[0] and 'store_street' in store_info[0] and \ 'store_mail' in store_info[0] and 'store_phone_number' in store_info[0] and 'store_city' in store_info[0] and \ 'store_state' in store_info[0] and 'store_zip_code' in store_info[0]