|
import os |
|
import streamlit as st |
|
from mongodb import add_post,get_post_titles, get_post_by_id, update_post, delete_post |
|
from dotenv import load_dotenv |
|
load_dotenv() |
|
|
|
|
|
USERNAME = os.getenv("USERNAME_") |
|
PASSWORD = os.getenv("PASSWORD_") |
|
|
|
def admin(): |
|
|
|
|
|
user_placeholder = st.empty() |
|
password_placeholder = st.empty() |
|
|
|
|
|
username = user_placeholder.text_input("Enter Username") |
|
password = password_placeholder.text_input("Enter Password", type="password") |
|
|
|
|
|
if password == PASSWORD and username == USERNAME: |
|
password_placeholder.empty() |
|
user_placeholder.empty() |
|
|
|
tab1, tab2, tab3 = st.tabs(["Read Post", "Add Post", "Update & Delete Post"]) |
|
|
|
with tab1: |
|
if st.session_state.get('selected_post_id', None): |
|
selected_post = get_post_by_id(st.session_state.selected_post_id) |
|
|
|
|
|
st.subheader(selected_post["title"]) |
|
st.divider() |
|
st.markdown(selected_post["content"], unsafe_allow_html =True) |
|
|
|
with tab2: |
|
st.subheader("Add a new Post") |
|
title = st.text_input("Post Title") |
|
|
|
|
|
content = st.text_area("Post Content (Markdown supported)", height=800) |
|
|
|
status = st.selectbox("Status", ["private", "public", "achieve"], index=0) |
|
|
|
if st.button("Add Post"): |
|
if title and content: |
|
res = add_post(title, content, status, username) |
|
st.write(res) |
|
else: |
|
st.error("Please fill both the title and content.") |
|
|
|
with tab3: |
|
|
|
show_status = st.sidebar.selectbox("Status", ["private", "public", "achieve"], index = 1) |
|
post_titles = list(get_post_titles(show_status)) |
|
|
|
if post_titles: |
|
|
|
if "selected_post_id" not in st.session_state: |
|
st.session_state.selected_post_id = None |
|
|
|
|
|
selected_post = None |
|
for post in post_titles: |
|
|
|
if st.sidebar.button(post["title"], key=f"button_{post['_id']}"): |
|
st.session_state.selected_post_id = post["_id"] |
|
|
|
|
|
if st.session_state.selected_post_id: |
|
post_id = st.session_state.selected_post_id |
|
selected_post = get_post_by_id(post_id) |
|
|
|
if selected_post: |
|
|
|
new_title = st.text_input("New Title", value=selected_post["title"]) |
|
new_content = st.text_area("New Content (Markdown supported)", value=selected_post["content"], |
|
height=800) |
|
|
|
options = ["private", "public", "achieve"] |
|
new_status = st.selectbox("Status", options, index= options.index(selected_post['status']), key="status_update") |
|
left, right = st.columns(2) |
|
|
|
if left.button("Update Post", use_container_width=True, key=f"update_button_{post_id}"): |
|
if new_title and new_content: |
|
|
|
res = update_post(post_id, new_title, new_content, new_status) |
|
st.success(res) |
|
|
|
else: |
|
st.error("Please fill both the new title and content.") |
|
|
|
|
|
delete_password = st.text_input("Password", type="password", key=f"delete_password_{post_id}") |
|
delete_button = right.button("Permanent Delete Post", use_container_width=True, |
|
key=f"delete_button_{post_id}") |
|
|
|
if delete_button: |
|
if delete_password == PASSWORD: |
|
res = delete_post(post_id) |
|
if res: |
|
st.success("Post deleted successfully!") |
|
st.session_state.selected_post_id = None |
|
post_titles = list(get_post_titles(show_status)) |
|
else: |
|
st.error("An error occurred while deleting the post.") |
|
elif delete_password: |
|
st.error("Incorrect password.") |
|
else: |
|
st.error("Post not found.") |
|
else: |
|
st.error("Please select a post first.") |
|
|
|
else: |
|
st.write("No posts available.") |
|
|
|
|
|
else: |
|
if password: |
|
st.error("Incorrect password. Please try again.") |
|
else: |
|
st.info("Please enter your Username & Password.") |
|
|
|
|