From 0fa1a1c363aaf934a9ce80ee8b255cf8b840901e Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Fri, 21 Apr 2017 23:09:42 +0200 Subject: We will finally have a table for tags of articles and tags of bookmarks. --- src/web/models/article.py | 2 -- src/web/models/bookmark.py | 16 ++++------------ src/web/models/tag.py | 18 ++++++++---------- 3 files changed, 12 insertions(+), 24 deletions(-) (limited to 'src/web') diff --git a/src/web/models/article.py b/src/web/models/article.py index 6245b10e..f5ef619e 100644 --- a/src/web/models/article.py +++ b/src/web/models/article.py @@ -34,8 +34,6 @@ from sqlalchemy.ext.associationproxy import association_proxy from web.models.right_mixin import RightMixin - - class Article(db.Model, RightMixin): "Represent an article from a feed." id = db.Column(db.Integer(), primary_key=True) diff --git a/src/web/models/bookmark.py b/src/web/models/bookmark.py index 6101a5f0..891c3eda 100644 --- a/src/web/models/bookmark.py +++ b/src/web/models/bookmark.py @@ -34,13 +34,6 @@ from sqlalchemy.ext.associationproxy import association_proxy from web.models.right_mixin import RightMixin -bookmarktags_table = db.Table('bookmarktags', db.metadata, - db.Column('bookmark_id', db.Integer, db.ForeignKey("bookmark.id"), - primary_key=True), - db.Column('tag_text', db.String, db.ForeignKey("BookmarkTag.text"), - primary_key=True) -) - class Bookmark(db.Model, RightMixin): """ Represent a bookmark. @@ -55,11 +48,10 @@ class Bookmark(db.Model, RightMixin): user_id = db.Column(db.Integer(), db.ForeignKey('user.id')) # relationships - tag_objs = db.relationship("BookmarkTag", secondary=lambda: bookmarktags_table) - # tag_objs = db.relationship('BookmarkTag', back_populates='bookmark', - # cascade='all,delete-orphan', - # lazy=False, - # foreign_keys='[BookmarkTag.bookmark_id]') + 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') diff --git a/src/web/models/tag.py b/src/web/models/tag.py index c23d15ea..70430fa6 100644 --- a/src/web/models/tag.py +++ b/src/web/models/tag.py @@ -6,6 +6,7 @@ from bootstrap import db class ArticleTag(db.Model): text = db.Column(db.String, primary_key=True, unique=False) + # user_id = db.Column(db.Integer(), db.ForeignKey('user.id')) # foreign keys article_id = db.Column(db.Integer, db.ForeignKey('article.id', ondelete='CASCADE'), @@ -13,26 +14,23 @@ class ArticleTag(db.Model): # relationships article = db.relationship('Article', back_populates='tag_objs', - foreign_keys=[article_id]) + foreign_keys=[article_id]) def __init__(self, text): self.text = text class BookmarkTag(db.Model): - __tablename__ = 'BookmarkTag' - #id = db.Column(db.Integer, primary_key=True) - text = db.Column(db.String, primary_key=True) + text = db.Column(db.String, primary_key=True, unique=False) + # user_id = db.Column(db.Integer(), db.ForeignKey('user.id')) # foreign keys - # bookmark_id = db.Column(db.Integer, db.ForeignKey('bookmark.id', ondelete='CASCADE'), - # primary_key=True) + bookmark_id = db.Column(db.Integer, db.ForeignKey('bookmark.id', ondelete='CASCADE'), + primary_key=True) # relationships - # bookmark = db.relationship('Bookmark', back_populates='tag_objs', - # single_parent=True, - # cascade="all, delete-orphan", - # foreign_keys=[bookmark_id]) + bookmark = db.relationship('Bookmark', back_populates='tag_objs', + foreign_keys=[bookmark_id]) def __init__(self, text): self.text = text -- cgit From 271179e4789bfa310aad3a21b5805a4a5ea544ba Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Sat, 22 Apr 2017 08:43:12 +0200 Subject: Removed debug print --- src/web/controllers/abstract.py | 1 - 1 file changed, 1 deletion(-) (limited to 'src/web') diff --git a/src/web/controllers/abstract.py b/src/web/controllers/abstract.py index 074ce24c..8563d5f2 100644 --- a/src/web/controllers/abstract.py +++ b/src/web/controllers/abstract.py @@ -100,7 +100,6 @@ class AbstractController: def update(self, filters, attrs, return_objs=False, commit=True): assert attrs, "attributes to update must not be empty" - print(attrs) result = self._get(**filters).update(attrs, synchronize_session=False) if commit: db.session.flush() -- cgit From b02a3c469e238892a1c13d32c1e208d35e7885ce Mon Sep 17 00:00:00 2001 From: Cédric Bonhomme Date: Tue, 23 May 2017 15:31:07 +0200 Subject: Update tags of bookmarks. --- src/web/controllers/__init__.py | 4 +++- src/web/controllers/bookmark.py | 17 +++++++++++++---- src/web/controllers/tag.py | 19 +++++++++++++++++++ src/web/models/bookmark.py | 11 ++++++----- src/web/models/tag.py | 17 +++++++++-------- src/web/templates/bookmarks.html | 2 +- src/web/views/bookmark.py | 18 +++++++++--------- 7 files changed, 60 insertions(+), 28 deletions(-) create mode 100644 src/web/controllers/tag.py (limited to 'src/web') 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 %}
  • {{ bookmark.href }}  - {{ bookmark.tags }}  + {{ bookmark.tags_proxy }}  edit
  • {% 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)) -- cgit