From 2cda737ccb21470a792a99c07271e978b5e6395f Mon Sep 17 00:00:00 2001 From: Ali Kaafarani Date: Sun, 18 Sep 2022 22:39:45 +0200 Subject: [PATCH] Add guestbook moderation --- src/kvikshaug/app.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/kvikshaug/app.py b/src/kvikshaug/app.py index 459d933..ea84b70 100644 --- a/src/kvikshaug/app.py +++ b/src/kvikshaug/app.py @@ -25,7 +25,7 @@ if not os.path.exists("data/guestbook.json"): @app.route("/") def home(): with open("data/guestbook.json") as file_: - guestbook = json.load(file_) + guestbook = [c for c in json.load(file_) if c["published"]] for message in guestbook: message["datetime"] = datetime.strptime(message["datetime"], "%Y-%m-%d %H:%M") guestbook = sorted(guestbook, key=lambda m: m["datetime"], reverse=True) @@ -40,10 +40,13 @@ def guestbook(): with open("data/guestbook.json") as file_: guestbook = json.load(file_) with open("data/guestbook.json", "w") as file_: - guestbook.append({ - "message": message, - "ip": request.remote_addr, - "datetime": datetime.now().strftime("%Y-%m-%d %H:%M") - }) + guestbook.append( + { + "message": message, + "ip": request.remote_addr, + "datetime": datetime.now().strftime("%Y-%m-%d %H:%M"), + "published": False, + } + ) json.dump(guestbook, file_) return redirect("/#guestbook") -- 2.45.2