def add(request): """ 用于增加新 weibo 的路由函数 """ u = current_user(request) form = request.form() Weibo.add(form, u.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'