def validate_username(self, field): with session_cm() as session: pattern = re.compile(r'^\w{6,16}$') if pattern.match(field.data) is None: raise ValueError(u"用户名只能由字母、下划线、数字组成,且字符数在(6-16)之间") if session.query(User).filter(User.username == field.data).first(): raise ValueError(u"该用户名已经存在")
def get_todo(): res = ajax_response() todo_id = request.args.get('todo_id') with session_cm() as db: td = db.query(Todo).get(todo_id) res.update(data=td.to_dict()) return jsonify(res)
def down_vote(): res = ajax_response() todo_id = request.args.get('todo_id') if not current_user.is_authenticated(): res.update({ 'info': '还木有登录呢,太没诚意了……', 'type': 'error' }) return jsonify(res) if request.cookies.get('voted_todo_' + str(todo_id)): res.update({ 'info': '你已经表示过了,谢谢了哈……', 'type': 'info' }) return jsonify(res) with session_cm() as db: td = db.query(Todo).get(todo_id) if not td.todo_down_vote: td.todo_down_vote = 0 td.todo_down_vote += 1 res.update(data=td.todo_down_vote) res.update({ 'data': td.todo_down_vote, 'info': '如果你真心觉得不靠谱,希望你能给予正确的指点或者意见,谢谢!' }) db.commit() response = jsonify(res) response.set_cookie('voted_todo_' + str(todo_id), 'voted') return response
def my_friends(): res = ajax_response() with session_cm() as session: user_list = session.query(User).all() user_dict_list = [] for user in user_list: user_dict_list.append(user.to_dict()) res.update(data={"items": user_dict_list}) return jsonify(res)
def update_profile(): res = ajax_response() desc = request.form.get('desc') with session_cm() as session: user = session.query(User).get(current_user.user_id) user.description = desc res['info'] = '更新用户资料成功' session.commit() return jsonify(res)
def delete_todo(): res = ajax_response() todo_id = request.args.get('todo_id') with session_cm() as db: td = db.query(Todo).get(todo_id) td.todo_is_deleted = True res['info'] = '删除成功' db.commit() return jsonify(res)
def change_visible(): res = ajax_response() todo_id = request.args.get('todo_id') with session_cm() as db: td = db.query(Todo).get(todo_id) td.todo_visible = not td.todo_visible res['data'] = td.to_dict() res['info'] = '成功设置该计划对所有人可见' if td.todo_visible else '成功设置该计划为私密计划' db.commit() return jsonify(res)
def mark_complete(): res = ajax_response() todo_id = request.args.get('todo_id') with session_cm() as db: td = db.query(Todo).get(todo_id) td.todo_is_completed = not td.todo_is_completed res['data'] = td.to_dict() res['info'] = '成功标记该计划为已完成' if td.todo_is_completed else '成功标记该计划为未完成' db.commit() return jsonify(res)
def latest_todos(): res = ajax_response() with session_cm() as db: latest_todo_list = db.query(Todo).\ filter(Todo.todo_is_deleted == False, Todo.todo_visible == True).order_by(Todo.created_date.asc()).all() items = [] for td in latest_todo_list: items.append(td.to_dict()) res.update(data={'items': items}) return jsonify(res)
def user_home(username): res = ajax_response() with session_cm() as db: owner = db.query(User).filter_by(username=username).first() if owner: res.update(data={'owner': owner.to_dict()}) session["owner_id"] = owner.user_id return jsonify(res) else: abort(404)
def my_friends(): res = ajax_response() with session_cm() as session: user_list = session.query(User).all() user_dict_list = [] for user in user_list: user_dict_list.append(user.to_dict()) res.update(data={ "items": user_dict_list }) return jsonify(res)
def user_home(username): res = ajax_response() with session_cm() as db: owner = db.query(User).filter_by(username=username).first() if owner: res.update(data={ 'owner': owner.to_dict() }) session["owner_id"] = owner.user_id return jsonify(res) else: abort(404)
def latest_todos(): res = ajax_response() with session_cm() as db: latest_todo_list = db.query(Todo).\ filter(Todo.todo_is_deleted == False, Todo.todo_visible == True).order_by(Todo.created_date.asc()).all() items = [] for td in latest_todo_list: items.append(td.to_dict()) res.update(data={ 'items': items }) return jsonify(res)
def add_new_complete(): res = ajax_response() ac_name = request.form.get('ac_name') ac_description = request.form.get('ac_description') todo_id = request.form.get('todo_id') with session_cm() as db: ac = Achievement(ac_name=ac_name, ac_description=ac_description, todo_id=todo_id, user_id=current_user.user_id) db.add(ac) db.commit() res.update(data=ac.to_dict()) return jsonify(res)
def validate_user(cls, email, password): # TODO: 验证用户是否激活,否则发送激活邮件 validate_user_errors = { 'user_error': None, 'password_error': None } with session_cm() as session: user = session.query(User).filter(User.email == email).first() if not user: validate_user_errors['user_error'] = u'该邮箱尚未注册' return None, validate_user_errors elif not user.check_password(password): validate_user_errors['password_error'] = u'密码错误' return None, validate_user_errors return user, validate_user_errors
def reset_password(): res = ajax_response() old_password = request.form.get('old_password') new_password = request.form.get('new_password') new_password_confirm = request.form.get('new_password_confirm') if new_password and new_password_confirm and new_password_confirm != new_password: res.update({'info': '两次输入的新密码不一致', 'type': 'error'}) return jsonify(res) if not current_user.check_password(old_password): res.update({'info': '原始密码错误', 'type': 'error'}) return jsonify(res) with session_cm() as session: session.query(User).get( current_user.user_id).set_password(new_password) session.commit() res.update(info='密码修改成功') return jsonify(res)
def up_vote(): res = ajax_response() todo_id = request.args.get('todo_id') if not current_user.is_authenticated(): res.update({'info': '还木有登录呢,太没诚意了……', 'type': 'error'}) return jsonify(res) if request.cookies.get('voted_todo_' + str(todo_id)): res.update({'info': '你已经表示过了,谢谢了哈……', 'type': 'info'}) return jsonify(res) with session_cm() as db: td = db.query(Todo).get(todo_id) if not td.todo_up_vote: td.todo_up_vote = 0 td.todo_up_vote += 1 res.update({'data': td.todo_up_vote, 'info': '鼓励别人,也是一种美德,谢谢!'}) db.commit() response = jsonify(res) response.set_cookie('voted_todo_' + str(todo_id), 'voted') return response
def add_todo(): todo_name = request.form.get("todo_name") todo_description = request.form.get("todo_description") todo_start = request.form.get("todo_start") todo_end = request.form.get("todo_end") todo_visible = request.form.get("todo_visible") is not None todo_erasable = request.form.get("todo_erasable") is None res = ajax_response() with session_cm() as db: td = Todo(user_id=current_user.user_id, todo_name=todo_name, todo_description=todo_description, todo_start=todo_start, todo_end=todo_end, todo_visible=todo_visible, todo_erasable=todo_erasable) db.add(td) db.commit() res.update(data=td.to_dict()) return jsonify(res)
def register(): """ 注册一个新的用户 """ template_var = {} form = RegisterForm() if request.method == "POST" and form.validate(): form = RegisterForm(request.form) with session_cm() as session: user = User() user.username = form.username.data user.nickname = form.nickname.data user.email = form.email.data user.set_password(form.password.data) session.add(user) if user is not None: login_user(user) session.commit() return redirect(url_for('frontend.index')) template_var['form'] = form return render_template('account/register.html', **template_var)
def reset_password(): res = ajax_response() old_password = request.form.get('old_password') new_password = request.form.get('new_password') new_password_confirm = request.form.get('new_password_confirm') if new_password and new_password_confirm and new_password_confirm != new_password: res.update({ 'info': '两次输入的新密码不一致', 'type': 'error' }) return jsonify(res) if not current_user.check_password(old_password): res.update({ 'info': '原始密码错误', 'type': 'error' }) return jsonify(res) with session_cm() as session: session.query(User).get(current_user.user_id).set_password(new_password) session.commit() res.update(info='密码修改成功') return jsonify(res)
def my_todos(): res = ajax_response() flag = request.args.get('flag', 'doing') todo_id = request.args.get('todo_id') with session_cm() as db: if todo_id: my_todo_list = db.query(Todo).filter(Todo.todo_id == todo_id).all() else: my_todo_list_query = db.query(Todo). \ filter(Todo.todo_is_deleted == False, Todo.user_id == session["owner_id"]) if not current_user.is_authenticated() or \ (current_user.is_authenticated() and current_user.user_id != session["owner_id"]): my_todo_list_query = my_todo_list_query.filter(Todo.todo_visible == True) if flag and flag == 'doing': my_todo_list_query = my_todo_list_query.\ filter(Todo.todo_is_completed == False, Todo.todo_end > datetime.date.today()) if flag and flag == 'completed': my_todo_list_query = my_todo_list_query.\ filter(Todo.todo_is_completed == True) if flag and flag == 'failed': my_todo_list_query = my_todo_list_query.\ filter(Todo.todo_is_completed == False, Todo.todo_end < datetime.date.today()) my_todo_list = my_todo_list_query.options(joinedload('achievement_list')).all() items = [] for td in my_todo_list: items.append(td.to_dict()) res.update(data={ 'items': items }) return jsonify(res)
def my_todos(): res = ajax_response() flag = request.args.get('flag', 'doing') todo_id = request.args.get('todo_id') with session_cm() as db: if todo_id: my_todo_list = db.query(Todo).filter(Todo.todo_id == todo_id).all() else: my_todo_list_query = db.query(Todo). \ filter(Todo.todo_is_deleted == False, Todo.user_id == session["owner_id"]) if not current_user.is_authenticated() or \ (current_user.is_authenticated() and current_user.user_id != session["owner_id"]): my_todo_list_query = my_todo_list_query.filter( Todo.todo_visible == True) if flag and flag == 'doing': my_todo_list_query = my_todo_list_query.\ filter(Todo.todo_is_completed == False, Todo.todo_end > datetime.date.today()) if flag and flag == 'completed': my_todo_list_query = my_todo_list_query.\ filter(Todo.todo_is_completed == True) if flag and flag == 'failed': my_todo_list_query = my_todo_list_query.\ filter(Todo.todo_is_completed == False, Todo.todo_end < datetime.date.today()) my_todo_list = my_todo_list_query.options( joinedload('achievement_list')).all() items = [] for td in my_todo_list: items.append(td.to_dict()) res.update(data={'items': items}) return jsonify(res)
def load_user(user_id): with session_cm() as session: return session.query(User).get(user_id)
def validate_email(self, field): with session_cm() as session: if session.query(User).filter(User.email == field.data).first(): raise ValueError(u"该邮箱已经存在")