def user_detail(id): # u = User.find_by(id=id) u = User.one(id=id) if u is None: abort(404) else: return render_template('profile.html', user=u)
def current_user(): # 从 session 中找到 user_id 字段, 找不到就 -1 # 然后 User.find_by 来用 id 找用户 # 找不到就返回 None uid = session.get('user_id', -1) # u = User.find_by(id=uid) u = User.one(id=uid) return u
def current_user(): # 从 session 中找到 user_id 字段, 找不到就 -1 # 然后 User.find_by 来用 id 找用户 # 找不到就返回 None if 'user_id' in session: uid = int(session['user_id']) e = User.exist(id=uid) # e = User.find_by(id=uid) if e: # return e return User.one(id=uid) else: return None else: return None
def user_profile(username): # u = User.find_by(username=username) u = User.one(username=username) if u is None: abort(404) else: # topics = Topic.find_all(user_id=u.id) topics = Topic.all(user_id=u.id) reply_topic = Topic.user_participated(u) return render_template('user.html', user=u, reply_topic=reply_topic, topics=topics, hots=Topic.hots(), u=current_user())
def add_mail(self): receivers = [] content = '' for tag in self.content.split(' '): if tag.startswith('@'): receivers.append(tag[1:]) else: content += tag for r in receivers: if User.exist(username=r): receiver = User.one(username=r) form = dict(title='你被{} @ 了'.format(self.user().username), content='<a href="/topic/{}">{} </a>查看'.format( self.topic_id, content), sender_id=self.user_id, receiver_id=receiver.id) Mail.new(**form)
def user(self): u = User.one(id=self.user_id) return u
def receiver(self): if User.exist(id=self.receiver_id): u = User.one(id=self.receiver_id) return u.username return 'Anoymous'
def sender(self): if User.exist(id=self.sender_id): u = User.one(id=self.sender_id) return u.username return 'Anoymous'