aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--migrations/versions/b329a1a7366f_add_new_tables_for_the_bookmarks_and_.py65
-rw-r--r--src/web/controllers/bookmark.py7
-rw-r--r--src/web/models/bookmark.py2
-rw-r--r--src/web/models/tag.py2
-rw-r--r--src/web/views/bookmark.py20
5 files changed, 86 insertions, 10 deletions
diff --git a/migrations/versions/b329a1a7366f_add_new_tables_for_the_bookmarks_and_.py b/migrations/versions/b329a1a7366f_add_new_tables_for_the_bookmarks_and_.py
new file mode 100644
index 00000000..e71b1d54
--- /dev/null
+++ b/migrations/versions/b329a1a7366f_add_new_tables_for_the_bookmarks_and_.py
@@ -0,0 +1,65 @@
+"""add new tables for the bookmarks and the tags of bookmarks
+
+Revision ID: b329a1a7366f
+Revises: 2c5cc05216fa
+Create Date: 2017-05-23 21:42:37.636307
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = 'b329a1a7366f'
+down_revision = '2c5cc05216fa'
+branch_labels = None
+depends_on = None
+
+from datetime import datetime
+from alembic import op
+import sqlalchemy as sa
+
+
+def upgrade():
+ op.drop_table('tag')
+ op.create_table('article_tag',
+ sa.Column('text', sa.String(), nullable=False),
+ sa.Column('article_id', sa.Integer(), nullable=False),
+ sa.ForeignKeyConstraint(['article_id'], ['article.id'],
+ ondelete='CASCADE'),
+ sa.PrimaryKeyConstraint('text', 'article_id')
+ )
+ op.create_table('bookmark_tag',
+ sa.Column('id', sa.Integer(), nullable=False),
+ sa.Column('text', sa.String(), nullable=False),
+ sa.Column('user_id', sa.Integer(), nullable=False),
+ sa.Column('bookmark_id', sa.Integer(), nullable=False),
+ sa.ForeignKeyConstraint(['bookmark_id'], ['bookmark.id'],
+ ondelete='CASCADE'),
+ sa.ForeignKeyConstraint(['user_id'], ['user.id'],
+ ondelete='CASCADE'),
+ sa.PrimaryKeyConstraint('id')
+ )
+ op.create_table('bookmark',
+ sa.Column('id', sa.Integer(), nullable=False),
+ sa.Column('href', sa.String(), default=""),
+ sa.Column('title', sa.String(), default=""),
+ sa.Column('description', sa.String(), default=""),
+ sa.Column('shared', sa.Boolean(), default=False),
+ sa.Column('to_read', sa.Boolean(), default=False),
+ sa.Column('time', sa.DateTime(), default=datetime.utcnow),
+ sa.Column('user_id', sa.Integer(), nullable=False),
+ sa.ForeignKeyConstraint(['user_id'], ['user.id'],
+ ondelete='CASCADE'),
+ sa.PrimaryKeyConstraint('id')
+ )
+
+
+def downgrade():
+ op.drop_table('article_tag')
+ op.drop_table('bookmark_tag')
+ op.drop_table('bookmark')
+ op.create_table('tag',
+ sa.Column('text', sa.String(), nullable=False),
+ sa.Column('article_id', sa.Integer(), nullable=False),
+ sa.ForeignKeyConstraint(['article_id'], ['article.id'],
+ ondelete='CASCADE'),
+ sa.PrimaryKeyConstraint('text', 'article_id')
+ )
diff --git a/src/web/controllers/bookmark.py b/src/web/controllers/bookmark.py
index c8423414..b5413243 100644
--- a/src/web/controllers/bookmark.py
+++ b/src/web/controllers/bookmark.py
@@ -22,8 +22,11 @@ class BookmarkController(AbstractController):
.delete()
for tag in attrs['tags']:
- BookmarkTagController(self.user_id) \
- .update({'id': tag.id}, {'bookmark_id': filters["id"]})
+ BookmarkTagController(self.user_id).create(
+ **{'text': tag.text,
+ 'id': tag.id,
+ 'bookmark_id': tag.bookmark_id,
+ 'user_id': tag.user_id})
del attrs['tags']
return super().update(filters, attrs)
diff --git a/src/web/models/bookmark.py b/src/web/models/bookmark.py
index 6eee3cba..a01e6973 100644
--- a/src/web/models/bookmark.py
+++ b/src/web/models/bookmark.py
@@ -43,7 +43,7 @@ class Bookmark(db.Model, RightMixin):
id = db.Column(db.Integer(), primary_key=True)
href = db.Column(db.String(), default="")
title = db.Column(db.String(), default="")
- description = db.Column(db.String(), default="FR")
+ description = db.Column(db.String(), default="")
shared = db.Column(db.Boolean(), default=False)
to_read = db.Column(db.Boolean(), default=False)
time = db.Column(db.DateTime(), default=datetime.utcnow)
diff --git a/src/web/models/tag.py b/src/web/models/tag.py
index 623808f0..76467c0b 100644
--- a/src/web/models/tag.py
+++ b/src/web/models/tag.py
@@ -24,7 +24,7 @@ class BookmarkTag(db.Model):
text = db.Column(db.String, unique=False)
# foreign keys
- user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
+ user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='CASCADE'))
bookmark_id = db.Column(db.Integer, db.ForeignKey('bookmark.id', ondelete='CASCADE'))
# relationships
diff --git a/src/web/views/bookmark.py b/src/web/views/bookmark.py
index c3a6e7bd..b5666248 100644
--- a/src/web/views/bookmark.py
+++ b/src/web/views/bookmark.py
@@ -55,25 +55,33 @@ def process_form(bookmark_id=None):
if not form.validate():
return render_template('edit_bookmark.html', form=form)
- 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': tags,
'shared': form.shared.data,
'to_read': form.to_read.data}
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_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_contr.update({'id': new_bookmark.id}, bookmark_attr)
+
flash(gettext('Bookmark successfully created.'), 'success')
bgstack15