Browse Source

Merge pull request #350 from justanr/349-Fix-Hide-Topic

349 fix hide topic
Alec Nikolas Reiter 7 years ago
parent
commit
19656a1e8a
2 changed files with 28 additions and 12 deletions
  1. 11 9
      flaskbb/forum/models.py
  2. 17 3
      migrations/d0ffadc3ea48_add_hidden_columns.py

+ 11 - 9
flaskbb/forum/models.py

@@ -15,7 +15,7 @@ from sqlalchemy.orm import aliased
 
 
 from flaskbb.extensions import db
 from flaskbb.extensions import db
 from flaskbb.utils.database import (CRUDMixin, HideableCRUDMixin, UTCDateTime,
 from flaskbb.utils.database import (CRUDMixin, HideableCRUDMixin, UTCDateTime,
-                                    make_comparable, HideableQuery)
+                                    make_comparable)
 from flaskbb.utils.helpers import (get_categories_and_forums, get_forums,
 from flaskbb.utils.helpers import (get_categories_and_forums, get_forums,
                                    slugify, time_utcnow, topic_is_unread)
                                    slugify, time_utcnow, topic_is_unread)
 from flaskbb.utils.settings import flaskbb_config
 from flaskbb.utils.settings import flaskbb_config
@@ -691,19 +691,21 @@ class Topic(HideableCRUDMixin, db.Model):
     def _remove_topic_from_forum(self):
     def _remove_topic_from_forum(self):
         # Grab the second last topic in the forum + parents/childs
         # Grab the second last topic in the forum + parents/childs
         topics = Topic.query.filter(
         topics = Topic.query.filter(
-            Topic.forum_id == self.forum_id
+            Topic.forum_id == self.forum_id,
+            Topic.hidden != True
         ).order_by(
         ).order_by(
             Topic.last_post_id.desc()
             Topic.last_post_id.desc()
         ).limit(2).offset(0).all()
         ).limit(2).offset(0).all()
 
 
         # do we want to replace the topic with the last post in the forum?
         # do we want to replace the topic with the last post in the forum?
-        if len(topics) > 1 and topics[0] == self:
-            # Now the second last post will be the last post
-            self.forum.last_post = topics[1].last_post
-            self.forum.last_post_title = topics[1].title
-            self.forum.last_post_user = topics[1].user
-            self.forum.last_post_username = topics[1].username
-            self.forum.last_post_created = topics[1].last_updated
+        if len(topics) > 1:
+            if topics[0] == self:
+                # Now the second last post will be the last post
+                self.forum.last_post = topics[1].last_post
+                self.forum.last_post_title = topics[1].title
+                self.forum.last_post_user = topics[1].user
+                self.forum.last_post_username = topics[1].username
+                self.forum.last_post_created = topics[1].last_updated
         else:
         else:
             self.forum.last_post = None
             self.forum.last_post = None
             self.forum.last_post_title = None
             self.forum.last_post_title = None

+ 17 - 3
migrations/d0ffadc3ea48_add_hidden_columns.py

@@ -20,8 +20,8 @@ depends_on = None
 def upgrade():
 def upgrade():
     # ### commands auto generated by Alembic - please adjust! ###
     # ### commands auto generated by Alembic - please adjust! ###
     with op.batch_alter_table('groups', schema=None) as batch_op:
     with op.batch_alter_table('groups', schema=None) as batch_op:
-        batch_op.add_column(sa.Column('makehidden', sa.Boolean(), nullable=True))
-        batch_op.add_column(sa.Column('viewhidden', sa.Boolean(), nullable=True))
+        batch_op.add_column(sa.Column('makehidden', sa.Boolean(), nullable=True, default=False))
+        batch_op.add_column(sa.Column('viewhidden', sa.Boolean(), nullable=True, default=False))
 
 
     with op.batch_alter_table('groups', schema=None) as batch_op:
     with op.batch_alter_table('groups', schema=None) as batch_op:
         groups = sa.sql.table(
         groups = sa.sql.table(
@@ -50,17 +50,31 @@ def upgrade():
         batch_op.alter_column('makehidden', existing_type=sa.Boolean(), nullable=False)
         batch_op.alter_column('makehidden', existing_type=sa.Boolean(), nullable=False)
 
 
     with op.batch_alter_table('posts', schema=None) as batch_op:
     with op.batch_alter_table('posts', schema=None) as batch_op:
-        batch_op.add_column(sa.Column('hidden', sa.Boolean(), nullable=True))
+        batch_op.add_column(sa.Column('hidden', sa.Boolean(), nullable=True, default=False))
         batch_op.add_column(sa.Column('hidden_at', flaskbb.utils.database.UTCDateTime(timezone=True), nullable=True))
         batch_op.add_column(sa.Column('hidden_at', flaskbb.utils.database.UTCDateTime(timezone=True), nullable=True))
         batch_op.add_column(sa.Column('hidden_by_id', sa.Integer(), nullable=True))
         batch_op.add_column(sa.Column('hidden_by_id', sa.Integer(), nullable=True))
         batch_op.create_foreign_key('fk_Post_hidden_by', 'users', ['hidden_by_id'], ['id'])
         batch_op.create_foreign_key('fk_Post_hidden_by', 'users', ['hidden_by_id'], ['id'])
 
 
+    with op.batch_alter_table('posts', schema=None) as batch_op:
+        posts = sa.sql.table('posts', sa.sql.column('hidden'))
+        batch_op.execute(
+            posts.update().values(hidden=False)
+        )
+        batch_op.alter_column('hidden', existing_type=sa.Boolean(), nullable=False)
+
     with op.batch_alter_table('topics', schema=None) as batch_op:
     with op.batch_alter_table('topics', schema=None) as batch_op:
         batch_op.add_column(sa.Column('hidden', sa.Boolean(), nullable=True))
         batch_op.add_column(sa.Column('hidden', sa.Boolean(), nullable=True))
         batch_op.add_column(sa.Column('hidden_at', flaskbb.utils.database.UTCDateTime(timezone=True), nullable=True))
         batch_op.add_column(sa.Column('hidden_at', flaskbb.utils.database.UTCDateTime(timezone=True), nullable=True))
         batch_op.add_column(sa.Column('hidden_by_id', sa.Integer(), nullable=True))
         batch_op.add_column(sa.Column('hidden_by_id', sa.Integer(), nullable=True))
         batch_op.create_foreign_key('fk_Topic_hidden_by', 'users', ['hidden_by_id'], ['id'])
         batch_op.create_foreign_key('fk_Topic_hidden_by', 'users', ['hidden_by_id'], ['id'])
 
 
+    with op.batch_alter_table('topics', schema=None) as batch_op:
+        topics = sa.sql.table('topics', sa.sql.column('hidden'))
+        batch_op.execute(
+            topics.update().values(hidden=False)
+        )
+        batch_op.alter_column('hidden', existing_type=sa.Boolean(), nullable=False)
+
     # ### end Alembic commands ###
     # ### end Alembic commands ###