def get_user_customers(): res = ResMsg() user_name = session["user_name"] if not user_name: res.update(ResponseCode.InvalidParameter) return res.data user_obj = db.session.query(User).filter( User.username == user_name).first() customers_json = [] if user_obj and user_obj.customers: for customer_obj in user_obj.customers: incomes_json = [] if customer_obj.incomes: incomes_json = model_to_dict(customer_obj.incomes) customer_json = model_to_dict(customer_obj) customer_json["incomes"] = incomes_json customers_json.append(customer_json) user = model_to_dict(user_obj) user["customers"] = customers_json res.update(data=user) return res.data
def get_all_customers(): res = ResMsg() customers = db.session.query(Customer).filter().all() customers_json = [] for customer in customers: incomes_dic = model_to_dict(customer.incomes) customer_dic = model_to_dict(customer) customer_dic["incomes"] = incomes_dic customers_json.append(customer_dic) res.update(data=customers_json) return res.data
def add_customer(): """ 添加客户 :return: """ res = ResMsg() try: customer = request.get_json(force=True) user_name = session["user_name"] user = db.session.query(User).filter( User.username == user_name).first() if not customer or not user: res.update(code=ResponseCode.InvalidParameter) return res.data customer_count = db.session.query(Customer).filter( Customer.name == customer.get("name")).count() if customer_count != 0: res.update(code=ResponseCode.RepeatUserName) return res.data customer_obj = Customer() customer_obj.name = customer.get("name") customer_obj.email = customer.get("email") customer_obj.users.append(user) db.session.add(customer_obj) db.session.commit() res.update(data=model_to_dict(customer_obj)) except Exception as ex: logging.error(ex) res.update(code=ResponseCode.Fail) return res.data
def login(): """ 登陆成功获取到数据获取token和刷新token :return: """ res = ResMsg() obj = request.get_json(force=True) user_name = obj.get("username") user_password = obj.get("password") # 未获取到参数或参数不存在 if not obj or not user_name or not user_password: res.update(code=ResponseCode.InvalidParameter) return res.data user = db.session.query(User).filter(User.username == user_name).first() if user and user.password == user_password: # 生成数据获取token和刷新token access_token, refresh_token = Auth.encode_auth_token(user_id=user_name) data = { "access_token": access_token.decode("utf-8"), "refresh_token": refresh_token.decode("utf-8"), "user": model_to_dict(user) } res.update(data=data) return res.data else: res.update(code=ResponseCode.AccountOrPassWordErr) return res.data
def get_incomes(): res = ResMsg() client_id = request.args.get("id") if not client_id: res.update(ResponseCode.InvalidParameter) return res.data client_obj = Client.query.filter(Client.id == client_id).first() if not client_obj: res.update(code=ResponseCode.NoResourceFound) return res.data user_obj = User.query.filter(User.name == session["user_name"]).first() if user_obj not in client_obj.users: res.update(code=ResponseCode.AccessNotAuthorized) return res.data incomes_obj = Income.query.filter(Income.client_id == client_id).order_by( Income.date) income_json = model_to_dict(incomes_obj) res.update(data={ key: [pair[key] for pair in income_json] for key in ('date', 'value') }) return res.data
def add(): res = ResMsg() user_name = session["user_name"] user_obj = User.query.filter(User.name == user_name).first() obj = request.get_json(force=True) if not all([obj, obj.get("name"), obj.get("email")]): res.update(code=ResponseCode.InvalidParameter) return res.data # 将客户添加到指定用户管理列表中 client_obj = Client.query.filter(Client.name == obj.get("name")).first() if client_obj: client_obj.users.append(user_obj) db.session.commit() return res.data valid_email = EmailTool.check_email(obj.get("email")) if not valid_email: res.update(code=ResponseCode.InvalidEmail) return res.data # 添加新客户记录 client_obj = Client(**obj, users=[user_obj]) db.session.add(client_obj) db.session.commit() res.update(data=model_to_dict(client_obj)) return res.data
def get_clients(): res = ResMsg() name = session["user_name"] user_obj = User.query.filter(User.name == name).first() res.update(data=model_to_dict(user_obj.clients)) return res.data
def get_info(): res = ResMsg() name = session["user_name"] user_obj = User.query.filter(User.name == name).first() user_json = model_to_dict(user_obj) user_json['n_clients'] = len(user_obj.clients) res.update(data=user_json) return res.data
def get_user(): res = ResMsg() obj = request.get_json(force=True) user_name = session["user_name"] if not obj or not user_name: res.update(ResponseCode.InvalidEmail) return res.data user = db.session.query(User).filter(User.username == user_name).first() customers = model_to_dict(user) res.update(data=customers) return res.data
def get_customer_income(): res = ResMsg() obj = request.get_json(force=True) customer_id = obj.get("customer_id") if not obj or not customer_id: res.update(ResponseCode.InvalidParameter) return res.data incomes = db.session.query(Income).filter( Income.customer_id == customer_id).all() incomes_json = model_to_dict(incomes) res.update(data=incomes_json) return res.data
def login(): """登陆成功获取到数据获取token和刷新token""" res = ResMsg() obj = request.get_json(force=True) name = obj.get("name") if not all([obj, name, obj.get("password")]): res.update(code=ResponseCode.InvalidParameter) return res.data user_obj = User.query.filter(User.name == name).first() if user_obj and user_obj.password == obj.get("password"): access_token, refresh_token = Auth.encode_auth_token(user_id=name) data = { "access_token": access_token.decode("utf-8"), "refresh_token": refresh_token.decode("utf-8"), "user": model_to_dict(user_obj) } res.update(data=data) else: res.update(code=ResponseCode.AccountOrPassWordErr) return res.data
def background_thread(): count = 0 while True: TradeNamespace.get_socketio().sleep(5) count += 1 with db.app.app_context(): try: trades = db.session.query(Trade).filter( Trade.feedback == 0).all() if not trades: continue # 向前端发送需求 trades_json = model_to_dict(trades) TradeNamespace.get_socketio().emit('hi', trades_json, namespace="/flask") for trade in trades: trade.feedback = 1 db.session.add(trade) db.session.commit() except Exception as ex: logger.error(ex)
def register(): """ 注册 :return: """ res = ResMsg() obj = request.get_json(force=True) user_name = obj.get("username") user_email = obj.get("email") if not obj or not user_name or not user_email: res.update(code=ResponseCode.InvalidParameter) return res.data email_qualify = EmailTool.check_email(user_email) if not email_qualify: res.update(code=ResponseCode.InvalidEmail) return res.data same_user_count = db.session.query(User).filter( User.username == user_name).count() if same_user_count != 0: res.update(code=ResponseCode.RepeatUserName) return res.data register_user = User(username=user_name, password=obj.get("password"), phone=obj.get("phone"), email=obj.get("email"), address=obj.get("address")) db.session.add(register_user) db.session.commit() user_json = model_to_dict(register_user) res.update(data=user_json) return res.data