def decorator(*args, **kwargs): current_user = Db.get_user(email=get_jwt_identity()) r = current_user.role if r == 2 or r == 'Attendant': msg = "Only administrators can access these resource" abort(406, msg) return f(*args, **kwargs)
def decorator(*args, **kwargs): current_user = Db.get_user(email=get_jwt_identity()) r = current_user.role if r != 'SuperAdmin': msg = "Only Super Admin can access these resource" abort(406, msg) return f(*args, **kwargs)
def post(self, id): """ Sell product """ json_data = request.get_json(force=True) sales_validator(json_data) email = get_jwt_identity() user = Db.get_user(email=email) store_id = user.store_id number = json_data['number'] product = Db.get_p_by_id(id) if product: price = product.price amount = number * price if product.inventory < number: d = product.inventory msg = 'There are only {} {} available'.format(d, product.name) return abort(400, msg) new_sale = Sale(store_id, product.name, number, amount) Db.sales.append(new_sale) res1 = new_sale.json_dump() new_inv = product.inventory - number product.inventory = new_inv return {"status": "Success!", "data": res1}, 201 msg = 'Product does not exist' return {"message": msg}, 404
def get(self): """ Get all products """ products = Db.products email = get_jwt_identity() user = Db.get_user(email=email) store_id = user.store_id res = [p.json_dump() for p in products if p.store_id == store_id] if len(products) < 1: res = {"message": 'There are no products at this time'}, 404 return res
def post(self): """ Add Admin """ json_data = request.get_json(force=True) login_validator(json_data) email = get_jwt_identity() newad = Db.get_user(json_data['email']) if newad and newad.role <= 1: msg = "User is Admin already" abort(406, msg) user = Db.get_user(email=email) store_id = user.store_id role = 1 user_reg = User(store_id, role, json_data['email'], json_data['password']) for i, item in enumerate(Db.users): if item == newad: Db.users[i] = user_reg Db.users.append(user_reg) return {"status": "Success!", "data": user_reg.json_dump()}, 201
def get(self): """ Get all sales """ sales = Db.sales if len(sales) < 1: res = {"message": 'There are no sale records'}, 404 return res email = get_jwt_identity() user = Db.get_user(email=email) store_id = user.store_id s_list = [s.json_dump() for s in sales if s.store_id == store_id] return {"status": "Success!", "data": s_list}, 200
def delete(self, id): """ Delete a product """ p = Db.get_p_by_id(id) email = get_jwt_identity() user = Db.get_user(email=email) store_id = user.store_id if p.store_id != store_id: msg = 'Product does not exist' abort(404, msg) Db.products.remove(p) return {"status": "Deleted!", "data": p.json_dump()}, 200
def get(self, id): """ Get a specific product """ email = get_jwt_identity() user = Db.get_user(email=email) store_id = user.store_id p = Db.get_p_by_id(id) if p.store_id != store_id: msg = 'Product does not exist' abort(404, msg) return {"status": "Success", "data": p.json_dump()}, 200
def post(self): """ Add Attendant """ json_data = request.get_json(force=True) login_validator(json_data) newatt = Db.get_user(json_data['email']) if newatt and newatt.role == 2: msg = "User is Attendant already" abort(406, msg) email = get_jwt_identity() user = Db.get_user(email=email) store_id = user.store_id role = 2 user_reg = User(store_id, role, json_data['email'], json_data['password']) newatt = Db.get_user(json_data['email']) for k, j in enumerate(Db.users): if j == newatt: Db.users[k] = user_reg Db.users.append(user_reg) return {"status": "Success!", "data": user_reg.json_dump()}, 201
def post(self): """ Login """ json_data = request.get_json(force=True) login_validator(json_data) u = Db.get_user(json_data['email']) epass = json_data['password'] if u is None or not check_password_hash(u.password, epass): msg = 'Invalid credentials' abort(400, msg) access_token = create_access_token(identity=json_data['email']) return {"status": "Success!", "token": access_token}, 200
def delete(self, id): """ Delete a sale """ sale = Db.get_s_by_id(id) email = get_jwt_identity() user = Db.get_user(email=email) store_id = user.store_id if sale.store_id != store_id: msg = 'That record does not exist' return abort(404, msg) sk = sale.json_dump() Db.sales.remove(sale) return {"status": "Deleted!", "data": sk}, 200
def get(self, id): """ Get a specicific sale record """ email = get_jwt_identity() user = Db.get_user(email=email) store_id = user.store_id sale = Db.get_s_by_id(id) if sale.store_id != store_id: msg = 'That record does not exist' return abort(404, msg) sk = sale.json_dump() return {"status": "Success!", "data": sk}, 200
def put(self, id): """ Update a sale """ s = Db.get_s_by_id(id) email = get_jwt_identity() user = Db.get_user(email=email) store_id = user.store_id if s.store_id != store_id: msg = 'Sale does not exist' abort(404, msg) json_data = request.get_json(force=True) sales_validator(json_data) number = json_data['number'] s.number = number return {"status": "Success!", "data": s.json_dump()}, 200
def post(self): """ Add a product to the manager """ json_data = request.get_json(force=True) product_validator(json_data) p = Db.get_product(json_data['name']) if p: msg = 'Product already exists.Update product inventory instead' abort(406, msg) email = get_jwt_identity() user = Db.get_user(email=email) store_id = user.store_id new_product = Product(store_id, json_data['name'], json_data['inventory'], json_data['price']) Db.products.append(new_product) res = new_product.json_dump() return {"status": "Success!", "data": res}, 201
def put(self, id): """ Edit a product """ p = Db.get_p_by_id(id) email = get_jwt_identity() user = Db.get_user(email=email) store_id = user.store_id if p.store_id != store_id: msg = 'Product does not exist' abort(404, msg) json_data = request.get_json(force=True) product_update_validator(json_data) name = json_data['name'] inventory = json_data['inventory'] price = json_data['price'] if name: p.name = name if inventory: p.inventory = inventory if price: p.price = price return {"status": "Success!", "data": p.json_dump()}, 200