Пример #1
0
def update(request):
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        return redirect('/login')
    if request.method == 'POST':
        form = request.form()
        todo_id = int(form.get('id', -1))
        t = Todo.find_by(id=todo_id)
        t.title = form.get('title', t.title)
        t.save()
    return redirect('/todo')
Пример #2
0
def delete_todo(request):
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        return redirect('/login')
    todo_id = int(request.query.get('id', -1))
    t = Todo.find_by(id=todo_id)
    if t.user_id != u.id:
        return redirect('/login')
    if t is not None:
        t.remove()
    return redirect('/todo')
Пример #3
0
def add(request):
    """
    add 功能
    """
    headers = {
        'Content-Type': 'text/html',
    }
    if request.method == 'POST':
        form = request.form()
        t = Todo.new(form)
        # 保存todo到数据库
        t.save()
    # 拿到后端返回的数据后,再重定向到首页,就可以看到新增的数据
    return redirect('/todo')
Пример #4
0
def index(request):
    """
    todo首页,查看所有的todo
    """
    headers = {
        'Content-Type': 'text/html'
    }
    # 拿到 所有todo
    todo_list = Todo.all()
    # 生成todo的html
    todo_html = ''.join(['<h3>{}: {}</h3>'.format(t.id, t.title) for t in todo_list])
    # 替换模板文件中的字符串
    body = template('todo_index.html')
    body = body.replace('{{todos}}', todo_html)
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode(encoding='utf')
Пример #5
0
def edit(request):
    headers = {
        'Content-Type': 'text/html',
    }
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        return redirect('/login')
    todo_id = int(request.query.get('id', -1))
    t = Todo.find_by(id=todo_id)
    if t.user_id != u.id:
        return redirect('/login')
    body = templates('todo_edit.html', todos=t)
    body = body.replace('{{todo_id}}', str(t.id))
    # print('t.id', t.id)
    body = body.replace('{{todo_title}}', str(t.title))
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Пример #6
0
def index(request):
    headers = {'Content-Type': 'text/html'}
    uname = current_user(request)
    u = User.find_by(username=uname)
    if u is None:
        return redirect('/login')
    todo_list = Todo.find_all(user_id=u.id)
    # todo_html = ''.join(['<h3>{} : {}</h3>'.format(t.id, t.title) for t in todo_list])
    todos = []
    for t in todo_list:
        edit_link = '<a href="/todo/edit?id={}">编辑</a>'.format(t.id)
        delete_link = '<a href="/todo/delete?id={}">删除</a>'.format(t.id)
        s = '<h3>{} : {} {} {}</h3>'.format(t.id, t.title, edit_link,
                                            delete_link)
        todos.append(s)
    todo_html = ''.join(todos)
    body = templates('todo_index.html')
    body = body.replace('{{todos}}', todo_html)
    header = response_with_headers(headers)
    r = header + '\r\n' + body
    return r.encode(encoding='utf-8')
Пример #7
0
 def create():
     title = request.form.get('title')
     new_todo = Todo(title=title, complete=False, user_id=current_user.id)
     db.session.add(new_todo)
     db.session.commit()
     return redirect(url_for('web.index'))
Пример #8
0
def add(request):
    form = request.form()
    Todo.new(form)
    return redirect('/todo')
Пример #9
0
def edit(request):
    todo_id = int(request.query.get('id', -1))
    t = Todo.find_by(id=todo_id)
    body = templates('simple_todo_edit.html', todo=t)
    return http_response(body)
Пример #10
0
def index(request):
    todo_list = Todo.all()
    body = templates('simple_todo_index.html', todos=todo_list)
    return http_response(body)
Пример #11
0
def update():
    form = request.json
    todo_id = int(form.get('id', -1))
    if len(form) > 0:
        t = Todo.update(todo_id, form)
        return jsonify(t.json())
Пример #12
0
def add():
    form = request.json
    if len(form['title']) > 0:
        form['user_id'] = current_user().id
        t = Todo.new(form)
        return jsonify(t.json())
Пример #13
0
def all():
    u = current_user()
    todo_list = Todo.find_all(user_id=u.id)
    if todo_list is not None:
        todos = Todo.all_json(todo_list)
        return jsonify(todos)
Пример #14
0
            due_date = request.args.get('due_date')
            return jsonify(todo.api(todo.due_date, date=due_date))
        else:
            return jsonify({"error":"param due_date not provided"})


@ns.route('/over_due')
@ns.response(404, 'Opps! page not found')
class over_due(Resource):
    @ns.doc('over_due')
    def get(self):
        '''Fetch a given resource'''
        return jsonify(todo.api(todo.overdue))


@ns.route('/finished')
@ns.response(404, 'Opps! page not found')
class finished(Resource):
    @ns.doc('finished')
    def get(self):
        '''Fetch a given resource'''
        return jsonify(todo.api(todo.finished))


if __name__ == "__main__":
    todo = Todo()
    user = users()
    app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
    app.run(debug=True)