def delete(self): form = request.get_json(True, True) user_id = auth_helper() task_id = form.get('task_id') if not task_id: return dict(error="请指定任务"), 400 task = Task.get(task_id=task_id) if not task: return dict(error="该任务不存在"), 400 task = task[0] participator_id = form.get('participator_id') if not participator_id: return dict(error="参与者不能为空"), 400 participate = Participate.get(user_id=participator_id, task_id=task_id) if not participate: return dict(error="该参与信息不存在"), 400 if user_id != participator_id: # 参与者才能取消参与任务 return dict(error="您没有操作权限"), 403 participate = participate[0] db.session.delete(participate) db.session.commit() # 发消息给甲方,乙方退出了任务 user = User.get_by_id(user_id) message = Message(user_id=task.creator_id, content=f'用户 {user.username} 退出了您的任务 {task.title}') db.session.add(message) db.session.commit() # 不退还乙方押金 return dict(data="ok"), 200
def appeal_task(): # 乙方(是否)申诉甲方的审核结果 user_id = auth_helper() form = request.get_json(True, True) task_id = form.get('task_id') view = form.get('view') if not task_id: return jsonify(error='请指定任务'), 400 task = Task.get(task_id=task_id) if not task: return jsonify(error='任务不存在'), 400 task = task[0] if not view or (view != 'yes' and view != 'no'): return jsonify(error='请指定正确的申诉请求'), 400 participate = Participate.get(user_id=user_id, task_id=task_id) if not participate: return jsonify(error='未参与该任务'), 400 participate = participate[0] if participate.status != ParticipateStatus.FINISH.value: return jsonify(error='未完成该任务'), 400 if view == 'yes': # 确认申诉 # 发消息给admin user = User.get_by_id(user_id) message = Message( user_id=ADMIN_ID, content=f'用户 {user.username} 对任务 {task.title} 的结果进行申诉') db.session.add(message) db.session.commit() else: # 不申诉 # 不退还乙方押金 pass return jsonify(data='申诉完成'), 200
def update_avatar(): user_id = auth_helper() flag, msg = upload_file(f"avatar-{user_id}") if not flag: return jsonify(error=msg), 400 user = User.get_by_id(user_id) user.avatar = msg db.session.commit() return jsonify(data=msg), 200
def get(self): user_id = auth_helper() message_id = request.args.get('message_id') messages = Message.get(user_id=user_id, message_id=message_id) if len(messages) > 1: for i in range(0, len(messages) - 1): for j in range(i + 1, len(messages)): if messages[i].id < messages[j].id: t = messages[i] messages[i] = messages[j] messages[j] = t result = [{ "id": message.id, "user_id": message.user_id, "content": message.content } for message in messages] for value in result: user_id = value['user_id'] user = User.get_by_id(user_id) value['username'] = user.username return dict(data=result), 200
def post(self): user_id = auth_helper() form = request.get_json(True, True) task_id = form.get('task_id') if not task_id: return dict(error='请指定任务'), 400 task = Task.get(task_id=task_id) if not task: return dict(error='该任务不存在'), 400 task = task[0] if task.creator_id == user_id: return dict(error='发起者无需申请参与该任务'), 400 # 支付押金 try: change_balance(user_id, -1 * PLEDGE) except RuntimeError as e: return dict(error=f'{e}'), 400 try: participate = Participate(user_id=user_id, task_id=task_id, status=ParticipateStatus.APPLYING.value) db.session.add(participate) db.session.commit() except exc.IntegrityError as e: logging.error(f'participate failed, msg: {e}') if re.search(r"Duplicate entry '\S*' for key '\S*'", e.orig.args[1]): return dict(error='您已经提交过该任务的申请'), 400 elif re.search(r"Cannot add or update a child row", e.orig.args[1]): return dict(error='该任务不存在'), 400 else: return dict(error=f'{e}'), 400 # 发申请消息给甲方 user = User.get_by_id(user_id) message = Message(user_id=task.creator_id, content=f'用户 {user.username} 申请参加任务 {task.title}', msg_type=MessageType.APPLY.value, send_from=user_id) db.session.add(message) db.session.commit() return dict(data="已成功发出申请"), 200
def finish_participate(): # 乙方确认完成任务 user_id = auth_helper() form = request.get_json(True, True) task_id = form.get('task_id') if not task_id: return jsonify(error='请指定任务'), 400 task = Task.get_by_id(task_id) if not task: return jsonify(error='任务不存在'), 400 participate = Participate.get(user_id=user_id, task_id=task_id) if not participate or participate[0].status == ParticipateStatus.APPLYING.value: return jsonify(error='未参与该任务'), 400 participate = participate[0] if participate.status == ParticipateStatus.FINISH.value: return jsonify(error='已完成该任务') participate.status = ParticipateStatus.CHECK.value db.session.commit() # 发消息告知甲方 乙方完成任务 user = User.get_by_id(user_id) message = Message(user_id=task.creator_id, content=f'用户 {user.username} 已完成任务 {task.title}, 请进行验收', msg_type=MessageType.FINISH.value, send_from=user.id) db.session.add(message) db.session.commit() return jsonify(data='确认成功'), 200