mirror of
				https://github.com/tiyn/container-critique.git
				synced 2025-10-31 03:01:21 +01:00 
			
		
		
		
	src: added entry deletion
This commit is contained in:
		
							
								
								
									
										27
									
								
								src/app.py
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								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/<ident>", 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") | ||||
|   | ||||
| @@ -158,6 +158,9 @@ def gen_stand_string(ident): | ||||
|             username + "</a> on <a href=\"" + \ | ||||
|             url_for("index", _anchor=str(ident)) + "\">" + str(reviewed) + \ | ||||
|             "</a></small><br>\n" | ||||
|         content_string += "<small>[<a href=\"" + \ | ||||
|             url_for("delete_entry", ident=ident) + \ | ||||
|             "\">delete entry</a>]</small>" | ||||
|         content_string += text + "<br>\n" | ||||
|     return content_string | ||||
|  | ||||
|   | ||||
| @@ -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() | ||||
|   | ||||
| @@ -38,7 +38,7 @@ | ||||
|         {% else %} | ||||
|         <a href="{{ url_for('logout') }}">Logout</a> | ||||
|         - | ||||
|         <a href="{{ url_for('write') }}">Write entry</a> | ||||
|         <a href="{{ url_for('write_entry') }}">Write entry</a> | ||||
|         {% endif %} | ||||
|     </footer> | ||||
| </body> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user