def update(**request): required = ["client_id"] check_params(request, required) client_id = request.get('client_id') info = ClientInfo.query.filter_by(id=client_id).first() client_name = request.get("client_name") if info is None: raise NotFound('客户信息不存在。') if client_name: exist = ClientInfo.query.filter_by(name=client_name, store=info.store).first() if exist: raise ResourceConflic("该姓名已经存在。") else: info.name = client_name wechat = request.get('wechat') description = request.get('description') phone = request.get('phone') if wechat: info.wechat = wechat if description: info.description = description if phone: info.phone = phone db.session.commit() return success()
def update_passsword(store_id, user_id, **request): required = ['password'] check_params(request, required) user = User.query.filter_by(id=user_id, store_id=store_id).first() if user: user.password = generate_password_hash(request['password']) db.session.add(user) db.session.commit() return success()
def add_not_commit(**request): required = ["boss_id", "name"] check_params(request, required) if not Boss.filter_by(boss_id=request['boss_id']).first(): raise NotFound("Boss not found!") store = Store() store.owner = request['boss_id'] store.name = request['store_name'] store.description = request['description'] return store
def reset_password(store_id, **request): required = ["user_id"] check_params(request, required) user = User.query.filter_by(id=request['user_id'], store_id=store_id).first() if user: new_pass = gen_random_password() user.password = generate_password_hash(new_pass) db.session.commit() return success(data=new_pass)
def delete(**request): required = ["client_id"] check_params(request, required) client_id = request["client_id"] if IncomeGeneral.query.filter_by(client=client_id).count(): raise NotAllowed("该客户存在账单,不能删除。") info = ClientInfo.query.filter_by(id=client_id).first() db.session.delete(info) db.session.commit() return success()
def delete(store_id, **request): required = ["user_id"] check_params(request, required) user = User.query.filter_by(id=request['user_id'], store_id=store_id).first() if not user: raise NotFound() if user.role.name == 'Administrator': raise NotAllowed("不能删除 admin 用户。") db.session.delete(user) db.session.commit() return success()
def update(store_id, **request): required = ["user_id"] check_params(request, required) user = User.query.filter_by(id=request['user_id'], store_id=store_id).first() if user: user.name = request.get('name', user.name) user.phone = request.get('phone', user.phone) user.role_id = request.get('role_id', user.role_id) if user.role.name == 'Administrator': raise NotAllowed("不能更改角色为管理员。") db.session.commit() return success()
def add(store_id, **request): required = ["role_id", "name", "password", "phone"] check_params(request, required) user = User() user.store_id = store_id user.role_id = request['role_id'] user.name = request['name'] user.password = generate_password_hash(request['password']) user.phone = request['phone'] user.wechat = request.get('wechat', '') db.session.add(user) db.session.commit() return success(data=user.get_dict(ignore={'password'}))
def add(**request): required = ["store_name", "phone"] check_params(request, required) boss = Boss() gb_id = uuid.uuid4() while Boss.query.filter_by(boss_id=gb_id).first(): gb_id = uuid.uuid4() boss.boss_id = gb_id boss.level = 0 boss.expire_time = '2099-12-29 00:00:00' request['boss_id'] = boss.boss_id store = Store.add_not_commit(**request) db.session.add(boss) db.session.add(store) db.session.commit()
def add(**request): required = ["client_name", "store_id"] check_params(request, required) stroe_id = request['store_id'] name = request['client_name'] exist = ClientInfo.query.filter_by(name=name, store=stroe_id).first() if exist: raise ResourceConflic("该姓名已经存在。") info = ClientInfo() info.store_id = stroe_id info.name = name info.description = request.get('description', '') info.phone = request.get('phone', '') info.wechat = request.get('wechat', '') db.session.add(info) db.session.commit() return success()
def get(**request): required = ["store_id"] check_params(request, required) page = int(request.get('page', 1)) page_size = int(request.get('page_size', 20)) name = request.get('client_name') store_id = request['store_id'] if name: query = (ClientInfo.query.filter(ClientInfo.name.like(q(name)), ClientInfo.store_id == store_id)) else: query = (ClientInfo.query.filter(ClientInfo.store_id == store_id)) query = query.paginate(page, page_size) current_app.logger.info(query) info = query.items data = [{ 'client_name': item.name, 'client_id': item.id, 'wechat': item.wechat, 'phone': item.phone, 'description': item.description } for item in info] return result_fmt(data, query)
def income_details(): if request.method == 'POST': required = ["client_id", "goods", "quantity", "unit_price", "unit"] check_params(request.json, required) return IncomeDetails().add(**request.json) elif request.method == 'GET': # IncomeDetails.get_unpaid_bills() return IncomeDetails.get(**request.args) elif request.method == 'PUT': required = ["unit_price", "quantity", "goods", "unit"] check_params(request.json, required) return IncomeDetails.update(**request.json) elif request.method == 'DELETE': required = ["detail_id"] check_params(request.args, required) return IncomeDetails.delete(**request.args) elif request.method == 'PATCH': required = ["detail_id"] check_params(request.json, required) current_app.logger.info(request.json) return IncomeDetails.update_payed(**request.json)
def income_general(): if request.method == 'POST': required = ['client_id', 'total'] current_app.logger.info(request.json) check_params(request.json, required) return IncomeGeneral.add(**request.json) elif request.method == 'GET': # required = ["client_id"] # check_params(request.args, required) return IncomeGeneral.get(**request.args) elif request.method == 'PUT': required = ["general_id"] check_params(request.json, required) return IncomeGeneral.update(**request.json) elif request.method == 'DELETE': required = ["general_id"] check_params(request.args, required) return IncomeGeneral.delete(**request.args)