def patch(self): file_parser = RequestParser() file_parser.add_argument('photo', type=parser.image_file, required=False, location='files') files = file_parser.parse_args() user_id = g.user_id new_cache_values = {} new_user_values = {} return_values = {'id': user_id} if files.photo: try: photo_url = upload_image(files.photo.read()) except Exception as e: current_app.logger.error('upload failed {}'.format(e)) return { 'message': 'Uploading profile photo image failed.' }, 507 new_cache_values['photo'] = photo_url new_user_values['profile_photo'] = photo_url return_values[ 'photo'] = current_app.config['QINIU_DOMAIN'] + photo_url if new_user_values: User.query.filter_by(id=user_id).update(new_user_values) db.session.commit() if new_cache_values: cache_user.UserProfileCache.clear() return return_values, 201
def post(self): """ 上传图片文件 """ req_parser = RequestParser() req_parser.add_argument('image', type=parser.image_file, required=True, location='files') file = req_parser.parse_args() user_id = g.user_id try: image_key = upload_image(file['image'].read()) except Exception as e: current_app.logger.error('upload failed {}'.format(e)) return {'message': 'Uploading profile photo image failed.'}, 507 # TODO 图片默认审核通过 query = insert(Material).values( user_id=user_id, type=Material.TYPE.IMAGE, hash=image_key, url=image_key, status=Material.STATUS.APPROVED ).on_duplicate_key_update(status=Material.STATUS.APPROVED) db.session.execute(query) db.session.commit() material = Material.query.options(load_only(Material.id, Material.url))\ .filter_by(user_id=user_id, hash=image_key).first() return {'id': material.id, 'url': current_app.config['QINIU_DOMAIN'] + material.url}, 201
def patch(self): # 获取参数并进行检查 rp = RequestParser() rp.add_argument('photo', type=image_file, required=True, location='files') args_dict = rp.parse_args() photo = args_dict['photo'] # 上传图片到七牛云,获取图片key file_name = upload_image(photo.read()) # 把图片数据保存到数据库 User.query.filter(User.id == g.user_id).update( {'profile_photo': file_name}) # 上传,提交 db.session.commit() # 把图片的完整url返回 # photo_url = current_app.config['QINIU_DOMAIN']+file_name # return {'photo_url':photo_url} ret_dict = { 'photo_url': '{}/{}'.format(current_app.config['QINIU_DOMAIN'], file_name) } return ret_dict
def patch(self): print(request.__dict__) # 接收请求的参数,并做检查 rp = RequestParser() rp.add_argument('photo', type=image_file, required=True, location='files') args_dict = rp.parse_args() # 文件对象 photo = args_dict['photo'] # 上传图片到七牛云,获取图片key,就是图片的url名称 file_name = upload_image(photo.read()) # 把图片的名字保存到数据库 User.query.filter(User.id == g.user_id).update( {'profile_photo': file_name}) db.session.commit() # 把图片的完整url返回 ret_dict = { 'photo_url': '{}/{}'.format(current_app.config['QINIU_DOMAIN'], file_name) } return ret_dict
def post(self): """ 新增用户 """ json_parser = RequestParser() json_parser.add_argument('account', type=parser.mis_account, required=True, location='json') json_parser.add_argument('password', type=parser.mis_password, required=True, location='json') json_parser.add_argument('name', type=inputs.regex(r'^.{1,7}$'), required=True, location='json') json_parser.add_argument('profile_photo', type=parser.image_base64, required=False, location='json') json_parser.add_argument('introduction', required=False, location='json') json_parser.add_argument('email', type=parser.email, required=False, location='json') json_parser.add_argument('mobile', type=parser.mobile, required=True, location='json') json_parser.add_argument('gender', type=inputs.int_range(0, 1), required=True, location='json') json_parser.add_argument('area', required=False, location='json') json_parser.add_argument('company', required=False, location='json') json_parser.add_argument('career', required=False, location='json') args = json_parser.parse_args() if User.query.filter_by(account=args.account).first(): return {'message': '{} already exists'.format(args.account)}, 403 if User.query.filter_by(mobile=args.mobile).first(): return {'message': '{} already exists'.format(args.mobile)}, 403 user = User(account=args.account, password=generate_password_hash(args.password), name=args.name, introduction=args.introduction, email=args.email, mobile=args.mobile, status=User.STATUS.ENABLE, last_login=datetime.now()) if args.profile_photo: try: photo_url = upload_image(args.profile_photo) user.profile_photo = photo_url except Exception as e: current_app.logger.error('upload failed {}'.format(e)) return { 'message': 'Uploading profile photo image failed.' }, 507 db.session.add(user) db.session.commit() user_profile = UserProfile(id=user.id, gender=args.gender, area=args.area, company=args.company, career=args.career) db.session.add(user_profile) db.session.commit() return marshal(user, UserListResource.user_fields), 201
def patch(self): """ 编辑用户的信息 """ json_parser = RequestParser() json_parser.add_argument('name', type=inputs.regex(r'^.{1,7}$'), required=False, location='json') json_parser.add_argument('photo', type=parser.image_base64, required=False, location='json') json_parser.add_argument('gender', type=self._gender, required=False, location='json') json_parser.add_argument('birthday', type=parser.date, required=False, location='json') json_parser.add_argument('intro', type=inputs.regex(r'^.{1, 60}$'), required=False, location='json') json_parser.add_argument('real_name', type=inputs.regex(r'^.{1,7}$'), required=False, location='json') json_parser.add_argument('id_number', type=parser.id_number, required=False, location='json') json_parser.add_argument('id_card_front', type=parser.image_base64, required=False, location='json') json_parser.add_argument('id_card_back', type=parser.image_base64, required=False, location='json') json_parser.add_argument('id_card_handheld', type=parser.image_base64, required=False, location='json') args = json_parser.parse_args() user_id = g.user_id new_user_values = {} new_profile_values = {} return_values = {'id': user_id} need_delete_profilex = False need_delete_profile = False if args.name: new_user_values['name'] = args.name return_values['name'] = args.name need_delete_profile = True if args.photo: try: photo_url = upload_image(args.photo) except Exception as e: current_app.logger.error('upload failed {}'.format(e)) return { 'message': 'Uploading profile photo image failed.' }, 507 new_user_values['profile_photo'] = photo_url return_values[ 'photo'] = current_app.config['QINIU_DOMAIN'] + photo_url need_delete_profile = True if args.gender: new_profile_values['gender'] = args.gender return_values['gender'] = args.gender need_delete_profilex = True if args.birthday: new_profile_values['birthday'] = args.birthday return_values['birthday'] = args.birthday.strftime('%Y-%m-%d') need_delete_profilex = True if args.intro: new_user_values['introduction'] = args.intro return_values['intro'] = args.intro need_delete_profile = True if args.real_name: new_profile_values['real_name'] = args.real_name return_values['real_name'] = args.real_name if args.id_number: new_profile_values['id_number'] = args.id_number return_values['id_number'] = args.id_number if args.id_card_front: try: id_card_front_url = upload_image(args.id_card_front) except Exception as e: current_app.logger.error('upload failed {}'.format(e)) return { 'message': 'Uploading id_card_front image failed.' }, 507 new_profile_values['id_card_front'] = id_card_front_url return_values['id_card_front'] = current_app.config[ 'QINIU_DOMAIN'] + id_card_front_url if args.id_card_back: try: id_card_back_url = upload_image(args.id_card_back) except Exception as e: current_app.logger.error('upload failed {}'.format(e)) return {'message': 'Uploading id_card_back image failed.'}, 507 new_profile_values['id_card_back'] = id_card_back_url return_values['id_card_back'] = current_app.config[ 'QINIU_DOMAIN'] + id_card_back_url if args.id_card_handheld: try: id_card_handheld_url = upload_image(args.id_card_handheld) except Exception as e: current_app.logger.error('upload failed {}'.format(e)) return { 'message': 'Uploading id_card_handheld image failed.' }, 507 new_profile_values['id_card_handheld'] = id_card_handheld_url return_values['id_card_handheld'] = current_app.config[ 'QINIU_DOMAIN'] + id_card_handheld_url try: if new_user_values: User.query.filter_by(id=user_id).update(new_user_values) if new_profile_values: UserProfile.query.filter_by( id=user_id).update(new_profile_values) db.session.commit() except IntegrityError: db.session.rollback() return {'message': 'User name has existed.'}, 409 if need_delete_profile: cache_user.UserProfileCache(user_id).clear() if need_delete_profilex: cache_user.UserAdditionalProfileCache(user_id).clear() return return_values, 201