From ff180bf11e9925e78f0026b9a0d1c75e068ed83b Mon Sep 17 00:00:00 2001
From: deadvey <deadvey@deadvey.com>
Date: Mon, 31 Mar 2025 16:01:19 +0100
Subject: [PATCH] able to edit previous posts

---
 README.md   | 11 ++---------
 editpost.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++
 glogger.py  | 38 ++++++++++++++++++++++++++++++--------
 newpost.py  | 20 --------------------
 4 files changed, 81 insertions(+), 37 deletions(-)
 create mode 100644 editpost.py

diff --git a/README.md b/README.md
index 0bbd4a3..cf19f75 100755
--- a/README.md
+++ b/README.md
@@ -2,15 +2,8 @@ I've written a basic program in python called glogger, it is a customisable, sta
 
 # Setup
 ## Requirements:
-mysql-connector-python
-mariadb-client
-mariadb-server
-## Database setup
-``` CREATE DATABASE glogger;
-USE glogger;
-CREATE TABLE users ( userID INT PRIMARY KEY AUTO_INCREMENT NOT NULL, username VARCHAR(255) );
-CREATE TABLE posts ( postID INT PRIMARY KEY AUTO_INCREMENT NOT NULL, userID INT, FOREIGN KEY(userID) REFERENCES users(userID), title VARCHAR(255), content VARCHAR(MAX), pubDate VARCHAR(255), editDate VARCHAR(255) );
-```
+* click: `pip install click`
+
 # TO DO
 * Add RSS and/or ATOM support
 * Make it more reliable?
diff --git a/editpost.py b/editpost.py
new file mode 100644
index 0000000..b3e7d5c
--- /dev/null
+++ b/editpost.py
@@ -0,0 +1,49 @@
+import users
+import posts
+import rebuild
+
+import click
+
+def editpost(userID, datetime):
+    post_counter = 0
+    user_posts = []
+    print("Which post do you want to edit?")
+    for current_post_index in range(len(posts.posts)):
+        if posts.posts[current_post_index]["userID"] == userID:
+            print(f"{post_counter}: {posts.posts[current_post_index]['title']}")
+            post_counter += 1
+            user_posts.append(posts.posts[current_post_index])
+
+    try:
+        post_to_edit_index = int(input())
+    except:
+        print("Failed to read input, did you input a number?")
+
+    print(f'''You selected:
+Title: {posts.posts[post_to_edit_index]['title']}
+Content: {posts.posts[post_to_edit_index]['content']}
+''')
+    print('''0. Edit post's (T)itle
+1. Edit post's (C)ontent
+2. (D)elete post''')
+    answer = input()
+
+    if answer == "0" or answer == "T": # Editing the title
+        posts.posts[post_to_edit_index]['title']    = click.edit(posts.posts[post_to_edit_index]['title'])
+        posts.posts[post_to_edit_index]['editdate'] = datetime.now().strftime("%d%m%YZ%H%M%ST")
+
+    elif answer == "1" or answer == "C": # Editing the content
+        posts.posts[post_to_edit_index]['content']  = click.edit(posts.posts[post_to_edit_index]['content'])
+        posts.posts[post_to_edit_index]['editdate'] = datetime.now().strftime("%d%m%YZ%H%M%ST")
+
+    elif answer == "2" or answer == "D": # Editing the existence
+        print("Are you sure you want to delete it? There is no undoing!\n(Y/n)")
+        if input().upper() == "Y":
+            posts.posts.pop(post_to_edit_index)
+        else:
+            exit()
+
+    with open("posts.py", "w") as posts_file:
+        posts_file.write(f"posts = {posts.posts}")
+
+    rebuild.rebuild()
diff --git a/glogger.py b/glogger.py
index 66c95e8..efe85a3 100755
--- a/glogger.py
+++ b/glogger.py
@@ -1,4 +1,9 @@
 import initialise
+try:
+    import posts
+except:
+    print("No posts database")
+    initialise.initialise()
 try:
     import users
 except:
@@ -6,9 +11,26 @@ except:
     initialise.initialise()
 import config
 import newpost
+import editpost
 import rebuild
+
+import sys # Command line arguments
 from datetime import datetime
 
+if len(sys.argv) > 1:
+    for argument in sys.argv:
+        if argument == "--help":
+            print('''
+If no argument is passed, glogger will ask for a username;
+if that user exists, it will as if you want to create a new post, or edit a post (TODO)
+                  --help    Output this help text
+                  --rebuild Rewrite every static page in the site
+                  ''')
+            exit()
+        if argument == "--rebuild":
+            rebuild.rebuild()
+            exit()
+
 username = input("Username: ").lower()
 
 # Check if this user exists
@@ -18,14 +40,14 @@ for current_user in range(len(users.users)):
 		user_present = True
 		userID = current_user
 if user_present == True:
-	print('''What do you want to do?
-	1. Create (N)ew post
-	2. (R)ebuild''')
+    print('''What do you want to do?
+    0. Create (N)ew post
+    1. (E)dit a post''')
 
-	answer = input()
-	if answer == 'N' or answer == '1':
-		newpost.newpost(userID, username, datetime)
-	if answer == 'R' or answer == '2':
-		rebuild.rebuild()
+    answer = input()
+    if answer == 'N' or answer == '0':
+        newpost.newpost(userID, username, datetime)
+    if answer == 'E' or answer == '1':
+        editpost.editpost(userID, datetime)
 else:
 	print('Sorry, that account does not exist, If it should, please ask the webadmin to add this account')
diff --git a/newpost.py b/newpost.py
index 88031fe..8abbb7c 100644
--- a/newpost.py
+++ b/newpost.py
@@ -1,23 +1,3 @@
-# users:
-# +----------+--------------+------+-----+---------+----------------+
-# | Field    | Type         | Null | Key | Default | Extra          |
-# +----------+--------------+------+-----+---------+----------------+
-# | userID   | int(11)      | NO   | PRI | NULL    | auto_increment |
-# | userName | varchar(255) | NO   |     | NULL    |                |
-# +----------+--------------+------+-----+---------+----------------+
-#
-# posts:
-# +----------+--------------+------+-----+---------+----------------+
-# | Field    | Type         | Null | Key | Default | Extra          |
-# +----------+--------------+------+-----+---------+----------------+
-# | postID   | int(11)      | NO   | PRI | NULL    | auto_increment |
-# | userID   | int(11)      | YES  | MUL | NULL    |                |
-# | title    | varchar(255) | YES  |     | NULL    |                |
-# | content  | text         | YES  |     | NULL    |                |
-# | pubDate  | varchar(255) | YES  |     | NULL    |                |
-# | editDate | varchar(255) | YES  |     | NULL    |                |
-# +----------+--------------+------+-----+---------+----------------+
-
 from datetime import datetime
 import click # Used to write post content, it launches a text editor to type into