def post(self): args = self.reqparse() user = UserModel.by(email=args.email) if check_password_hash(user.password, args.password): jwt_payload = { 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=30, seconds=5), 'iat': datetime.datetime.utcnow(), 'sub': user.id } try: token = jwt.encode(jwt_payload, app.config.get('SECRET_KEY'), algorithm='HS256') user.token = token.decode() err = user.update() if err != None: raise err resp = rp(success=True, payload=user.serialize()), 201 except Exception as e: resp = rp(message=str(e)), 500 else: resp = rp(message='The passsword is incorrect'), 404 return resp
def delete(self, id:int): row = WarehouseModel.by(id=id) err = row.delete() if err != None: res = rp(message=str(err)), 500 else: res = rp(success=True, payload=row.serialize()), 204 return res
def post(self): args = self.reqparse() row = UserModel(args.name, args.email, generate_password_hash(args.password)) err = row.save() if err != None: return rp(message=str(err)), 500 return rp(success=True, payload=row.serialize()), 201
def post(self): args = self.reqparse() row = ProductModel(args.name, args.sku) err = row.save() if err != None: res = rp(message=str(err)), 500 else: res = rp(success=True, payload=row.serialize()), 201 return res
def delete(self, id): if not is_int(id): return rp(message="The ID must be an integer"), 404 row = ProductModel.by(id=int(id)) err = row.delete() if err != None: res = rp(message=str(err)), 500 else: res = rp(success=True, payload=row.serialize()), 204 return res
def put(self, id:int): args = self.reqparse() row = WarehouseModel.by(id=id) row.name= args.name row.address= args.address err = row.update() if err != None: res = rp(message=str(err)), 500 else: res = rp(success=True, payload=row.serialize()), 201 return res
def get(self, product_id:int=0, warehouse_id:int=0): if product_id>0 and warehouse_id>0: rows = stock_product_warehouse(product_id, warehouse_id) else: rows = stock_all() if isinstance(rows, Exception): return rp(message=str(rows)), 404 return rp(success=True,payload=rows), 200
def get(self, warehouse_id:int=0): if warehouse_id>0: rows = stock_warehouse(warehouse_id) else: return rp(message="The warehouse ID must be an integer and greate zero"), 404 if isinstance(rows, Exception): return rp(message=str(rows)), 404 return rp(success=True,payload=rows), 200
def get(self, product_id:int=0): if product_id>0: rows = stock_product(product_id) else: return rp(message="The product ID must be an integer and greate zero"), 404 if isinstance(rows, Exception): return rp(message=str(rows)), 404 return rp(success=True,payload=rows), 200
def put(self, id): args = self.reqparse() if not is_int(id): return rp(message="The ID must be an integer"), 404 row = ProductModel.by(id=int(id)) row.name = args.name row.sku = args.sku err = row.update() if err != None: res = rp(message=str(err)), 500 else: res = rp(success=True, payload=row.serialize()), 201 return res
def get(self, id=None): if id is not None: if is_int(id): rows = ProductModel.by(id=int(id)).serialize() else: rows = ProductModel.by(sku=id).serialize() else: rows = [a.serialize() for a in ProductModel.all()] return rp(success=True, payload=rows)
def jwt_required(): authorization = request.headers.get('Authorization', '')[7:] path = request.path exclude = [ '/v1/auth/login', '/v1/auth/register', '/', '', ] if path not in exclude: try: payload = jwt.decode(authorization, app.config.get('SECRET_KEY')) UserModel.by(id=payload['sub']) except jwt.ExpiredSignatureError: return rp(message='Signature expired. Please log in again.') except jwt.InvalidTokenError: return rp(message='Invalid token. Please log in again.') except Exception as e: return rp(message=str(e)) return None
def stock_move_transfer(ttype): args = stock_move_reqparse() if args.qty <= 0: return rp(message="Quantity must be an integer and greater to zero"), 404 if args.product_id <= 0: return rp(message="Product ID must be an integer. Check /VERSION/product"), 404 if args.warehouse_id <= 0: return rp(message="Warehouse ID must be an integer. Check /VERSION/warehouse"), 404 product_id = ProductModel.by(id=args.product_id) warehouse_id = WarehouseModel.by(id=args.warehouse_id) if ttype == 'sale': stock = stock_product_warehouse( product_id=product_id.id, warehouse_id=warehouse_id.id) if isinstance(stock, Exception): return rp(message=str(stock)), 400 product_stock = stock[0].get('qty') if int(args.qty) > product_stock: return rp( message="The product %s only has %s stock" % (product_id.id, product_stock), ), 400 type_move = 'out' else: # defautl is purchase type_move = 'in' name = next_move(ttype) stock_move = StockMoveModel(name, type_move, args.qty, product_id=product_id.id, warehouse_id=warehouse_id.id) err =stock_move.save() if err != None: return rp(message=str(err)), 500 return rp(success=True, payload=stock_move.serialize()), 201
def get(self, id:int=0): if id > 0: rows = WarehouseModel.by(id=id).serialize() else: rows = [ a.serialize() for a in WarehouseModel.all()] return rp(success=True, payload=rows)