From 894474fdee44dcc2833c2a468585f3cd6ac36bfd Mon Sep 17 00:00:00 2001 From: tiyn Date: Mon, 1 Aug 2022 23:48:27 +0200 Subject: [PATCH] src: added entry deletion --- README.md | 2 +- src/app.py | 27 +++++++++++++-------------- src/content.py | 3 +++ src/database.py | 25 +++++++++++++++---------- src/templates/template.html | 2 +- 5 files changed, 33 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 8d2e0ca..a98a8ec 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ The blog is intended to be used to review and critique things. - [ ] Review blog entries - [x] Writing entries - [ ] Editing entries - - [ ] Deleting entries + - [x] Deleting entries - [x] Infinite-scroll blog page - [x] Archive page - [x] Months as headings diff --git a/src/app.py b/src/app.py index 82e06aa..04aa588 100644 --- a/src/app.py +++ b/src/app.py @@ -36,14 +36,12 @@ def page_not_found(e): @app.route("/") -@app.route("/index.html") def index(): content = con_gen.gen_index_string() return render_template("index.html", content_string=content) @app.route("/archive") -@app.route("/archive.html") def archive(): entries = db.get_entries() content = con_gen.gen_arch_string() @@ -67,9 +65,6 @@ def entry(ident): @app.route("/feed") -@app.route("/feed.xml") -@app.route("/rss") -@app.route("/rss.xml") def feed(): content = con_gen.get_rss_string() rss_xml = render_template("rss.xml", content_string=content) @@ -85,7 +80,6 @@ def load_user(ident): @app.route("/login", methods=["GET", "POST"]) -@app.route("/login.html", methods=["GET", "POST"]) def login(): if current_user.is_authenticated: return redirect(url_for("index")) @@ -103,14 +97,12 @@ def login(): @app.route('/logout') -@app.route('/logout.html') def logout(): logout_user() - return redirect(url_for('index')) + return redirect(url_for("index")) @app.route("/register", methods=["GET", "POST"]) -@app.route("/register.html", methods=["GET", "POST"]) def register(): if current_user.is_authenticated or not config.ALLOW_REGISTRATION: return redirect(url_for("index")) @@ -130,15 +122,12 @@ def register(): return render_template("register.html", form=form) -@app.route("/write", methods=["GET", "POST"]) -@app.route("/write.html", methods=["GET", "POST"]) +@app.route("/write_entry", methods=["GET", "POST"]) @login_required -def write(): +def write_entry(): if not current_user.is_authenticated: return redirect(url_for("index")) form = WriteForm() - print("data", form.text.data) - print("data", request.form.get("text")) if form.validate_on_submit(): db.insert_entry(form.name.data, form.date.data, form.text.data, form.rating.data, current_user.id) @@ -146,5 +135,15 @@ def write(): return render_template("write.html", form=form) +@app.route("/delete_entry/", methods=["GET", "POST"]) +@login_required +def delete_entry(ident): + if not current_user.is_authenticated: + return redirect(url_for("index")) + if current_user.id == db.get_entry_by_id(ident)[5]: + db.delete_entry(ident) + return redirect(url_for("index")) + + if __name__ == "__main__": app.run(host="0.0.0.0") diff --git a/src/content.py b/src/content.py index e286af8..51738cc 100644 --- a/src/content.py +++ b/src/content.py @@ -158,6 +158,9 @@ def gen_stand_string(ident): username + " on " + str(reviewed) + \ "
\n" + content_string += "[delete entry]" content_string += text + "
\n" return content_string diff --git a/src/database.py b/src/database.py index 6379845..eeb26f1 100644 --- a/src/database.py +++ b/src/database.py @@ -36,7 +36,8 @@ class Database: self.setup_db() def connect(self): - """Connect to an existing database instance based on the object + """ + Connect to an existing database instance based on the object attributes. """ path = os.path.join(self.DB_DIR, "data.db") @@ -64,8 +65,6 @@ class Database: db.commit() def insert_user(self, user): - """Insert a new user into the database. - """ if self.check_user_name(user.name) and user.pass_hash is not None: db = self.connect() crs = db.cursor() @@ -78,8 +77,6 @@ class Database: return None def insert_entry(self, name, date, text, rating, user_id=None): - """Insert a new user into the database. - """ db = self.connect() crs = db.cursor() reviewed = dt.today().strftime('%Y-%m-%d') @@ -90,6 +87,19 @@ class Database: db.commit() return crs.lastrowid + def check_user_name(self, name): + if self.get_user_by_name(name) is None: + return True + return False + + def delete_entry(self, ident): + db = self.connect() + crs = db.cursor() + query = "DELETE FROM " + self.ENTRY_TABLE_FILE + " WHERE id = ?" + crs.execute(query, (ident, )) + db.commit() + return crs.lastrowid + def get_entries(self): db = self.connect() crs = db.cursor() @@ -106,11 +116,6 @@ class Database: crs.execute(query, (name, )) return crs.fetchall() - def check_user_name(self, name): - if self.get_user_by_name(name) is None: - return True - return False - def get_entry_by_id(self, ident): db = self.connect() crs = db.cursor() diff --git a/src/templates/template.html b/src/templates/template.html index 3bad69e..65117d2 100644 --- a/src/templates/template.html +++ b/src/templates/template.html @@ -38,7 +38,7 @@ {% else %} Logout - - Write entry + Write entry {% endif %}