diff options
author | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2017-05-23 15:31:07 +0200 |
---|---|---|
committer | Cédric Bonhomme <cedric@cedricbonhomme.org> | 2017-05-23 15:31:07 +0200 |
commit | b02a3c469e238892a1c13d32c1e208d35e7885ce (patch) | |
tree | 814abd1cf5e06b139c5978ace289ad2ad20d2c9c /src/web | |
parent | Removed debug print (diff) | |
download | newspipe-b02a3c469e238892a1c13d32c1e208d35e7885ce.tar.gz newspipe-b02a3c469e238892a1c13d32c1e208d35e7885ce.tar.bz2 newspipe-b02a3c469e238892a1c13d32c1e208d35e7885ce.zip |
Update tags of bookmarks.
Diffstat (limited to 'src/web')
-rw-r--r-- | src/web/controllers/__init__.py | 4 | ||||
-rw-r--r-- | src/web/controllers/bookmark.py | 17 | ||||
-rw-r--r-- | src/web/controllers/tag.py | 19 | ||||
-rw-r--r-- | src/web/models/bookmark.py | 11 | ||||
-rw-r--r-- | src/web/models/tag.py | 17 | ||||
-rw-r--r-- | src/web/templates/bookmarks.html | 2 | ||||
-rw-r--r-- | src/web/views/bookmark.py | 18 |
7 files changed, 60 insertions, 28 deletions
diff --git a/src/web/controllers/__init__.py b/src/web/controllers/__init__.py index dd497e9a..5fbc2619 100644 --- a/src/web/controllers/__init__.py +++ b/src/web/controllers/__init__.py @@ -4,7 +4,9 @@ from .article import ArticleController from .user import UserController from .icon import IconController from .bookmark import BookmarkController +from .tag import BookmarkTagController __all__ = ['FeedController', 'CategoryController', 'ArticleController', - 'UserController', 'IconController', 'BookmarkController'] + 'UserController', 'IconController', 'BookmarkController', + 'BookmarkTagController'] diff --git a/src/web/controllers/bookmark.py b/src/web/controllers/bookmark.py index f7dab9d8..c8423414 100644 --- a/src/web/controllers/bookmark.py +++ b/src/web/controllers/bookmark.py @@ -3,8 +3,9 @@ import itertools from datetime import datetime, timedelta from bootstrap import db -from .abstract import AbstractController from web.models import Bookmark +from .abstract import AbstractController +from .tag import BookmarkTagController logger = logging.getLogger(__name__) @@ -15,6 +16,14 @@ class BookmarkController(AbstractController): def count_by_href(self, **filters): return self._count_by(Bookmark.href, filters) - def update(self, filters, attrs, *args, **kwargs): - #self.tag_objs = attrs['tags'] - return super().update(filters, attrs, *args, **kwargs) + def update(self, filters, attrs): + BookmarkTagController(self.user_id) \ + .read(**{'bookmark_id': filters["id"]}) \ + .delete() + + for tag in attrs['tags']: + BookmarkTagController(self.user_id) \ + .update({'id': tag.id}, {'bookmark_id': filters["id"]}) + + del attrs['tags'] + return super().update(filters, attrs) diff --git a/src/web/controllers/tag.py b/src/web/controllers/tag.py new file mode 100644 index 00000000..a40387c9 --- /dev/null +++ b/src/web/controllers/tag.py @@ -0,0 +1,19 @@ +import logging +import itertools +from datetime import datetime, timedelta + +from bootstrap import db +from .abstract import AbstractController +from web.models.tag import BookmarkTag + +logger = logging.getLogger(__name__) + + +class BookmarkTagController(AbstractController): + _db_cls = BookmarkTag + + def count_by_href(self, **filters): + return self._count_by(BookmarkTag.text, filters) + + def update(self, filters, attrs): + return super().update(filters, attrs) diff --git a/src/web/models/bookmark.py b/src/web/models/bookmark.py index 891c3eda..6eee3cba 100644 --- a/src/web/models/bookmark.py +++ b/src/web/models/bookmark.py @@ -28,9 +28,11 @@ __license__ = "GPLv3" from bootstrap import db from datetime import datetime +from sqlalchemy import desc from sqlalchemy.orm import validates from sqlalchemy.ext.associationproxy import association_proxy +from web.models.tag import BookmarkTag from web.models.right_mixin import RightMixin @@ -48,11 +50,10 @@ class Bookmark(db.Model, RightMixin): user_id = db.Column(db.Integer(), db.ForeignKey('user.id')) # relationships - tag_objs = db.relationship('BookmarkTag', back_populates='bookmark', - cascade='all,delete-orphan', - lazy=False, - foreign_keys='[BookmarkTag.bookmark_id]') - tags = association_proxy('tag_objs', 'text') + tags = db.relationship(BookmarkTag, backref='of_bookmark', lazy='dynamic', + cascade='all,delete-orphan', + order_by=desc(BookmarkTag.text)) + tags_proxy = association_proxy('tags', 'text') @validates('description') diff --git a/src/web/models/tag.py b/src/web/models/tag.py index 70430fa6..ab901cda 100644 --- a/src/web/models/tag.py +++ b/src/web/models/tag.py @@ -21,16 +21,17 @@ class ArticleTag(db.Model): class BookmarkTag(db.Model): - text = db.Column(db.String, primary_key=True, unique=False) - # user_id = db.Column(db.Integer(), db.ForeignKey('user.id')) + id = db.Column(db.Integer, primary_key=True) + text = db.Column(db.String, unique=False) # foreign keys - bookmark_id = db.Column(db.Integer, db.ForeignKey('bookmark.id', ondelete='CASCADE'), - primary_key=True) + user_id = db.Column(db.Integer, db.ForeignKey('user.id')) + bookmark_id = db.Column(db.Integer, db.ForeignKey('bookmark.id', ondelete='CASCADE')) # relationships - bookmark = db.relationship('Bookmark', back_populates='tag_objs', - foreign_keys=[bookmark_id]) + bookmark = db.relationship('Bookmark', back_populates='tags', + cascade="all,delete", foreign_keys=[bookmark_id]) - def __init__(self, text): - self.text = text + # def __init__(self, text, user_id): + # self.text = text + # self.user_id = user_id diff --git a/src/web/templates/bookmarks.html b/src/web/templates/bookmarks.html index fc0e3aaa..3e65d2c0 100644 --- a/src/web/templates/bookmarks.html +++ b/src/web/templates/bookmarks.html @@ -6,7 +6,7 @@ {% for bookmark in bookmarks %} <li class="list-group-item"> <a href="{{ bookmark.href }}">{{ bookmark.href }}</a> - <a href="{{ bookmark.href }}">{{ bookmark.tags }}</a> + <a href="{{ bookmark.href }}">{{ bookmark.tags_proxy }}</a> <a href="{{ url_for('bookmark.form', bookmark_id=bookmark.id) }}">edit</a> </li> {% endfor %} diff --git a/src/web/views/bookmark.py b/src/web/views/bookmark.py index c2fdc597..64acded4 100644 --- a/src/web/views/bookmark.py +++ b/src/web/views/bookmark.py @@ -9,8 +9,7 @@ from flask_login import login_required, current_user import conf from bootstrap import db from web.forms import BookmarkForm -from web.controllers import BookmarkController -from web.models.tag import BookmarkTag +from web.controllers import BookmarkController, BookmarkTagController logger = logging.getLogger(__name__) bookmarks_bp = Blueprint('bookmarks', __name__, url_prefix='/bookmarks') @@ -39,7 +38,7 @@ def form(bookmark_id=None): action = gettext('Edit bookmark') head_titles = [action] form = BookmarkForm(obj=bookmark) - form.tags.data = bookmark.tags + form.tags.data = bookmark.tags_proxy return render_template('edit_bookmark.html', action=action, head_titles=head_titles, bookmark=bookmark, form=form) @@ -51,23 +50,24 @@ def form(bookmark_id=None): def process_form(bookmark_id=None): 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) - # Edit an existing bookmark + tags = [] + for tag in form.tags.data.split(','): + new_tag = tag_contr.create(text= tag.strip(), user_id= current_user.id) + tags.append(new_tag) + bookmark_attr = {'href': form.href.data, 'description': form.description.data, 'title': form.title.data, - 'tags': [tag.strip() for tag in form.tags.data.split(',')], + 'tags': tags, 'shared': form.shared.data, 'to_read': form.to_read.data} if bookmark_id is not None: - # bookmark = BookmarkController(current_user.id).get(id=bookmark_id) - # form.populate_obj(bookmark) - # bookmark.tags = [tag.strip() for tag in form.tags.data.split(',')], - # db.session.commit() bookmark_contr.update({'id': bookmark_id}, bookmark_attr) flash(gettext('Bookmark successfully updated.'), 'success') return redirect(url_for('bookmark.form', bookmark_id=bookmark_id)) |