From 613c7c49743e222ba128e8f3b681ece637417e5d Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Wed, 30 Mar 2022 23:14:49 +0200 Subject: fix: [controllers] fixed an issue in the management of bookmark's tags. --- newspipe/controllers/bookmark.py | 20 ++++++++++---------- newspipe/models/bookmark.py | 9 +-------- newspipe/web/views/bookmark.py | 19 +++---------------- 3 files changed, 14 insertions(+), 34 deletions(-) diff --git a/newspipe/controllers/bookmark.py b/newspipe/controllers/bookmark.py index 81412a5e..73f0a11f 100644 --- a/newspipe/controllers/bookmark.py +++ b/newspipe/controllers/bookmark.py @@ -1,5 +1,7 @@ import logging +from flask_login import current_user + from .abstract import AbstractController from .tag import BookmarkTagController from newspipe.models import Bookmark @@ -20,16 +22,14 @@ class BookmarkController(AbstractController): BookmarkTagController(self.user_id).read( **{"bookmark_id": filters["id"]} ).delete() - for tag in attrs["tags"]: - BookmarkTagController(self.user_id).create( - **{ - "text": tag.text, - "id": tag.id, - "bookmark_id": tag.bookmark_id, - "user_id": tag.user_id, - } - ) - + if tag: + BookmarkTagController(self.user_id).create( + **{ + "text": tag.strip(), + "user_id": current_user.id, + "bookmark_id": filters["id"], + } + ) del attrs["tags"] return super().update(filters, attrs) diff --git a/newspipe/models/bookmark.py b/newspipe/models/bookmark.py index d6be3862..76be3422 100644 --- a/newspipe/models/bookmark.py +++ b/newspipe/models/bookmark.py @@ -48,16 +48,9 @@ class Bookmark(db.Model, RightMixin): user_id = db.Column(db.Integer(), db.ForeignKey("user.id")) # relationships - # tags = db.relationship( - # BookmarkTag, - # backref="of_bookmark", - # lazy="dynamic", - # cascade="all,delete-orphan", - # order_by=desc(BookmarkTag.text), - # ) tags = db.relationship( "BookmarkTag", - back_populates="bookmark", + # back_populates="bookmark", cascade="all, delete-orphan", foreign_keys="[BookmarkTag.bookmark_id]", ) diff --git a/newspipe/web/views/bookmark.py b/newspipe/web/views/bookmark.py index 04d2e8af..f3d336dd 100644 --- a/newspipe/web/views/bookmark.py +++ b/newspipe/web/views/bookmark.py @@ -41,7 +41,7 @@ from flask_paginate import Pagination, get_page_args from werkzeug.exceptions import BadRequest from newspipe.bootstrap import db -from newspipe.controllers import BookmarkController, BookmarkTagController +from newspipe.controllers import BookmarkController from newspipe.lib.data import export_bookmarks, import_pinboard_json from newspipe.lib.utils import redirect_url from newspipe.web.forms import BookmarkForm @@ -151,7 +151,6 @@ def process_form(bookmark_id=None): "Process the creation/edition of bookmarks." form = BookmarkForm() bookmark_contr = BookmarkController(current_user.id) - tag_contr = BookmarkTagController(current_user.id) if not form.validate(): return render_template("edit_bookmark.html", form=form) @@ -170,26 +169,14 @@ def process_form(bookmark_id=None): } if bookmark_id is not None: - tags = [] - for tag in form.tags.data.split(","): - new_tag = tag_contr.create( - text=tag.strip(), user_id=current_user.id, bookmark_id=bookmark_id - ) - tags.append(new_tag) - bookmark_attr["tags"] = tags + bookmark_attr["tags"] = form.tags.data.split(",") bookmark_contr.update({"id": bookmark_id}, bookmark_attr) flash(gettext("Bookmark successfully updated."), "success") return redirect(url_for("bookmark.form", bookmark_id=bookmark_id)) # Create a new bookmark new_bookmark = bookmark_contr.create(**bookmark_attr) - tags = [] - for tag in form.tags.data.split(","): - new_tag = tag_contr.create( - text=tag.strip(), user_id=current_user.id, bookmark_id=new_bookmark.id - ) - tags.append(new_tag) - bookmark_attr["tags"] = tags + bookmark_attr["tags"] = form.tags.data.split(",") bookmark_contr.update({"id": new_bookmark.id}, bookmark_attr) flash(gettext("Bookmark successfully created."), "success") return redirect(url_for("bookmark.form", bookmark_id=new_bookmark.id)) -- cgit