From 8f6ec527509f1ef93f7385046ab2bd9194209b43 Mon Sep 17 00:00:00 2001 From: Domeniko Gentner Date: Fri, 27 Nov 2020 22:35:20 +0100 Subject: [PATCH] Streamlined export_location to take the id instead of a string This was quite the stupid bugger... it also involved changing a table in the database --- labertasche/blueprints/bp_comments.py | 6 +++--- labertasche/helper/__init__.py | 15 ++++++++++++--- labertasche/models/t_comments.py | 2 +- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/labertasche/blueprints/bp_comments.py b/labertasche/blueprints/bp_comments.py index 239f494..2583706 100644 --- a/labertasche/blueprints/bp_comments.py +++ b/labertasche/blueprints/bp_comments.py @@ -146,7 +146,7 @@ def check_and_insert_new_comment(): print(e, file=stderr) return make_response(jsonify(status="post-internal-server-error"), 400) - export_location(location) + export_location(t_comment.location_id) return make_response(jsonify(status="post-success", comment_id=t_comment.comments_id), 200) @@ -164,7 +164,7 @@ def check_confirmation_link(email_hash): setattr(comment, "is_published", True) db.session.commit() url = f"{settings.system['blog_url']}{location.location}#comment_{comment.comments_id}" - export_location(location.location) + export_location(location.id_location) return redirect(url) return redirect(f"{settings.system['blog_url']}?cnf=true") @@ -183,7 +183,7 @@ def check_deletion_link(email_hash): query.delete() db.session.commit() url = f"{settings.system['blog_url']}?deleted=true" - export_location(location.location) + export_location(location.id_location) return redirect(url) return redirect(f"{settings.system['blog_url']}?cnf=true") diff --git a/labertasche/helper/__init__.py b/labertasche/helper/__init__.py index 2e31356..0621616 100644 --- a/labertasche/helper/__init__.py +++ b/labertasche/helper/__init__.py @@ -132,16 +132,22 @@ def basic_login_required(f): return wrapped_view -def export_location(location: str) -> bool: +def export_location(location_id: int) -> bool: """ Exports the comments for the location after the comment was accepted - :param location: relative url of the hugo page + :param location_id: The id of the store location to export """ try: + # flush before query + db.session.flush() + + print(f"Location: {location_id}") + # Query - loc_query = db.session.query(TLocation).filter(TLocation.location == location).first() + loc_query = db.session.query(TLocation).filter(TLocation.id_location == location_id).first() if loc_query: + print(f"has loc_query") comments = db.session.query(TComments).filter(TComments.is_spam != True) \ .filter(TComments.is_published == True) \ .filter(TComments.location_id == loc_query.id_location) \ @@ -151,6 +157,7 @@ def export_location(location: str) -> bool: "comments": [] } for comment in comments: + print(f"Data: {comment.comments_id}") bundle['comments'].append(alchemy_query_to_dict(comment)) path_loc = re_match(".*(?=/)", loc_query.location)[0] @@ -163,6 +170,8 @@ def export_location(location: str) -> bool: folder.mkdir(parents=True, exist_ok=True) with out.open('w') as fp: json.dump(bundle, fp) + print("Bundle here ---------- \n") + print(bundle) return True diff --git a/labertasche/models/t_comments.py b/labertasche/models/t_comments.py index ab4c392..8c8e1f9 100644 --- a/labertasche/models/t_comments.py +++ b/labertasche/models/t_comments.py @@ -19,7 +19,7 @@ class TComments(db.Model): comments_id = db.Column(db.Integer, primary_key=True) # foreign keys - location_id = db.Column(db.Text, ForeignKey('t_location.id_location'), nullable=False) + location_id = db.Column(db.Integer, ForeignKey('t_location.id_location'), nullable=False) # data email = db.Column(db.Text, nullable=False)