def edit_todo(request): if request.Method == 'GET': todo_id = request.query.get('id', '') if not todo_id: return handle_404() todo_id = int(todo_id) #根据前台get请求中的todo_id查找todo的条目 t = Todo.find_by_id(todo_id) if not t: return handle_404() header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection:Close\r\n' body = template( 'todo_update.html', todo_id=t.id, todo_content=t.content.encode('utf8'), ) r = header + '\r\n' + body return r if request.Method == 'POST': todo_id = int(request.form().get('id', '')) todo_content = request.form().get('content', '') t = Todo.find_by_id(todo_id) t.content = todo_content t.save() return redirect('/todo')
def add_todo(request): if request.Method == 'POST': form = request.form() user = current_user(request) if not user: return redirect('/404') form['user_id'] = user.id todo = Todo(form) if form.get('content', ''): print '我不是空的!' todo.save() return redirect('/todo')
def get(self, todo_id): todo = Todo.get_by_pk(todo_id) if todo: todo = todo.to_dict() todo['content'] = markdown.render(todo['content']) self.finish({'code': 0, 'data': todo}) else: self.finish({'code': -1})
def todo_route(request): if request.Method == 'GET': user = current_user(request) todo_models = Todo.find(user_id=user.id) header = 'HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection:Close\r\n' body = template('todo.html', todo_models=todo_models) r = header + '\r\n' + body return r
def post(self): title = self.get_argument('title', '').strip() content = self.get_argument('content', '').strip() if title and config.TITLE_LENGTH_MIN <= len( title) <= config.TITLE_LENGTH_MAX: t = Todo.new(title, self.current_user() or 0, content) self.finish({'code': 0, 'todo': t.to_dict()}) else: # 非标准提交 self.finish({'code': -1})
def post(self): todo_text = self.get_argument('todo_lst', '[]') todo_lst = json.loads(todo_text) user = self.current_user() mods = [] for i in todo_lst: t = Todo.get_by_pk(i['id']) #if t.can_edit(user): # 姑且不对权限做限制 if t.edit(i, user): mods.append(t.to_dict()) self.finish({'code': 0, 'modified': mods})
def post(self): todo_id = self.get_argument('todo_id') if todo_id: todo = Todo.get_by_pk(todo_id) if todo and todo.can_edit(self.current_user()): todo.remove() self.finish({'code': 0}) else: self.finish({'code': -2}) else: self.finish({'code': -1})
def create_todo(): try: data = request.get_json() print(data.get("dueDate")) todo = Todo(name=data.get("name"), description=data.get("description"), due_date=datetime.strptime(data.get("dueDate"), "%Y-%m-%d"), category=data.get("category"), status="in-progress", created_at=datetime.now()) db.session.add(todo) db.session.commit() return jsonify({ "success": True, "todo": todo.to_dict(), "msg": "Added new todo successfully" }), 200 except Exception as e: return jsonify({"Success": False, "msg": str(e)}), 400
def delete_todo(request): if request.Method == 'GET': user = current_user(request) todo_id = request.query.get('id', '') if not todo_id: return handle_404() t = Todo.find_by_id(int(todo_id)) if not t: return handle_404() if t.user_id != user.id: return handle_404() t.remove() return redirect('/todo')
def put(self): todo = Todo(text=request.form['data']) todo.save() return {'status': 'success'}
def delete_method(id): Todo.delete_one(id) return Response(json.dumps({"result": "ok"}), mimetype='application/json')
def update_method(): data = request.get_json(silent=True) Todo.update_one(data.get("id"), data.get("done")) return Response(json.dumps({"result": "ok"}), mimetype='application/json')
def post_method(): data = request.get_json(silent=True) obj = Todo(data.get("task"), data.get("author")) obj.save_to_db() return Response(json.dumps({"result": "ok"}), mimetype='application/json')
def get(self): page = self.get_argument('p', '1') count, query = Todo.get_list() self.finish({'code': 0, 'data': list(map(Todo.to_dict, query))})
from database import db from model.todo import Todo db.Database.initialize() # let try insert new record to db new_todo = Todo.get_all() print(new_todo)
def getall_method(): res = Todo.get_all() for r in res: if "_id" in r: del r["_id"] return Response(json.dumps(res), mimetype='application/json')