def add(): """发送新私信""" form = request.form.to_dict() u = current_user() receiver_id = form.get('receiver_id', None) if receiver_id is None: r_name = form.get('receiver_name', None) receiver = User.one(username=r_name) else: # receiver = User.one(id=receiver_id) receiver = cached_user_id2user(receiver_id) if receiver is None: flash('收件人不存在') elif receiver.id == u.id or receiver.username == u.username: flash('私信是发给其他人的哦,请重新输入收件人') else: Messages.send( title=form['title'], content=form['content'], sender_id=u.id, receiver_id=receiver.id, ) flash('发送成功') with data_cache.pipeline(transaction=False) as pipe: key = 'user_id_{}.sent_message'.format(u.id) key2 = 'user_id_{}.received_message'.format(receiver_id) pipe.delete(key) pipe.delete(key2) pipe.execute() return redirect(url_for('.index'))
def send_back(): """用于回复私信""" receiver_id = request.args.get('receiver_id') token = new_csrf_token() u = current_user() receiver = cached_user_id2user(receiver_id) return render_template('mail/reply_page.html', user=u, token=token, receiver=receiver)
def user_created_topics(user_id): """查看用户发表的所有帖子""" u = cached_user_id2user(user_id) if u is None: abort(404) else: topics = cached_created_topics(user_id) topics.sort(key=lambda x: x.last_active_time, reverse=True) return render_template( 'user/user_items.html', itemtype='创建', topics=topics, user=u, )
def set_read(): """标记所有未读私信为已读""" u = current_user() receiver_id = int(request.form.get('receiver_id', -1)) receiver = cached_user_id2user(receiver_id) if receiver is not None: if receiver_id == u.id: unread_messages = Messages.all(receiver_id=receiver_id, been_read=False) with data_cache.pipeline(transaction=False) as pipe: for message in unread_messages: key = 'message_id_{}.message'.format(message.id) message.been_read = True message.save() pipe.delete(key) pipe.execute() return redirect(url_for('.inbox'))
def user_replied_topics(user_id): """查看用户回复过的所有帖子""" u = cached_user_id2user(user_id) if u is None: abort(404) else: # replies = Reply.all(user_id=id) # # topics = [] # if replies: # for rep in replies: # topic = Topic.one(id=rep.topic_id) # if topic not in topics: # topics.append(topic) # topics.sort(key=lambda x: x.last_active_time, reverse=True) topics = cached_replied_topics(user_id) return render_template( 'user/user_items.html', itemtype='参与', topics=topics, user=u, )
def user_detail(user_id): """访问其他用户""" u = cached_user_id2user(user_id) visitor = current_user() if u is None: return abort(404) else: recent_create_topics = Topic.newest_n(5, user_id=user_id) replies = Reply.newest_n(user_id=user_id) recent_rep_topics = [] for rep in replies: topic = cached_topic_id2topic(rep.topic_id) if topic not in recent_rep_topics: recent_rep_topics.append(topic) if len(recent_rep_topics) >= 5: break return render_template( 'user/user_profile.html', user=u, visitor=visitor, recent_rep_topics=recent_rep_topics, recent_create_topics=recent_create_topics, )
def get_receiver(k): """获取某条私信的收件人""" receiver_id = k.receiver_id return cached_user_id2user(receiver_id)
def get_sender(message): """获取某条私信的发件人""" sender_id = message.sender_id return cached_user_id2user(sender_id)