def join_project(): """加入项目""" project = Project.check_invitation(g.data['invitation']) if not project: return make_err('邀请券无效或已过期') project.members.append(g.user) return make_resp(project.simple(g.user.id))
def create_project(): """创建项目""" if g.user.projects and g.user.projects[-1].create_time > get_time(0, -60): return make_err('您的操作太频繁') project = Project.append(g.user.id, g.data['name'], g.data.get('describe')) project.members.append(g.user) db_session.flush() return make_resp(project.simple(g.user.id))
def delete_project(): """删除项目""" project = Project.query.get(g.data['project_id']) if not project: return make_err('项目不存在或已删除') if project.owner != g.user: return make_err('项目所有者才有权删除项目') db_session.delete(project) return make_resp('fin')
def delete_contact(): """删除联系人""" db_session.execute( 'DELETE FROM user_contact WHERE user_id = :user_id AND contact_id = :contact_id', { 'user_id': g.user.id, 'contact_id': g.data['user_id'] }) return make_resp('fin')
def add_member(): """添加项目成员""" project = Project.query.get(g.data['project_id']) if not project: return make_err('项目不存在或已删除') if g.user not in project.members: return make_err('项目的成员才有权添加新成员') user = User.query.get(g.data['user_id']) if not user: return make_err('无效的用户ID') project.members.append(user) return make_resp('fin')
def get_project_detail(): """获取项目详情""" project = Project.query.get(g.data['project_id']) if not project: return make_err('项目不存在或已删除') return make_resp({ 'id': project.id, 'owner': project.owner.simple(), 'is_owner': project.owner.id == g.user.id, 'name': project.name, 'describe': project.describe, 'create_time': project.create_time, 'status': project.status, 'members': [member.simple() for member in project.members] })
def login_user(): """用户登录""" user, err = User.login( g.data.get('account'), g.data.get('password'), request.cookies.get('user'), ) if err: return make_err(err) session['user'] = user.id token = user.account.create_token() user_dict = user.simple() user_dict['token'] = token resp = make_response(make_resp(user_dict)) resp.set_cookie('user', token) return resp
def reset_password(): """重置密码(副作用可以创建User)""" account = Account.query.get(request.cookies.get('verify_account')) if not account: return make_err('设置密码失败,无效的cookie或已过期') # if not account.check_password(g.data['code']): # return make_err('设置密码失败,验证码无效或已过期') User.append( account.code, g.data['password'], g.data.get('name', account.name), g.data.get('email', account.email), g.data.get('phone', account.phone)) resp = make_response(make_resp('fin')) resp.delete_cookie('verify_account') return resp
def remove_member(): """移除项目成员(或成员从自己项目中移除该项目,即退出)""" project = Project.query.get(g.data['project_id']) if not project: return make_err('项目不存在或已删除') user_id = g.data.get('user_id', g.user.id) if int(user_id) == project.owner_id: return make_err('项目所有者不能退出') if user_id != g.user.id and project.owner != g.user: return make_err('项目所有者才有权移除其他成员') db_session.execute( 'DELETE FROM project_member WHERE project_id = :project_id AND user_id = :user_id', { 'project_id': project.id, 'user_id': user_id }) return make_resp('fin')
def get_verify_code(): """获取验证码(副作用为创建Account)""" code = g.data['account'] email = g.data['email'] account = Account.query \ .filter(Account.code == code) \ .filter(Account.email == email) \ .first() if not account: if Account.query.filter(or_(Account.code == code, Account.email == email)).first(): return make_err('账户已存在且账号邮箱不匹配') account = Account.append( code, '%06d' % randint(0, 999999), code, email) db_session.flush() # TODO 发送验证码到邮箱 resp = make_response(make_resp('fin')) resp.set_cookie('verify_account', str(account.id)) return resp
def search_user(): """查询用户""" return make_resp( [user.simple() for user in User.search(g.data['keyword'])])
def logout(): """登出""" session.pop('user') resp = make_response(make_resp('fin')) resp.delete_cookie('user') return resp
def update_password(): """修改密码""" _, err = g.user.change_password(g.data['oldPasswd'], g.data['newPasswd']) if err: return make_err(err) return make_resp('fin')
def pull_time(): """拉取当前时间""" if False: return make_err("Error reason.") return make_resp(str(datetime.datetime.now()))
def list_project(): """列出项目""" return make_resp( [project.simple(g.user.id) for project in g.user.projects])
def list_contact(): """列出联系人""" return make_resp([contact.simple() for contact in g.user.contacts])
def add_contact(): """添加联系人""" err = g.user.add_contact(g.data['user_id']) if err: return make_err(err) return make_resp('fin')
def generate_invitation(): """生成邀请券""" project = Project.query.get(g.data['project_id']) return make_resp(project.gen_invitation(g.user.id))