def edit_room(room_id): room = get_room(room_id) if room and is_room_admin(room_id, current_user.username): existing_room_members = [ member['_id']['username'] for member in get_room_members(room_id) ] room_members_str = ",".join(existing_room_members) message = '' if request.method == 'POST': room_name = request.form.get('room_name') room['name'] = room_name update_room(room_id, room_name) new_members = [ username.strip() for username in request.form.get('members').split(',') ] members_to_add = list( set(new_members) - set(existing_room_members)) members_to_remove = list( set(existing_room_members) - set(new_members)) if len(members_to_add): add_room_members(room_id, room_name, members_to_add, current_user.username) if len(members_to_remove): remove_room_members(room_id, members_to_remove) message = 'Room edited successfully' room_members_str = ",".join(new_members) return render_template('edit_room.html', room=room, room_members_str=room_members_str, message=message) else: return "Room not found", 404
def create_room(): message = '' if request.method == 'POST': room_name = request.form.get('room_name') usernames = [ username.strip() for username in request.form.get('members').split(',') ] if len(room_name) and len(usernames): room_id = save_room(room_name, current_user.username) if current_user.username in usernames: usernames.remove(current_user.username) add_room_members(room_id, room_name, usernames, current_user.username) return redirect(url_for('view_room', room_id=room_id)) else: message = "Failed to create room" return render_template('create_room.html', message=message)