def comment_update(request): """ 用于增加新 weibo 的路由函数 """ form = request.form() comment_id = int(form['comment_id']) Comment.update(comment_id, content=form['content']) # 浏览器发送数据过来被处理后, 重定向到首页 # 浏览器在请求新首页的时候, 就能看到新增的数据了 return redirect('/weibo/index')
def delete(request): weibo_id = int(request.query['weibo_id']) Weibo.delete(weibo_id) cs = Comment.all(weibo_id=weibo_id) for c in cs: c.delete() return redirect('/weibo/index')
def f(request): log('comment_or_weibo_owner_required') if request.method == 'GET': data = request.query elif request.method == 'POST': data = request.form() else: raise ValueError('不支持的方法', request.method) comment_key = 'comment_id' weibo_key = 'weibo_id' if comment_key in data: c = Comment.one(id=int(data[comment_key])) if c is None: return redirect('/weibo/index') else: user_id = c.user_id elif weibo_key in data: w = Weibo.one(id=int(data[weibo_key])) if w is None: return redirect('/weibo/index') else: user_id = w.user_id else: raise ValueError('不支持的参数', data) u = current_user(request) if user_id == u.id: log('不是评论或者微博的作者', user_id, u.id) return route_function(request) else: return redirect('/weibo/index')
def delete(request): weibo_id = int(request.query['weibo_id']) print('shuchu') Weibo.delete(weibo_id) cs = Comment.all(weibo_id=int(weibo_id)) for c in cs: c.delete(c.id) return redirect('/weibo/index')
def f(request): log('comment_owner_required') u = current_user(request) id_key = 'comment_id' if id_key in request.query: comment_id = request.query[id_key] else: comment_id = request.form()[id_key] c = Comment.one(id=int(comment_id)) if c.user_id == u.id: log('不是评论作者', c) return route_function(request) else: return redirect('/weibo/index')
def comments(self): cs = Comment.all(weibo_id=self.id) return cs
def comment_edit(request): comment_id = int(request.query['comment_id']) c = Comment.one(id=comment_id) return html_response('comment_edit.html', comment=c)
def comment_delete(request): comment_id = int(request.query['comment_id']) Comment.delete(comment_id) return redirect('/weibo/index')
def comment_add(request): u = current_user(request) form = request.form() weibo_id = int(form['weibo_id']) Comment.add(form, u.id, weibo_id) return redirect('/weibo/index')
def test(): connection = pymysql.connect( host='localhost', user='******', password='******'.format(secret.database_password), # db='web8', charset='utf8mb4', cursorclass=pymysql.cursors.DictCursor) with connection.cursor() as cursor: cursor.execute('DROP DATABASE IF EXISTS`web`') cursor.execute('CREATE DATABASE `web` CHARACTER SET utf8mb4') cursor.execute('USE `web`') cursor.execute(User.sql_create) cursor.execute(Session.sql_create) cursor.execute(Todo.sql_create) cursor.execute(Weibo.sql_create) cursor.execute(Comment.sql_create) connection.commit() connection.close() form = dict( username='******', password='******', ) User.register_user(form) u, result = User.login_user(form) assert u is not None, result form = dict( username='******', password='******', ) User.register_user(form) session_id = random_string() form = dict( session_id=session_id, user_id=u.id, ) Session.new(form) s: Session = Session.one(session_id=session_id) assert s.session_id == session_id form = dict( title='test todo', user_id=u.id, ) t = Todo.add(form, u.id) assert t.title == 'test todo' form = dict( content='test weibo', user_id=u.id, ) w = Weibo.add(form, u.id) assert w.content == 'test weibo' form = dict( content='test comment', user_id=u.id, ) c = Comment.add(form, u.id, w.id) assert c.content == 'test comment'