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/models/bookmark.py | 11 ++++++----- src/web/models/tag.py | 17 +++++++++-------- 2 files changed, 15 insertions(+), 13 deletions(-) (limited to 'src/web/models') 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 -- cgit