def put(self, store_id, item_id): # Validate current store_id current_store = StoreModel.find_by_id(store_id) if not current_store: return Store.response_not_found(store_id) request_data = self.put_parser.parse_args() new_name = request_data.get('new_name') new_price = request_data.get('new_price') new_store_id = request_data.get('new_store_id') if not (new_name or new_store_id or new_price): return make_response_message('Nothing to update', 400) # Validate new store_id if new_store_id: new_store = StoreModel.find_by_id(new_store_id) if not new_store: return Store.response_not_found(new_store_id) # Validate item item = current_store.find_item(item_id) if not item: return self.__class__.response_not_found(item_id) item.name = new_name if new_name else item.name item.price = new_price if new_price else item.price item.store_id = new_store_id if new_store_id else item.store_id item.save_to_db() return make_response_message('Item updated', 200)
def get(cls): uid = get_jwt_identity() orders = [] if StoreModel.find_by_id(uid): for order in OrderModel.find_all(): if order.status == 'pending': items = fetching_order(order.order_items) message = order.message orders.append({ 'id': order.id, 'status': order.status, 'items': items, 'message': message }) return orders, 200 elif CustomerModel.find_by_id(uid): for order in OrderModel.find_customer_completed_orders( customer_id=uid): items = fetching_order(order.order_items) message = order.message orders.append({ 'id': order.id, 'items': items, 'message': message }) return orders, 200 return 401
def get(self, store_id): # Validate store_id store = StoreModel.find_by_id(store_id) if not store: return Store.response_not_found(store_id) return [item.to_json() for item in store.items]
def get(cls, id: int): """Return a single store""" store = StoreModel.find_by_id(id) if store: return store.json(), 200 return {"message": STORE_NOT_FOUND}, 404
def post(self, name): data = Item.parser.parse_args() # Check to see if the store exists. store = StoreModel.find_by_id(data['store_id']) if store is None: return { 'message': "Store with ID '{}' was not found.".format(data['store_id']) }, 400 # Check to see if an item with the same name already exists. item = ItemModel.find_by_name(name, data['store_id']) if item: return { 'message': "An item with the name '{}' already exists for store with ID '{}'" .format(name, data['store_id']) }, 400 item = ItemModel(name, data['price'], data['store_id']) try: item.save_to_db() except: return {'message': "An error occurred inserting the item."}, 500 return item.json(), 201
def validate_admin(*args, **kwargs): jwt_data = _decode_jwt_from_request(request_type='access') uid = jwt_data[0]['identity'] admin_email = StoreModel.find_by_id(uid).email if admin_email in ADMINS: return func(*args, **kwargs) abort(401)
def put(self, id): data = Store.parser.parse_args() store = StoreModel.find_by_id(id) if store: store.name = data['name'] updated_store = store.save_to_db() return updated_store, 200 return StoreModel(**data).save_to_db(), 201
def delete(cls, id: int): """Delete an existing store""" store = StoreModel.find_by_id(id) if store: store.delete_from_db() return {"message": STORE_DELETED}, 200 return {"message": STORE_NOT_FOUND}, 404
def get(cls): uid = get_jwt_identity() if CustomerModel.find_by_id(uid): return {"type": "customer"}, 200 store = StoreModel.find_by_id(uid) if store: return {"type": "messenger"}, 200 return {"message": CUSTOMER_NOT_FOUND}, 404
def get(cls): customer_id = get_jwt_identity() order = OrderModel.find_customer_pending_order(customer_id) store_order = StoreOrderModel.find_by_order_id(order.id) store = StoreModel.find_by_id(store_order.store_id) store_data = {"email": store.email, "phone1": store.phone1} if store.phone2: store_data['phone2'] = store.phone2 return store_data, 200
def post(self): data = Store.parser.parse_args() if StoreModel.find_by_id(data['name']): return { 'message': "A store with name '{}' already exists".format(data['name']) }, 400 store = StoreModel(data['name']) try: store.save_to_db() except: return { 'message': 'An error occurred while creating the store' }, 500 store = StoreModel.find_by_id(store.id) return store.json(), 201
def validate_input(**kwargs): store = StoreModel.find_by_id(kwargs['store_id']) if store is None: raise ValueError('Store not found.') if 'item_id' not in kwargs: return store item = ItemModel.find_by_id(kwargs['item_id']) if item is None: raise ValueError('Item not found.') return item
def put(self, store_id): new_name = self.put_parser.parse_args()['new_name'] store = StoreModel.find_by_id(store_id) if not store: return self.__class__.response_not_found(store_id) store.name = new_name store.save_to_db() return make_response_message('Store updated', 200)
def put(self, id): data = Store.parser.parse_args() store = StoreModel.find_by_id(id) if store: store.name = data['name'] store.save_to_db() return store.json() store = StoreModel(data['name']) store.save_to_db() return store.json()
def delete(self, _id): store: StoreModel = StoreModel.find_by_id(_id) if not store: return {'message': f'Store with id {_id} is not present'}, 404 try: store.delete_from_db() return {}, 204 except Exception as ex: self.logger.error(traceback.format_exc()) return {'message': f'An error occurred while deleting store with id {_id}'}, 500
def get(self, _id): store = StoreModel.find_by_id(_id) if store: return store.json() else: return { 'status': 'error', 'message': f'Store {_id} not found' }, 404
def get(self, store_id, item_id): # Validate current store_id current_store = StoreModel.find_by_id(store_id) if not current_store: return Store.response_not_found(store_id) item = current_store.find_item(item_id) if not item: return self.__class__.response_not_found(item_id) return item.to_json(), 200
def delete(self, store_id, item_id): # Validate current store_id if not StoreModel.find_by_id(store_id): return Store.response_not_found(store_id) item = ItemModel.find_by_id(item_id) if not item: return self.__class__.response_not_found(item_id) item.delete() return make_response_message("Item '{}' deleted".format(item_id), 200)
def post(self, store_id): # Validate store_id store = StoreModel.find_by_id(store_id) if not store: return Store.response_not_found(store_id) request_data = self.post_parser.parse_args() item = ItemModel(request_data['name'], request_data['price'], store_id) item.save_to_db() return item.to_json(), 201
def delete(self, name): item = ItemModel.find_by_name(name) if item: user_id = get_jwt_identity() if user_id != StoreModel.find_by_id(item.store_id).user_id: return {"message": "You can only delete item in your own store"} item.delete_from_db() return {"message": "item deleted"} return {"message": "item not found"}
def delete(cls, _id: str): store = StoreModel.find_by_id(_id) if not store: return {"message" : STORE_NOT_FOUND}, 404 try: store.delete_from_db() except: return {"message": ERROR_DELETING_STORE}, 500 return {"message": SUCCESS_DELETING_STORE}, 200
def delete(self, store_id): store = StoreModel.find_by_id(store_id) if not store: return self.__class__.response_not_found(store_id) if len(store.items.all()) > 0: return make_response_message( 'Cannot delete store that contains items', 400) store.delete() return make_response_message("Store '{}' deleted".format(store_id), 200)
def delete(self, _id): store = StoreModel.find_by_id(_id) if store: store.delete() return {'status': 'ok', 'message': f'Store {_id} deleted'} else: return { 'status': 'error', 'message': f'Store {_id} not found' }, 404
def put(self, name): if not StoreModel.find_by_id(1): return {"message": "You have to create store first"}, 400 item = ItemModel.find_by_name(name) data = Item.parser.parse_args() if item is None: item = ItemModel(name, data['price'], data["store_id"]) else: item.price = data["price"] item.store = data["store_id"] item.save_to_db() return item.json()
def post(cls, _id: str): if StoreModel.find_by_id(_id): return {"message": ERROR_ID_EXISTS}, 400 data = request.get_json() if StoreModel.find_by_email(data["email"]): return {"message": "Store email already exists"}, 400 store = store_schema.load(data) try: store.save_to_db() except: return {"message": "Error saving store"}, 500 return store_schema.dump(store)
def post(self, name): data = Item.parser.parse_args() if StoreModel.find_by_id(data['store_id']) and ItemModel.find_by_name( name ): #Dedupe at Item Level is okay, but dedupe at Store and Item Level return { 'message': "An item with name '{}' already exists for the store".format( name) }, 400 item = ItemModel(name, **data) try: item.save_to_db() except: return {"message": "An internal server error occurred"}, 500 return item.json(), 201
def post(self, name): if not StoreModel.find_by_id(1): return {"message": "You have to create store first"}, 400 if ItemModel.find_by_name(name): return {"message": "Item already exists!!"}, 400 else: data = Item.parser.parse_args() try: StoreModel("test").save_to_db() ItemModel(name, data['price'], data['store_id']).save_to_db() except: return {"message": "An error occured"}, 500 return {"message": "Item has been created"}, 201
def put(self, name): data = Item.parser.parse_args() # Check if there is a store with this id if StoreModel.find_by_id(data['store_id']): # Create a new item if it's not found item = ItemModel.find_by_name(name) if item: item.price = data['price'] item.store_id = data['store_id'] else: item = ItemModel(name, **data) item.save_in_database() else: return { 'message': "No store with id '{}' found!".format(data['store_id']) }, 400 return item.json(), 201
def post(self, name): #Check if the item is already in the database if ItemModel.find_by_name(name) is not None: return {'message':"Item '{}' already exists!".format(name)}, 400 #Request will take the data from the body of an HTTP request and convert it into a dictionary # data will have the fields needed to create an item; data['price'] for example #depricated by reqparse: data = request.get_json() data = Item.parser.parse_args() #Create a JSON of the item #Depricated: item = {'name':name, 'price':data['price']} item = ItemModel(name, data['price'], data['store_id']) if StoreModel.find_by_id(data['store_id']): item.save_to_db() return item.json(), 201 else: return {'message':'Store does not exists!'}
def put(self, name): data = Item.parser.parse_args() if not StoreModel.find_by_id(data['store_id']): return { 'message': f'Store with store_id: {int(data["store_id"])} not found, please create a store first' }, 400 item = ItemModel.find_by_name(name) if item is None: item = ItemModel(name, **data) else: item.price = data['price'] item.save_to_db() return item.json()