allow html
Browse files
admin.py
CHANGED
|
@@ -1,124 +1,124 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import streamlit as st
|
| 3 |
-
from mongodb import add_post,get_post_titles, get_post_by_id, update_post, delete_post
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
-
load_dotenv()
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
USERNAME = os.getenv("USERNAME_")
|
| 9 |
-
PASSWORD = os.getenv("PASSWORD_")
|
| 10 |
-
|
| 11 |
-
def admin():
|
| 12 |
-
|
| 13 |
-
# Create a placeholder for the password input field
|
| 14 |
-
user_placeholder = st.empty()
|
| 15 |
-
password_placeholder = st.empty()
|
| 16 |
-
|
| 17 |
-
# Show password input if it's not already correct
|
| 18 |
-
username = user_placeholder.text_input("Enter Username")
|
| 19 |
-
password = password_placeholder.text_input("Enter Password", type="password")
|
| 20 |
-
|
| 21 |
-
# Check if the password is correct
|
| 22 |
-
if password == PASSWORD and username == USERNAME:
|
| 23 |
-
password_placeholder.empty() # Hide the password input
|
| 24 |
-
user_placeholder.empty() # Hide the password input
|
| 25 |
-
|
| 26 |
-
tab1, tab2, tab3 = st.tabs(["Read Post", "Add Post", "Update & Delete Post"])
|
| 27 |
-
|
| 28 |
-
with tab1:
|
| 29 |
-
if st.session_state.get('selected_post_id', None):
|
| 30 |
-
selected_post = get_post_by_id(st.session_state.selected_post_id)
|
| 31 |
-
|
| 32 |
-
# Display the content of the selected post
|
| 33 |
-
st.subheader(selected_post["title"])
|
| 34 |
-
st.divider()
|
| 35 |
-
st.markdown(selected_post["content"]) # Render the content as Markdown
|
| 36 |
-
|
| 37 |
-
with tab2:
|
| 38 |
-
st.subheader("Add a new Post")
|
| 39 |
-
title = st.text_input("Post Title")
|
| 40 |
-
|
| 41 |
-
# Text input area for Markdown content
|
| 42 |
-
content = st.text_area("Post Content (Markdown supported)", height=800)
|
| 43 |
-
|
| 44 |
-
status = st.selectbox("Status", ["private", "public", "achieve"], index=0)
|
| 45 |
-
|
| 46 |
-
if st.button("Add Post"):
|
| 47 |
-
if title and content:
|
| 48 |
-
res = add_post(title, content, status, username)
|
| 49 |
-
st.write(res)
|
| 50 |
-
else:
|
| 51 |
-
st.error("Please fill both the title and content.")
|
| 52 |
-
|
| 53 |
-
with tab3:
|
| 54 |
-
|
| 55 |
-
show_status = st.sidebar.selectbox("Status", ["private", "public", "achieve"], index = 1)
|
| 56 |
-
post_titles = list(get_post_titles(show_status))
|
| 57 |
-
|
| 58 |
-
if post_titles:
|
| 59 |
-
# Use session state to remember the selected post ID
|
| 60 |
-
if "selected_post_id" not in st.session_state:
|
| 61 |
-
st.session_state.selected_post_id = None
|
| 62 |
-
|
| 63 |
-
# Display titles as clickable buttons in the sidebar
|
| 64 |
-
selected_post = None
|
| 65 |
-
for post in post_titles:
|
| 66 |
-
# Check if the post is selected
|
| 67 |
-
if st.sidebar.button(post["title"], key=f"button_{post['_id']}"):
|
| 68 |
-
st.session_state.selected_post_id = post["_id"]
|
| 69 |
-
|
| 70 |
-
# Only show the update section if a post is selected
|
| 71 |
-
if st.session_state.selected_post_id:
|
| 72 |
-
post_id = st.session_state.selected_post_id
|
| 73 |
-
selected_post = get_post_by_id(post_id)
|
| 74 |
-
|
| 75 |
-
if selected_post:
|
| 76 |
-
# Display the content of the selected post
|
| 77 |
-
new_title = st.text_input("New Title", value=selected_post["title"])
|
| 78 |
-
new_content = st.text_area("New Content (Markdown supported)", value=selected_post["content"],
|
| 79 |
-
height=800)
|
| 80 |
-
|
| 81 |
-
options = ["private", "public", "achieve"]
|
| 82 |
-
new_status = st.selectbox("Status", options, index= options.index(selected_post['status']), key="status_update")
|
| 83 |
-
left, right = st.columns(2)
|
| 84 |
-
|
| 85 |
-
if left.button("Update Post", use_container_width=True, key=f"update_button_{post_id}"):
|
| 86 |
-
if new_title and new_content:
|
| 87 |
-
# Call the update_post function to save the changes to MongoDB
|
| 88 |
-
res = update_post(post_id, new_title, new_content, new_status)
|
| 89 |
-
st.success(res) # Display the result from the update function
|
| 90 |
-
# Optionally, display updated content for the user to confirm the changes
|
| 91 |
-
else:
|
| 92 |
-
st.error("Please fill both the new title and content.")
|
| 93 |
-
|
| 94 |
-
# Permanent delete logic
|
| 95 |
-
delete_password = st.text_input("Password", type="password", key=f"delete_password_{post_id}")
|
| 96 |
-
delete_button = right.button("Permanent Delete Post", use_container_width=True,
|
| 97 |
-
key=f"delete_button_{post_id}")
|
| 98 |
-
|
| 99 |
-
if delete_button:
|
| 100 |
-
if delete_password == PASSWORD:
|
| 101 |
-
res = delete_post(post_id)
|
| 102 |
-
if res:
|
| 103 |
-
st.success("Post deleted successfully!")
|
| 104 |
-
st.session_state.selected_post_id = None # Reset selected post after deletion
|
| 105 |
-
post_titles = list(get_post_titles(show_status)) # Refresh the list of posts
|
| 106 |
-
else:
|
| 107 |
-
st.error("An error occurred while deleting the post.")
|
| 108 |
-
elif delete_password: # If password is filled but incorrect
|
| 109 |
-
st.error("Incorrect password.")
|
| 110 |
-
else:
|
| 111 |
-
st.error("Post not found.")
|
| 112 |
-
else:
|
| 113 |
-
st.error("Please select a post first.")
|
| 114 |
-
|
| 115 |
-
else:
|
| 116 |
-
st.write("No posts available.")
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
else:
|
| 120 |
-
if password: # If password is entered but incorrect
|
| 121 |
-
st.error("Incorrect password. Please try again.")
|
| 122 |
-
else: # If no password is entered
|
| 123 |
-
st.info("Please enter your Username & Password.")
|
| 124 |
-
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from mongodb import add_post,get_post_titles, get_post_by_id, update_post, delete_post
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
load_dotenv()
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
USERNAME = os.getenv("USERNAME_")
|
| 9 |
+
PASSWORD = os.getenv("PASSWORD_")
|
| 10 |
+
|
| 11 |
+
def admin():
|
| 12 |
+
|
| 13 |
+
# Create a placeholder for the password input field
|
| 14 |
+
user_placeholder = st.empty()
|
| 15 |
+
password_placeholder = st.empty()
|
| 16 |
+
|
| 17 |
+
# Show password input if it's not already correct
|
| 18 |
+
username = user_placeholder.text_input("Enter Username")
|
| 19 |
+
password = password_placeholder.text_input("Enter Password", type="password")
|
| 20 |
+
|
| 21 |
+
# Check if the password is correct
|
| 22 |
+
if password == PASSWORD and username == USERNAME:
|
| 23 |
+
password_placeholder.empty() # Hide the password input
|
| 24 |
+
user_placeholder.empty() # Hide the password input
|
| 25 |
+
|
| 26 |
+
tab1, tab2, tab3 = st.tabs(["Read Post", "Add Post", "Update & Delete Post"])
|
| 27 |
+
|
| 28 |
+
with tab1:
|
| 29 |
+
if st.session_state.get('selected_post_id', None):
|
| 30 |
+
selected_post = get_post_by_id(st.session_state.selected_post_id)
|
| 31 |
+
|
| 32 |
+
# Display the content of the selected post
|
| 33 |
+
st.subheader(selected_post["title"])
|
| 34 |
+
st.divider()
|
| 35 |
+
st.markdown(selected_post["content"], unsafe_allow_html =True) # Render the content as Markdown
|
| 36 |
+
|
| 37 |
+
with tab2:
|
| 38 |
+
st.subheader("Add a new Post")
|
| 39 |
+
title = st.text_input("Post Title")
|
| 40 |
+
|
| 41 |
+
# Text input area for Markdown content
|
| 42 |
+
content = st.text_area("Post Content (Markdown supported)", height=800)
|
| 43 |
+
|
| 44 |
+
status = st.selectbox("Status", ["private", "public", "achieve"], index=0)
|
| 45 |
+
|
| 46 |
+
if st.button("Add Post"):
|
| 47 |
+
if title and content:
|
| 48 |
+
res = add_post(title, content, status, username)
|
| 49 |
+
st.write(res)
|
| 50 |
+
else:
|
| 51 |
+
st.error("Please fill both the title and content.")
|
| 52 |
+
|
| 53 |
+
with tab3:
|
| 54 |
+
|
| 55 |
+
show_status = st.sidebar.selectbox("Status", ["private", "public", "achieve"], index = 1)
|
| 56 |
+
post_titles = list(get_post_titles(show_status))
|
| 57 |
+
|
| 58 |
+
if post_titles:
|
| 59 |
+
# Use session state to remember the selected post ID
|
| 60 |
+
if "selected_post_id" not in st.session_state:
|
| 61 |
+
st.session_state.selected_post_id = None
|
| 62 |
+
|
| 63 |
+
# Display titles as clickable buttons in the sidebar
|
| 64 |
+
selected_post = None
|
| 65 |
+
for post in post_titles:
|
| 66 |
+
# Check if the post is selected
|
| 67 |
+
if st.sidebar.button(post["title"], key=f"button_{post['_id']}"):
|
| 68 |
+
st.session_state.selected_post_id = post["_id"]
|
| 69 |
+
|
| 70 |
+
# Only show the update section if a post is selected
|
| 71 |
+
if st.session_state.selected_post_id:
|
| 72 |
+
post_id = st.session_state.selected_post_id
|
| 73 |
+
selected_post = get_post_by_id(post_id)
|
| 74 |
+
|
| 75 |
+
if selected_post:
|
| 76 |
+
# Display the content of the selected post
|
| 77 |
+
new_title = st.text_input("New Title", value=selected_post["title"])
|
| 78 |
+
new_content = st.text_area("New Content (Markdown supported)", value=selected_post["content"],
|
| 79 |
+
height=800)
|
| 80 |
+
|
| 81 |
+
options = ["private", "public", "achieve"]
|
| 82 |
+
new_status = st.selectbox("Status", options, index= options.index(selected_post['status']), key="status_update")
|
| 83 |
+
left, right = st.columns(2)
|
| 84 |
+
|
| 85 |
+
if left.button("Update Post", use_container_width=True, key=f"update_button_{post_id}"):
|
| 86 |
+
if new_title and new_content:
|
| 87 |
+
# Call the update_post function to save the changes to MongoDB
|
| 88 |
+
res = update_post(post_id, new_title, new_content, new_status)
|
| 89 |
+
st.success(res) # Display the result from the update function
|
| 90 |
+
# Optionally, display updated content for the user to confirm the changes
|
| 91 |
+
else:
|
| 92 |
+
st.error("Please fill both the new title and content.")
|
| 93 |
+
|
| 94 |
+
# Permanent delete logic
|
| 95 |
+
delete_password = st.text_input("Password", type="password", key=f"delete_password_{post_id}")
|
| 96 |
+
delete_button = right.button("Permanent Delete Post", use_container_width=True,
|
| 97 |
+
key=f"delete_button_{post_id}")
|
| 98 |
+
|
| 99 |
+
if delete_button:
|
| 100 |
+
if delete_password == PASSWORD:
|
| 101 |
+
res = delete_post(post_id)
|
| 102 |
+
if res:
|
| 103 |
+
st.success("Post deleted successfully!")
|
| 104 |
+
st.session_state.selected_post_id = None # Reset selected post after deletion
|
| 105 |
+
post_titles = list(get_post_titles(show_status)) # Refresh the list of posts
|
| 106 |
+
else:
|
| 107 |
+
st.error("An error occurred while deleting the post.")
|
| 108 |
+
elif delete_password: # If password is filled but incorrect
|
| 109 |
+
st.error("Incorrect password.")
|
| 110 |
+
else:
|
| 111 |
+
st.error("Post not found.")
|
| 112 |
+
else:
|
| 113 |
+
st.error("Please select a post first.")
|
| 114 |
+
|
| 115 |
+
else:
|
| 116 |
+
st.write("No posts available.")
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
else:
|
| 120 |
+
if password: # If password is entered but incorrect
|
| 121 |
+
st.error("Incorrect password. Please try again.")
|
| 122 |
+
else: # If no password is entered
|
| 123 |
+
st.info("Please enter your Username & Password.")
|
| 124 |
+
|