def callback(): '''用户成功登录授权后,会回调此方法,获取access_token,完成授权''' verifier = request.args.get('oauth_verifier', None) # 设置之前保存在session的request_token if 'oauth_request_token' in session: request_token = session['oauth_request_token'] del session['oauth_request_token'] else: return render_template('index.html') auth_client.set_request_token(request_token.key, request_token.secret) access_token = auth_client.get_access_token(verifier) at_key = access_token.key at_secret = access_token.secret #设定用户令牌密钥 auth_client.setToken(at_key, at_secret) #绑定用户验证信息. api = API(auth_client) #获取微博信息 weibo = api.verify_credentials() if weibo is False or weibo is None: flash(u'杯具啊,你木有开通微博吧(⊙﹏⊙)a', 'error') return render_template('index.html') #记录用户登录信息,更新用户微博资料 User.login(weibo=weibo) Weibo.set(weibo=weibo, at_key=at_key, at_secret=at_secret) #设置session session['id'] = weibo.id session.permanent = True # 跳转回最初登录前的页面 back_to_url = session.get('login_back_to_url', '/') return redirect(back_to_url)
def callback(): '''用户成功登录授权后,会回调此方法,获取access_token,完成授权''' verifier = request.args.get('oauth_verifier', None) # 设置之前保存在session的request_token if 'oauth_request_token' in session: request_token = session['oauth_request_token'] del session['oauth_request_token'] else: return render_template('index.html') auth_client.set_request_token(request_token.key, request_token.secret) access_token = auth_client.get_access_token(verifier) at_key = access_token.key at_secret = access_token.secret #设定用户令牌密钥 auth_client.setToken( at_key, at_secret ) #绑定用户验证信息. api = API(auth_client) #获取微博信息 weibo = api.verify_credentials() if weibo is False or weibo is None: flash(u'杯具啊,你木有开通微博吧(⊙﹏⊙)a', 'error') return render_template('index.html') #记录用户登录信息,更新用户微博资料 User.login(weibo=weibo) Weibo.set(weibo=weibo, at_key=at_key, at_secret=at_secret) #设置session session['id'] = weibo.id session.permanent = True # 跳转回最初登录前的页面 back_to_url = session.get('login_back_to_url', '/') return redirect(back_to_url)
def guess(): guessed_name = request.args.get('guessed_name').strip() if guessed_name: guessed_user, fans = None, False try: guessed_user = g.api.get_user(id = guessed_name) except: return render_template('guess.html', error=u"纳尼!查无此人)^O^( 你们真的认识吗") try: fans = g.api.exists_friendship(user_a = guessed_user.id, user_b = g.weibo.id).friends except: return render_template('guess.html', error=u"获取好友关系失败,请稍候再试") else: return render_template('guess.html', error=u"微博昵称不能为空") if guessed_user: constellation_id, rate, error = worker.guess(id=guessed_user.id) if rate <= 30: constellation_id, rate, error = worker.guess(id=guessed_user.id) constellation = constellations[constellation_id][1] if error: return render_template('guess.html', error=error) Guess.set(guesser_id=g.user.id, guessed_id=guessed_user.id, constellation_id=constellation_id, rate=rate) Weibo.set(weibo=guessed_user) guessed_weibo = Weibo.get(id=guessed_user.id)[0] message = weibo_txt(fans=fans, screen_name=guessed_weibo.screen_name, constellation=constellation, rate=rate) rate = str(rate) + "%" return render_template('guess.html',constellation=constellation, rate=rate, message=message, error=None, guessed_weibo=guessed_weibo) else: return render_template('guess.html', error=u'无法获取该用户的微博信息')
def delete(request): weibo_id = int(request.query.get('id', -1)) w = Weibo.find_by(id=weibo_id) u = current_user(request) if w.user_id != u.id: return error(request) Weibo.delete(weibo_id) return redirect('/weibo/detail')
def route_weibo_add(request): username = current_user(request) user = User.find_by(username=username) # 创建微博 form = request.form() w = Weibo(form) w.user_id = user.id w.save() return redirect('/weibo?user_id={}'.format(user.id))
def add(): form = request.form w = Weibo(form) if w.valid(): w.save() else: abort(400) # 蓝图中的 url_for 需要加上蓝图的名字,这里是 todo return redirect(url_for('weibo.index'))
def add(): u = current_user() if u is not None: form = request.form w = Weibo(form) w.user_id = u.id w.save() return redirect(url_for('.timeline_view', username=u.username)) else: abort(401)
def add(): u = current_user() if u is None: return render_template('user_login.html') # abort(404) else: form = request.form w = Weibo(form) w.user_id = u.id w.save() return redirect(url_for('.timeline_view', username=u.username))
def route_weibo_add(request): headers = { 'Content-Type': 'text/html', } username = current_user(request) header = response_with_headers(headers) user = User.find_by(username=username) # 创建微博 form = request.form() w = Weibo(form) w.user_id = user.id w.save() return redirect('/weibo?user_id={}'.format(user.id))
def route_weibo_index(request): headers = { 'Content-Type': 'text/html', } # username = current_user(request) # if username == '游客': # # 没登录 不让看 重定向到 / # return redirect('/login') # else: header = response_with_headers(headers) user_id = request.query.get('user_id', -1) user_id = int(user_id) user = User.find(user_id) if user is None: return error(request) # 找到 user 发布的所有 weibo weibos = Weibo.find_all(user_id=user_id) log('weibos', weibos) def weibo_tag(weibo): return '<p>{} from {}@{} <a href="/weibo/delete?id={}">删除</a></p>'.format( weibo.content, user.username, weibo.created_time, weibo.id, ) weibos = '\n'.join([weibo_tag(w) for w in weibos]) body = template('weibo_index.html', weibos=weibos) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_comment_add(request): form = request.form() c = Comment(form) log('commentAdd c', c) c.save() w = Weibo.find(c.weibo_id) return redirect('/weibo?user_id={}'.format(w.user_id))
def route_comment_add(request): form = request.form() c = Comment(form) c.save() w = Weibo.find(c.weibo_id) # 重定向到用户的主页 return redirect('/weibo?user_id={}'.format(w.user_id))
def edit(request): weibo_id = int(request.query.get('id', -1)) w = Weibo.find_by(id=weibo_id) u = current_user(request) if w.user_id != u.id: return error(request) body = template('weibo_edit.html', weibo=w) return http_response(body)
def route_weibo_index(request): headers = { 'Content-Type': 'text/html', } header = response_with_header(headers) user_id = request.query.get('user_id', -1) user_id = int(user_id) user = User.find(user_id) if user is None: return error(request) # 找到 user 发布的所有 weibo weibos = Weibo.find_all(user_id=user.id) # 任一 user 访问任一 index current_username = current_user(request) u = User.find_by(username=current_username) if u is None: return redirect('/login') def weibo_tag(weibo): comment_list = Comment.find_all(weibo_id=weibo.id) comments = '<br>'.join([c.content for c in comment_list]) # format 函数的字典用法 # 注意 u.id 是 current_user # user.username 是博主 w = { 'id': weibo.id, 'user_id': u.id, 'content': weibo.content, 'username': user.username, 'time': weibo.created_time, 'comments': comments, } # 手动处理 weibos 这个 list # 把每个 weibo 以 <p> 的形式展现在页面 return """ <p>{content} from {username}@{time} <a href="/weibo/delete?id={id}">删除</a> <a href="/weibo/edit?id={id}">修改</a></p> <button class="weibo-show-comment" data-id="{id}">评论</button> <div> {comments} </div> <div id="id-div-comment-{id}" class="weibo-comment-form weibo-hide"> <form action="/weibo/comment/add" method="post"> <input name="user_id" value="{user_id}" type="hidden"> <input name="weibo_id" value="{id}" type="hidden"> <textarea name="content"></textarea> <button type="submit">添加评论</button> </form> </div> </p> """.format(**w) # 用 join() 返回 str weibos = '\n'.join([weibo_tag(w) for w in weibos]) body = template('weibo_index.html', weibos=weibos) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def delete(request): comment_id = int(request.query.get('id', -1)) c = Comment.find_by(id=comment_id) w = Weibo.find_by(id=c.weibo_id) u = current_user(request) if w.user_id != u.id: return error(request) Comment.delete(comment_id) return redirect('/weibo/detail')
def route_comment_add(request): # username = current_user(request) # user = User.find_by(username=username) form = request.form() c = Comment(form) c.save() w = Weibo.find(c.weibo_id) # 重定向到用户的主页 return redirect('/weibo?user_id={}'.format(w.user_id))
def load_weiboes(path='train/train_status.txt'): weiboes = defaultdict(list) with open(path, 'r') as f: for line in f: uid, *rest = line.strip('\n').split(',', 5) wb = Weibo(*rest) weiboes[uid].append(wb) return weiboes
def guess(): guessed_name = request.args.get('guessed_name').strip() if guessed_name: guessed_user, fans = None, False try: guessed_user = g.api.get_user(id=guessed_name) except: return render_template('guess.html', error=u"纳尼!查无此人)^O^( 你们真的认识吗") try: fans = g.api.exists_friendship(user_a=guessed_user.id, user_b=g.weibo.id).friends except: return render_template('guess.html', error=u"获取好友关系失败,请稍候再试") else: return render_template('guess.html', error=u"微博昵称不能为空") if guessed_user: constellation_id, rate, error = worker.guess(id=guessed_user.id) if rate <= 30: constellation_id, rate, error = worker.guess(id=guessed_user.id) constellation = constellations[constellation_id][1] if error: return render_template('guess.html', error=error) Guess.set(guesser_id=g.user.id, guessed_id=guessed_user.id, constellation_id=constellation_id, rate=rate) Weibo.set(weibo=guessed_user) guessed_weibo = Weibo.get(id=guessed_user.id)[0] message = weibo_txt(fans=fans, screen_name=guessed_weibo.screen_name, constellation=constellation, rate=rate) rate = str(rate) + "%" return render_template('guess.html', constellation=constellation, rate=rate, message=message, error=None, guessed_weibo=guessed_weibo) else: return render_template('guess.html', error=u'无法获取该用户的微博信息')
def add(request): u = current_user(request) form = request.form() w = Weibo(form) w.user_id = u.id w.created_time = w.change_time() w.save() return redirect('/weibo')
def before_request(): db_check(db) if 'id' in session: g.user = User.get(id=session['id'])[0] g.weibo = Weibo.get(id=session['id'])[0] else: g.user = None g.weibo = None g.api = None
def index(request): user_id = request.query.get('user_id', -1) user_id = int(user_id) user = User.find(user_id) if user is None: return redirect('/login') # 找到 user 发布的所有 weibo weibos = Weibo.find_all(user_id=user_id) body = template('weibo_index.html', weibos=weibos, user=user) return http_response(body)
def update(request): weibo_id = int(request.query.get('id', -1)) w = Weibo.find_by(id=weibo_id) u = current_user(request) if w.user_id != u.id: return error(request) form = request.form() w.content = form.get('content') w.save() return redirect('/weibo/detail')
def route_weibo_add(request): """ 这个函数相当于一个裸的 API 它提取某个 HTML页面 的数据 处理过后 redirect 到一个页面 """ # headers = { # 'Content-Type': 'text/html', # } username = current_user(request) log('发微博的用户: ', username) # header = response_with_header(headers) user = User.find_by(username=username) # 创建一个新微博实例 # 就是把 weibo_new.html 的数据处理 form = request.form() w = Weibo(form) w.user_id = user.id w.save() return redirect('/weibo?user_id={}'.format(user.id))
def route_weibo_delete(request): username = current_user(request) user = User.find_by(username=username) # 删除微博 weibo_id = request.query.get('id', None) weibo_id = int(weibo_id) w = Weibo.find(weibo_id) if w.user_id == user.id: w.delete() return redirect('/weibo?user_id={}'.format(user.id)) else: return error(request)
def route_weibo_update(request): username = current_user(request) user = User.find_by(username=username) form = request.form() content = form.get('content', '') weibo_id = int(form.get('id', -1)) w = Weibo.find(weibo_id) if user.id != w.user_id: return error(request) w.content = content w.save() return redirect('/weibo?user_id={}'.format(user.id))
def route_weibo_index(request): headers = { 'Content-Type': 'text/html', } header = response_with_headers(headers) user_id = request.query.get('user_id', -1) user_id = int(user_id) user = User.find(user_id) if user is None: return error(request) # 找到 user 发布的所有 weibo weibos = Weibo.find_all(user_id=user_id) log('weibos', weibos) current_username = current_user(request) u = User.find_by(username=current_username) if u is None: return redirect('/login') def weibo_tag(weibo): comment_list = Comment.find_all(weibo_id=weibo.id) comments = '<br>'.join([c.content for c in comment_list]) w = { "id": weibo.id, "user_id": u.id, "content": weibo.content, "username": user.username, "time": weibo.created_time, "comments": comments, } log('comments debug', comment_list) return """ <p>{content} from {username}@{time} <a href="/weibo/delete?id={id}">删除</a> <a href="/weibo/edit?id={id}">修改</a> <button class="gua-show-comment" data-id="{id}">评论</button> <div> {comments} </div> <div id="id-div-comment-{id}" class="gua-comment-form gua-hide"> <form action="/weibo/comment/add" method="post"> <input name="user_id" value="{user_id}" type="hidden"> <input name="weibo_id" value="{id}" type="hidden"> <textarea name="content"></textarea> <button type="submit">添加评论</button> </form> </div> </p> """.format(**w) weibos = '\n'.join([weibo_tag(w) for w in weibos]) body = template('weibo_index.html', weibos=weibos) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def route_weibo_delete(request): headers = { 'Content-Type': 'text/html', } username = current_user(request) header = response_with_headers(headers) user = User.find_by(username=username) # 删除微博 weibo_id = request.query.get('id', None) weibo_id = int(weibo_id) w = Weibo.find(weibo_id) w.delete() return redirect('/weibo?user_id={}'.format(user.id))
def route_weibo_update(request): username = current_user(request) user = User.find_by(username=username) form = request.form() content = form.get('content', '') weibo_id = int(form.get('id', -1)) w = Weibo.find(weibo_id) if user.id != w.user_id: return error(request) w.content = content w.save() # 重定向到用户的主页 return redirect('/weibo?user_id={}'.format(user.id))
def route_weibo_delete(request): # headers = { # 'Content-Type': 'text/html', # } username = current_user(request) # header = response_with_header(headers) user = User.find_by(username=username) # 删除微博 weibo_id = request.query.get('id', None) weibo_id = int(weibo_id) w = Weibo.find(weibo_id) w.delete() return redirect('/weibo?user_id={}'.format(user.id))
def add(): form = request.form u = current_user() t = Weibo(form) t.name = u.username if t.valid(): t.save() return redirect(url_for('.index'))
def add(): form = request.form u = current_user() t = Weibo(form) t.name = u.username r = {'data': []} if t.valid(): t.save() r['success'] = True r['data'] = t.json() else: r['success'] = False message = t.error_message() r['message'] = message return json.dumps(r, ensure_ascii=False)
def route_weibo_edit(request): headers = { 'Content-Type': 'text/html', } header = response_with_headers(headers) weibo_id = request.query.get('id', -1) weibo_id = int(weibo_id) w = Weibo.find(weibo_id) if w is None: return error(request) # 生成一个 edit 页面 body = template('weibo_edit.html', weibo_id=w.id, weibo_content=w.content) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def post(self): user_id = int(self.get_cookie('user_id')) content = self.get_argument('content') # 保存微博数据 session = Session() weibo = Weibo(user_id=user_id, content=content, created=datetime.datetime.now()) session.add(weibo) session.commit() # 创建完成后,跳到显示页面 return self.redirect('/weibo/show?weibo_id=%s' % weibo.id)
def update(weibo_id): form = request.form w = Weibo.query.get(weibo_id) u = current_user() t = Weibo(form) print('debug', w.weibo, '1', t.weibo, '2', t.name, '3', u.username, '4', w.name, '5') r = { 'data': [] } if w.name == u.username: if t.valid(): w.weibo = t.weibo w.save() r['success'] = True r['data'] = t.json() else: r['success'] = False message = t.error_message() r['message'] = message else: r['success'] = False r['message'] = '暗搓搓的改别人微博你这价值观有问题啊' return json.dumps(r, ensure_ascii=False)
def add(): form = request.form u = current_user() t = Weibo(form) t.name = u.username if t.valid(): t.save() # 蓝图中的 url_for 需要加上蓝图的名字,这里是 todo return redirect(url_for('.index'))
def route_weibo_edit(request): headers = { 'Content-Type': 'text/html', } header = response_with_header(headers) # 这个 query.get 是在 weibo_index路由 里面放上去的 # 用来指定要修改那一条微博 weibo_id = request.query.get('id', -1) weibo_id = int(weibo_id) w = Weibo.find(weibo_id) if w is None: return error(request) # 生成一个 edit 页面 body = template('weibo_edit.html', weibo_id=w.id, weibo_content=w.content) r = header + '\r\n' + body return r.encode(encoding='utf-8')
def index(): u = current_user() # 这里是游客查看全部weibo if u is None: # 这里是全部用户的索引 user_list = User.query.all() all_weibo = Weibo.query.filter_by(hidden=0).all() fix_all_weibo = User.change_uid_to_uname(User, all_weibo) # fix_all_weibo = copy.deepcopy(all_weibo) # for ws in fix_all_weibo: # wu = User.query.filter_by(id=ws.user_id).first() # ws.user_id = wu.username fix_ws_comment = Weibo.inser_comment_to_weibo(Weibo, fix_all_weibo) return render_template('timeline.html', users=user_list, weibos=fix_ws_comment) else: # return redirect('/weibo/timeline/{}'.format(u.username)) # 上面是直接写网址,下面是url_for带参数的写法 return redirect(url_for('.timeline_view', username=u.username))
def timeline_view(username): u = User.query.filter_by(username=username).first() if u is None: # abort(404) return '查无此人' else: # 这里是全部用户的索引 us = User.query.all() # 这里是自己查看自己的weibo # ws = Weibo.query.filter_by(user_id=u.id).all() ws = u.weibos() # 这里把ID显示成用户名 fix_ws = User.change_uid_to_uname(User, ws) # fix_ws = copy.deepcopy(ws) # for fws in fix_ws: # wu = User.query.filter_by(id=fws.user_id).first() # fws.user_id = wu.username # 下面是把当前微博的comment_list动态添加进每个weibo字典里 # fws.cs = Comment.query.filter_by(weibo_id=fws.id).all() (旧)下面是抽出来放到weibo model里了 fix_ws_comment = Weibo.inser_comment_to_weibo(Weibo, fix_ws) return render_template('timeline.html', users=us, weibos=fix_ws_comment)