def put(self): args = user_update_parser.parse_args() password = args.get('password') password2 = args.get('password2') user_id = args.get('user_id') apply_stat = args.get('apply_stat') if password and password2: if password2 == password: hash_password = User.set_password(password) args['password'] = hash_password args['password2'] = None else: data = {'stat': 400, 'msg': '密码不一致'} return data elif password or password2: data = {'stat': 400, 'msg': '缺少参数'} return data if apply_stat and int(apply_stat) == 1: try: user = UserModel.objects.get(id=user_id) except Exception as e: logger.error(e) return {'msg': '用户不存在'} mobile = user.mobile # salt = ''.join(random.sample(string.ascii_letters + string.digits, 8)) # args['password'] = User.set_password(salt) send_sms(mobile, str(mobile)[-6:]) args['user_id'] = None update_fields = {arg: args[arg] for arg in args if args.get(arg)} try: UserModel.objects(id=user_id).update(**update_fields) except Exception as exc: logger.warning(exc) return {'stat': 400, 'msg': '更新数据库失败'} data = {'msg': '更新成功'} return data
def post(self): user = utils._get_user() user_id = str(user.dbUser.id) args = user_create_parser.parse_args() mobile = args.get('mobile') email = args.get('email') password = args.get('password') org_id = args.get('org_id') channel_id = args.get('channel_id') cluster_id = args.get('cluster_id') is_admin = args.get('is_admin', False) if mobile: if not re.match('^1[3456789]\d{9}$', mobile): return {'stat': '400', 'msg': '手机号格式错误'} if email: if not re.match('^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$', email): return {'stat': '400', 'msg': '邮箱格式错误'} try: user = UserModel.objects.get(mobile=mobile) if user: return {'stat': "400", 'msg': "手机号已存在"} except Exception as exc: logger.error(exc) pass try: cluster = ClusterModel.objects.get(id=cluster_id) org = OrgModel.objects.get(cluster=cluster, org_type='peer',alias=org_id) except Exception as e: logger.error(e) return {'msg': '组织不存在', 'stat': 400} try: ChannelModel.objects.get(cluster=cluster, alias=channel_id) except Exception as e: logger.error(e) return {'msg': '通道不存在', 'stat': 400} args['password'] = User.set_password(password) # args['orgs'] = [org_id] args.pop('org_id') args.pop('channel_id') args.pop('is_admin') args.pop('cluster_id') if int(is_admin) == 1: role = 2 else: role = 3 try: new_user = UserModel(**args, orgs=[org.id], apply_stat=1, active=True, role=role) new_user.save() except Exception as exc: logger.error("exc %s", exc) return {'stat': "-1", 'msg': "存储数据库失败"} body = { "BlockchainSign": str(org.cluster.id), "ChannelId": channel_id, "OrgId": str(org_id), "UserId": str(new_user.id) } logger.info('add user info:{}'.format(body)) if not send_new_user_info(str(user_id), body=body): new_user.delete() return {'stat': 400, 'msg': '添加用户失败'} org.update(add_to_set__users=[new_user]) data = { 'stat': 200, 'msg': '成功' } return data