def put_vip(): if request.method != 'PUT' and request.method != 'POST': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = UserPutVipForm() if form.validate_for_api() and form.validate(): vip = form.vip.data if vip <= 0: return params_error(message="vip天数需大于0") coins = form.coins.data if coins <= 0: return params_error(message="P币数需大于0") user = db.session.query(User).filter_by(id=g.user.uid).first() if user: if coins > user.coins: return params_error(message="用户P币数不够") user.coins -= coins user.vip += vip user.vip_end = ( datetime.datetime.now() + datetime.timedelta(days=user.vip)).strftime('%Y-%m-%d') db.session.commit() data = {'vip': user.vip_end, 'coins': user.coins} return success(message="购买大会员成功", data=data) else: return params_error(message="未查到用户") else: return params_error(message=form.get_error())
def upload_background(): if request.method != 'POST' and 'content' in request.files: raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = ImageUploadForm() if form.validate(): content = request.files['content'] uid = g.user.uid filename = secure_filename(content.filename) path = basedir + "image/temp" user = db.session.query(User).filter_by(id=uid).first() if user.background: bucket.delete_object(user.background) time = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S') if filename.endswith('jpg'): background_name = str(uid) + '-' + time + '-background.jpg' elif filename.endswith('png'): background_name = str(uid) + '-' + time + '-background.png' else: return params_error(message="文件类型错误") filename = filename + time + "-" + str(uid) content.save(os.path.join(path, filename)) bucket.put_object_from_file(background_name, path + '/' + filename) user.background = background_name db.session.commit() os.remove(path + '/' + filename) data = {'background': background_name} return success(data=data, message="上传背景成功") else: return params_error(message=form.get_error())
def get_video(id_): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) all_video = db.session.query(Video).filter_by(uid=id_).order_by( db.desc(Video.upload_time)).all() video_list = [] if all_video: for video in all_video: video_item = { 'pv': video.id, 'title': video.title, 'type': video.type, 'duration': video.duration, 'likes': len(list(map(int, video.likes_user.split(',')))) if video.likes_user else 0, 'views': len(list(map(int, video.views.split(',')))) if video.views else 0, 'danmuku': video.danmuku, 'time': video.upload_time.strftime('%Y-%m-%d %H:%M:%S'), 'bucket_cover': get_bucket_token(video.cover) } video_list.append(video_item) data = {'video_list': video_list} return success(data=data, message="获取投稿成功")
def list_followings(id_): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) user = db.session.query(User).filter_by(id=id_).first() if user: list_ = [] if not user.followings: data = {'list': list_} else: all_followings = list(map(int, user.followings.split(','))) for item_id in all_followings: item_user = db.session.query(User).filter_by( id=item_id).first() is_followed = False if item_user.fans: fans = list(map(int, item_user.fans.split(','))) if g.user.uid in fans: is_followed = True user = { 'id': item_user.id, 'username': item_user.username, 'sign': item_user.sign, 'is_followed': is_followed } list_.append(user) data = {'list': list_} return success(message="获取成功", data=data) else: return params_error(message='用户未找到')
def get_fans(): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = UserGetFanForm() if form.validate_for_api() and form.validate(): user_id = form.id.data page = form.page.data page_count = form.page_count start = (page - 1) * page_count end = start + page_count fans = [] user = db.session.query(User).filter_by(id=user_id).first() if user: if not user.fans: data = {'fans': ''} else: all_fans = list(map(int, user.fans.split(','))) for i in range(start, end): if i < len(all_fans): fans.append(all_fans[i]) else: break data = {'fans': fans} return success(message="获取成功", data=data) else: return params_error(message=form.get_error())
def details(): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) user_id = g.user.uid user = db.session.query(User).filter_by(id=user_id).first() if user: data = { 'id': user.id, 'username': user.username, 'email': user.email, 'gender': user.gender, 'coins': user.coins, 'vip': user.vip_end, 'fans_count': len(list(map(int, user.fans.split(',')))) if user.fans else 0, 'followings_count': len(list(map(int, user.followings.split(',')))) if user.followings else 0 } return success(message="获取用户信息成功", data=data) else: return params_error(message='未查到用户信息')
def get_background(id_): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) user = db.session.query(User).filter_by(id=id_).first() if user: background_path = user.background return success(message="背景", data=get_bucket_token(background_path)) else: return params_error(message="未查到用户背景")
def un_fan(): if request.method != 'PUT' and request.method != 'POST': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = UserFanForm() if form.validate_for_api() and form.validate(): target_id = form.id.data user_id = g.user.uid if user_id == target_id: return params_error(message="无法取关自己") user = db.session.query(User).filter_by(id=user_id).first() if user: if not user.followings: return params_error(message="关注数为0") followings = list(map(int, user.followings.split(','))) if not followings: return params_error(message="关注数为0") if target_id not in followings: return params_error(message="未粉") followings.remove(target_id) if followings: user.followings = ','.join(str(i) for i in followings) else: user.followings = None db.session.commit() target_user = db.session.query(User).filter_by( id=target_id).first() if not target_user.fans: return params_error(message="粉丝数为0") fans = list(map(int, target_user.fans.split(','))) if not fans: return params_error(message="粉丝数为0") if user_id not in fans: return params_error(message="不是粉丝") fans.remove(user_id) if followings: target_user.fans = ','.join(str(i) for i in fans) else: target_user.fans = None db.session.commit() data = { 'fans_count': len(list(map(int, target_user.fans.split(',')))) if target_user.fans else 0, } return success(message="取消关注", data=data) else: return params_error(message=form.get_error())
def get_followings(id_): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) user = db.session.query(User).filter_by(id=id_).first() if user: if not user.followings: data = {'list': ''} else: all_followings = list(map(int, user.followings.split(','))) data = {'list': all_followings} return success(message="获取成功", data=data) else: return params_error(message='用户未找到')
def is_followed(id_): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) user = db.session.query(User).filter_by(id=id_).first() if user: is_followed = False if user.fans: fans = list(map(int, user.fans.split(','))) if g.user.uid in fans: is_followed = True data = {'is_followed': is_followed} return success(message="获取成功", data=data) else: return params_error(message="未查到用户")
def upload_sign(): if request.method != 'POST': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = UserPutSignForm() if form.validate_for_api and form.validate(): user = db.session.query(User).filter_by(id=g.user.uid).first() if user: user.sign = form.sign.data db.session.commit() data = {'sign': user.sign} return success(message="签名", data=data) else: return params_error(message="未查到用户") else: return params_error(message=form.get_error())
def put_password(): if request.method != 'PUT' and request.method != 'POST': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = PutPasswordForm() if form.validate_for_api() and form.validate(): password = form.password.data user = db.session.query(User).filter_by(id=g.user.uid).first() if user: user._password = generate_password_hash(password) db.session.commit() return success(message="修改成功") else: return params_error(message="未查到用户") else: return params_error(message=form.get_error())
def fan(): if request.method != 'PUT' and request.method != 'POST': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = UserFanForm() if form.validate_for_api() and form.validate(): target_id = form.id.data user_id = g.user.uid if user_id == target_id: return params_error(message="无法关注自己") user = db.session.query(User).filter_by(id=user_id).first() target_user = db.session.query(User).filter_by(id=target_id).first() if not target_user: return params_error(message="未查到用户") if user: if user.followings: followings = list(map(int, user.followings.split(','))) if target_id in followings: return params_error(message="已粉") followings.append(target_id) user.followings = ','.join(str(i) for i in followings) db.session.commit() else: followings = [target_id] user.followings = followings db.session.commit() if target_user.fans: fans = list(map(int, target_user.fans.split(','))) if user_id not in fans: fans.append(user_id) target_user.fans = ','.join(str(i) for i in fans) db.session.commit() else: return params_error(message="已是粉丝") else: fans = [user_id] target_user.fans = fans db.session.commit() data = { 'fans_count': len(list(map(int, target_user.fans.split(',')))) } return success(message="已关注", data=data) else: return params_error(message=form.get_error())
def upload_avatar_save(): if request.method != 'POST': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = ImageUploadNewForm() if form.validate_for_api() and form.validate(): filename = form.filename.data uid = g.user.uid filename = secure_filename(filename) user = db.session.query(User).filter_by(id=uid).first() if user.avatar: bucket.delete_object(user.avatar) user.avatar = filename db.session.commit() return success(message="上传头像成功") else: return params_error(message=form.get_error())
def put_gender(): if request.method != 'PUT' and request.method != 'POST': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = UserPutGenderForm() if form.validate_for_api and form.validate(): user = db.session.query(User).filter_by(id=g.user.uid).first() if user: gender = form.gender.data user.gender = gender db.session.commit() data = {'gender': user.gender} return success(message="性别", data=data) else: return params_error(message="未查到用户") else: return params_error(message=form.get_error())
def register(): # 注册 # 验证Request method是否为POST,如果不是抛出405 if request.method.upper() != 'POST': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = RegisterForm() if form.validate_for_api() and form.validate(): if validate_email(form.email) and validate_username(form.username): email = form.email.data password = form.password.data username = form.username.data user = User(email=email, username=username, password=password) db.session.add(user) db.session.commit() return success(message="注册成功") return params_error(message=form.get_error())
def check_password(): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = PutPasswordForm() if form.validate_for_api() and form.validate(): password = form.password.data user = db.session.query(User).filter_by(id=g.user.uid).first() if user: if user.check_password(password): return success(message="密码正确") else: return params_error(message="密码错误") else: return params_error(message="未查到用户") else: return params_error(message=form.get_error())
def login(): # 登录 # 验证Request method是否为POST,如果不是抛出405 if request.method != 'POST': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = LoginForm() # 检查表单 if request.method == 'POST' and form.validate_for_api() and form.validate( ): user = db.session.query(User).filter_by(email=form.email.data).first() if not user or not user.check_password(form.password.data): return unauthorized_error(message="邮箱或密码错误") # 生成token token = generate_token(user.id) result = {'username': user.username, 'token': token.decode('ascii')} return success(data=result, message="登录成功") return params_error(message=form.get_error())
def get_space(): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) user = db.session.query(User).filter_by(id=g.user.uid).first() if user: all_video = db.session.query(Video).filter_by(uid=g.user.uid).all() likes = 0 if all_video: for video in all_video: likes += len(list(map( int, video.likes_user.split(',')))) if video.likes_user else 0 data = { 'sign': user.sign if user.sign else '这个人很懒,什么都没有留下', 'likes': likes } return success(message="空间数据", data=data) else: return params_error(message="未查到用户")
def put_coin(): if request.method != 'PUT' and request.method != 'POST': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = UserPutCoinForm() if form.validate_for_api() and form.validate(): count = form.coins.data user_id = g.user.uid user = db.session.query(User).filter_by(id=user_id).first() if user: coins = user.coins if count <= 0: return params_error(message="币数错误") user.coins = coins + count db.session.commit() data = { 'coins': coins + count, } return success(message="购买P币成功", data=data) else: return params_error(message=form.get_error())
def open_details(id_): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) user = db.session.query(User).filter_by(id=id_).first() if user: all_video = db.session.query(Video).filter_by(uid=id_).all() likes = 0 if all_video: for video in all_video: likes += len(list(map( int, video.likes_user.split(',')))) if video.likes_user else 0 data = { 'id': user.id, 'username': user.username, 'gender': user.gender, 'vip': user.vip_end, 'sign': user.sign, 'likes': likes, 'fans_count': len(list(map(int, user.fans.split(',')))) if user.fans else 0, 'followings_count': len(list(map(int, user.followings.split(',')))) if user.followings else 0 } return success(message="获取用户信息成功", data=data) else: return params_error(message="未查到用户信息")
def upload_avatar_new(): if request.method != 'POST': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = ImageUploadNewForm() if form.validate_for_api() and form.validate(): filename = form.filename.data uid = g.user.uid filename = secure_filename(filename) user = db.session.query(User).filter_by(id=uid).first() if user.avatar: bucket.delete_object(user.avatar) time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') if filename.endswith('jpg'): avatar_name = str(uid) + '-' + time + '-avatar.jpg' elif filename.endswith('png'): avatar_name = str(uid) + '-' + time + '-avatar.png' else: return params_error(message="文件类型错误") data = {'bucket_avatar': get_bucket_token(avatar_name)} return success(data=data, message="上传头像成功") else: return params_error(message=form.get_error())
def put_username(): if request.method != 'PUT' and request.method != 'POST': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = UserPutUsernameForm() if form.validate_for_api() and form.validate(): if validate_username(form.username): username = form.username.data cost = form.cost.data if cost < 0: return params_error(message="币数错误") user_id = g.user.uid user = db.session.query(User).filter_by(id=user_id).first() if user: user.username = username if cost > user.coins: return params_error(message="币数不足") user.coins = user.coins - cost db.session.commit() data = {'username': username, 'coins': user.coins} return success(message="改名成功", data=data) else: return params_error(message=form.get_error())