def message_action(): user = session.get('user_info') if request.form['btn'] is None: util.set_message('参数错误') return redirect(url_for('messagebox')) msgs_key = 'user_messages:%s' % user.get('username') all_message = r_session.lrange(msgs_key, 0, -1) for val in request.form: if len(val) < 4 or val[0:3] != 'msg': continue msg_id = val[4:] if bytes(msg_id, 'utf-8') not in all_message: continue if request.form['btn'] == 'mark_as_read': msg_key = 'user_message:%s' % msg_id msg = json.loads(r_session.get(msg_key).decode('utf-8')) msg['is_read'] = True r_session.set(msg_key, json.dumps(msg)) elif request.form['btn'] == 'delete': r_session.lrem(msgs_key, msg_id) msg_key = 'user_message:%s' % msg_id r_session.delete(msg_key) return redirect(url_for('messagebox'))
def message_box(): if session is None or session.get('user_info') is None: return dict() user = session.get('user_info') msgs_key = 'user_messages:%s' % user.get('username') msg_box = list() msg_count = 0 for b_msg_id in r_session.lrange(msgs_key, 0, -1): msg_key = 'user_message:%s' % b_msg_id.decode('utf-8') b_msg = r_session.get(msg_key) if b_msg is None: r_session.lrem(msgs_key, msg_key) continue msg = json.loads(b_msg.decode('utf-8')) if msg.get('is_read'): continue if len(msg.get('content')) > 41: msg['content'] = msg.get('content')[:30] + '...' else: msg['content'] = msg.get('content')[:30] msg_count += 1 if not len(msg_box) > 3: msg_box.append(msg) return dict(msg_box=msg_box, msg_count=msg_count)
def messagebox(): user = session.get('user_info') err_msg = None if session.get('error_message') is not None: err_msg = session.get('error_message') session['error_message'] = None info_msg = None if session.get('info_message') is not None: info_msg = session.get('info_message') session['info_message'] = None msgs_key = 'user_messages:%s' % user.get('username') msg_box = list() show_read_all = False for b_msg_id in r_session.lrange(msgs_key, 0, -1): msg_key = 'user_message:%s' % b_msg_id.decode('utf-8') b_msg = r_session.get(msg_key) if b_msg is None: r_session.lrem(msgs_key, msg_key) continue msg = json.loads(b_msg.decode('utf-8')) if show_read_all or not msg.get('is_read'): show_read_all = True msg_box.append(msg) return render_template('messages.html', err_msg=err_msg, info_msg=info_msg, messages=msg_box, show_read_all=show_read_all)
def message_box(): if session is None or session.get("user_info") is None: return dict() user = session.get("user_info") msgs_key = "user_messages:%s" % user.get("username") msg_box = list() msg_count = 0 for b_msg_id in r_session.lrange(msgs_key, 0, -1): msg_key = "user_message:%s" % b_msg_id.decode("utf-8") b_msg = r_session.get(msg_key) if b_msg is None: r_session.lrem(msgs_key, msg_key) continue msg = json.loads(b_msg.decode("utf-8")) if msg.get("is_read"): continue if len(msg.get("content")) > 41: msg["content"] = msg.get("content")[:30] + "..." else: msg["content"] = msg.get("content")[:30] msg_count += 1 if not len(msg_box) > 3: msg_box.append(msg) return dict(msg_box=msg_box, msg_count=msg_count)
def get_comments(): r_session.ltrim('comments_list',0,10000) comments_list=r_session.lrange('comments_list',0,100) recent_chat=[] for i in range(0,len(comments_list)): if comments_list[i] is None: break chat=json.loads(comments_list[i].decode('utf-8')) recent_chat.append(chat) return Response(json.dumps(dict(messages=recent_chat)), mimetype='application/json')
def get_comments(): r_session.ltrim('comments_list', 0, 10000) comments_list = r_session.lrange('comments_list', 0, 100) recent_chat = [] for i in range(0, len(comments_list)): if comments_list[i] is None: break chat = json.loads(comments_list[i].decode('utf-8')) recent_chat.append(chat) return Response(json.dumps(dict(messages=recent_chat)), mimetype='application/json')