示例#1
0
文件: view.py 项目: sarike/timefly
 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"该用户名已经存在")
示例#2
0
文件: views.py 项目: depsi/timefly
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)
示例#3
0
文件: views.py 项目: sarike/timefly
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)
示例#4
0
文件: view.py 项目: depsi/timefly
 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"该用户名已经存在")
示例#5
0
文件: views.py 项目: depsi/timefly
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
示例#6
0
文件: view.py 项目: sarike/timefly
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)
示例#7
0
文件: view.py 项目: depsi/timefly
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)
示例#8
0
文件: view.py 项目: sarike/timefly
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)
示例#9
0
文件: views.py 项目: depsi/timefly
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)
示例#10
0
文件: views.py 项目: sarike/timefly
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)
示例#11
0
文件: views.py 项目: depsi/timefly
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)
示例#12
0
文件: views.py 项目: depsi/timefly
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)
示例#13
0
文件: views.py 项目: sarike/timefly
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)
示例#14
0
文件: views.py 项目: sarike/timefly
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)
示例#15
0
文件: views.py 项目: sarike/timefly
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)
示例#16
0
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)
示例#17
0
文件: view.py 项目: depsi/timefly
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)
示例#18
0
文件: views.py 项目: depsi/timefly
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)
示例#19
0
文件: views.py 项目: depsi/timefly
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)
示例#20
0
文件: views.py 项目: depsi/timefly
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)
示例#21
0
文件: views.py 项目: sarike/timefly
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)
示例#22
0
文件: user.py 项目: sarike/timefly
 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
示例#23
0
文件: view.py 项目: sarike/timefly
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)
示例#24
0
文件: views.py 项目: sarike/timefly
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
示例#25
0
文件: views.py 项目: depsi/timefly
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)
示例#26
0
文件: views.py 项目: sarike/timefly
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)
示例#27
0
文件: view.py 项目: depsi/timefly
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)
示例#28
0
文件: view.py 项目: sarike/timefly
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)
示例#29
0
文件: view.py 项目: depsi/timefly
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)
示例#30
0
文件: views.py 项目: depsi/timefly
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)
示例#31
0
文件: views.py 项目: sarike/timefly
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)
示例#32
0
文件: wsgi.py 项目: sarike/timefly
def load_user(user_id):
    with session_cm() as session:
        return session.query(User).get(user_id)
示例#33
0
文件: view.py 项目: sarike/timefly
 def validate_email(self, field):
     with session_cm() as session:
         if session.query(User).filter(User.email == field.data).first():
             raise ValueError(u"该邮箱已经存在")
示例#34
0
文件: view.py 项目: depsi/timefly
 def validate_email(self, field):
     with session_cm() as session:
         if session.query(User).filter(User.email == field.data).first():
             raise ValueError(u"该邮箱已经存在")