def room(room_id): room_online_user_channel = app.config["ROOM_ONLINE_USER_CHANNEL"].format( room=room_id) room_content_channel = app.config["ROOM_CONTENT_CHANNEL"].format( room=room_id) room_info_key = app.config["ROOM_INFO_KEY"].format(room=room_id) if session["room"] is not None: session["room"] = None rc.zrem(room_online_user_channel, current_user.username) session["room"] = str(room_id) rc.zadd(room_online_user_channel, current_user.username, time.time()) room_info = json.loads(rc.get(room_info_key)) room_content = reversed( rc.zrevrange(room_content_channel, 0, 200, withscores=True)) room_content_list = [] for item in room_content: room_content_list.append(json.loads(item[0])) room_online_users = [] for user in rc.zrange(room_online_user_channel, 0, -1): room_online_users.append(user) return render_template("room.html", room_id=room_id, room_info=room_info, users=room_online_users, user_name=current_user.username, messages=room_content_list)
def index(): session["room"] = None form = RoomCreateForm() if form.validate_on_submit(): room_id = rc.incr(app.config["ROOM_INCR_KEY"]) rc.set( app.config["ROOM_INFO_KEY"].format(room=room_id), json.dumps({ "title": form.title.data, "room_id": room_id, "creator": current_user.username, "created": datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Y") })) flash("New room has already been created successfully!") return redirect("/room/" + str(room_id)) rooms = [] room_info_keys = app.config["ROOM_INFO_KEY"].format(room='*') for room_info_key in rc.keys(room_info_keys): room_info = json.loads(rc.get(room_info_key)) rooms.append({ "id": room_info["room_id"], "creator": room_info["creator"], "title": room_info["title"], "time": room_info["created"] }) return render_template("index.html", form=form, rooms=rooms)
def room(room_id): room_online_user_channel = app.config["ROOM_ONLINE_USER_CHANNEL"].format(room=room_id) room_content_channel = app.config["ROOM_CONTENT_CHANNEL"].format(room=room_id) room_info_key = app.config["ROOM_INFO_KEY"].format(room=room_id) if session["room"] is not None: session["room"] = None rc.zrem(room_online_user_channel, current_user.username) session["room"] = str(room_id) rc.zadd(room_online_user_channel, current_user.username, time.time()) room_info = json.loads(rc.get(room_info_key)) room_content = reversed(rc.zrevrange(room_content_channel, 0, 200, withscores=True)) room_content_list = [] for item in room_content: room_content_list.append(json.loads(item[0])) room_online_users =[] for user in rc.zrange(room_online_user_channel, 0, -1): room_online_users.append(user) return render_template("room.html", room_id=room_id, room_info=room_info, users=room_online_users, user_name=current_user.username, messages=room_content_list)
def rm_room(room_id): room_info_key = app.config["ROOM_INFO_KEY"].format(room=room_id) room_info = json.loads(rc.get(room_info_key)) if room_info["creator"] != current_user.username: flash("You are not the creator of this room!") return redirect(url_for("index")) room_content = app.config["ROOM_CONTENT_CHANNEL"].format(room=room_id) room_online_user_channel = app.config["ROOM_ONLINE_USER_CHANNEL"].format(room=room_id) rc.delete(room_info_key) rc.delete(room_content) rc.delete(room_online_user_channel) flash("The room "+str(room_id)+" has been deleted.") return redirect(url_for("index"))
def rm_room(room_id): room_info_key = app.config["ROOM_INFO_KEY"].format(room=room_id) room_info = json.loads(rc.get(room_info_key)) if room_info["creator"] != current_user.username: flash("You are not the creator of this room!") return redirect(url_for("index")) room_content = app.config["ROOM_CONTENT_CHANNEL"].format(room=room_id) room_online_user_channel = app.config["ROOM_ONLINE_USER_CHANNEL"].format( room=room_id) rc.delete(room_info_key) rc.delete(room_content) rc.delete(room_online_user_channel) flash("The room " + str(room_id) + " has been deleted.") return redirect(url_for("index"))
def index(): session["room"] = None form = RoomCreateForm() if form.validate_on_submit(): room_id = rc.incr(app.config["ROOM_INCR_KEY"]) rc.set(app.config["ROOM_INFO_KEY"].format(room=room_id), json.dumps({"title": form.title.data, "room_id": room_id, "creator": current_user.username, "created": datetime.datetime.now().strftime("%a %b %d %H:%M:%S %Y") })) flash("New room has already been created successfully!") return redirect("/room/"+str(room_id)) rooms = [] room_info_keys = app.config["ROOM_INFO_KEY"].format(room='*') for room_info_key in rc.keys(room_info_keys): room_info = json.loads(rc.get(room_info_key)) rooms.append({ "id": room_info["room_id"], "creator": room_info["creator"], "title": room_info["title"], "time": room_info["created"] }) return render_template("index.html", form=form, rooms=rooms)