File size: 5,861 Bytes
29b2b56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
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():

    # Create a placeholder for the password input field
    user_placeholder = st.empty()
    password_placeholder = st.empty()

    # Show password input if it's not already correct
    username = user_placeholder.text_input("Enter Username")
    password = password_placeholder.text_input("Enter Password", type="password")

    # Check if the password is correct
    if password == PASSWORD and username == USERNAME:
        password_placeholder.empty()  # Hide the password input
        user_placeholder.empty()  # Hide the password input

        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)

                # Display the content of the selected post
                st.subheader(selected_post["title"])
                st.divider()
                st.markdown(selected_post["content"], unsafe_allow_html =True)  # Render the content as Markdown

        with tab2:
            st.subheader("Add a new Post")
            title = st.text_input("Post Title")

            # Text input area for Markdown content
            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:
                # Use session state to remember the selected post ID
                if "selected_post_id" not in st.session_state:
                    st.session_state.selected_post_id = None

                # Display titles as clickable buttons in the sidebar
                selected_post = None
                for post in post_titles:
                    # Check if the post is selected
                    if st.sidebar.button(post["title"], key=f"button_{post['_id']}"):
                        st.session_state.selected_post_id = post["_id"]

                # Only show the update section if a post is selected
                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:
                        # Display the content of the 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:
                                # Call the update_post function to save the changes to MongoDB
                                res = update_post(post_id, new_title, new_content, new_status)
                                st.success(res)  # Display the result from the update function
                                # Optionally, display updated content for the user to confirm the changes
                            else:
                                st.error("Please fill both the new title and content.")

                        # Permanent delete logic
                        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  # Reset selected post after deletion
                                    post_titles = list(get_post_titles(show_status))  # Refresh the list of posts
                                else:
                                    st.error("An error occurred while deleting the post.")
                            elif delete_password:  # If password is filled but incorrect
                                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:  # If password is entered but incorrect
            st.error("Incorrect password. Please try again.")
        else:  # If no password is entered
            st.info("Please enter your Username & Password.")