From 6c80557e39e7add5878a6fa4fb362cdf894a0c59 Mon Sep 17 00:00:00 2001 From: TheAMM Date: Fri, 26 May 2017 16:08:46 +0300 Subject: [PATCH 1/5] SCHEMA CHANGE: Add comment_count to Torrent with a helper function on Torrent to update the count. --- ...ceb2cb4d7c_add_comment_count_to_torrent.py | 44 +++++++++++++++++++ nyaa/models.py | 10 +++++ 2 files changed, 54 insertions(+) create mode 100644 migrations/versions/2bceb2cb4d7c_add_comment_count_to_torrent.py diff --git a/migrations/versions/2bceb2cb4d7c_add_comment_count_to_torrent.py b/migrations/versions/2bceb2cb4d7c_add_comment_count_to_torrent.py new file mode 100644 index 0000000..49b949a --- /dev/null +++ b/migrations/versions/2bceb2cb4d7c_add_comment_count_to_torrent.py @@ -0,0 +1,44 @@ +"""Add comment_count to Torrent + +Revision ID: 2bceb2cb4d7c +Revises: d0eeb8049623 +Create Date: 2017-05-26 15:07:21.114331 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '2bceb2cb4d7c' +down_revision = 'd0eeb8049623' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.add_column('nyaa_torrents', sa.Column('comment_count', sa.Integer(), nullable=False)) + op.create_index(op.f('ix_nyaa_torrents_comment_count'), 'nyaa_torrents', ['comment_count'], unique=False) + + try: + op.add_column('sukebei_torrents', sa.Column('comment_count', sa.Integer(), nullable=False)) + op.create_index(op.f('ix_sukebei_torrents_comment_count'), 'sukebei_torrents', ['comment_count'], unique=False) + except (sa.exc.OperationalError, sa.exc.ProgrammingError): + print('Could not upgrade sukebei!') + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_nyaa_torrents_comment_count'), table_name='nyaa_torrents') + op.drop_column('nyaa_torrents', 'comment_count') + + try: + op.drop_index(op.f('ix_sukebei_torrents_comment_count'), table_name='sukebei_torrents') + op.drop_column('sukebei_torrents', 'comment_count') + except (sa.exc.OperationalError, sa.exc.ProgrammingError): + print('Could not downgrade sukebei!') + + # ### end Alembic commands ### diff --git a/nyaa/models.py b/nyaa/models.py index 8b27679..5d386da 100755 --- a/nyaa/models.py +++ b/nyaa/models.py @@ -67,6 +67,8 @@ class Torrent(db.Model): uploader_ip = db.Column(db.Binary(length=16), default=None, nullable=True) has_torrent = db.Column(db.Boolean, nullable=False, default=False) + comment_count = db.Column(db.Integer, default=0, nullable=False, index=True) + created_time = db.Column(db.DateTime(timezone=False), default=datetime.utcnow, nullable=False) updated_time = db.Column(db.DateTime(timezone=False), default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False) @@ -107,6 +109,10 @@ class Torrent(db.Model): def __repr__(self): return '<{0} #{1.id} \'{1.display_name}\' {1.filesize}b>'.format(type(self).__name__, self) + def update_comment_count(self): + self.comment_count = Comment.query.filter_by(torrent_id = self.id).count() + return self.comment_count + @property def created_utc_timestamp(self): ''' Returns a UTC POSIX timestamp, as seconds ''' @@ -149,6 +155,8 @@ class Torrent(db.Model): if self.uploader_ip: return str(ip_address(self.uploader_ip)) + # Flag getters and setters below + @property def anonymous(self): return self.flags & TorrentFlags.ANONYMOUS @@ -197,6 +205,8 @@ class Torrent(db.Model): def complete(self, value): self.flags = (self.flags & ~TorrentFlags.COMPLETE) | (value and TorrentFlags.COMPLETE) + # Class methods + @classmethod def by_id(cls, id): return cls.query.get(id) From b5ebebf98e9ec94231bad45dc6f9dc355404570a Mon Sep 17 00:00:00 2001 From: TheAMM Date: Fri, 26 May 2017 16:11:31 +0300 Subject: [PATCH 2/5] Update Torrent.comment_count on comment submit/delete --- nyaa/routes.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/nyaa/routes.py b/nyaa/routes.py index 8893567..251ebe5 100644 --- a/nyaa/routes.py +++ b/nyaa/routes.py @@ -658,9 +658,10 @@ def view_torrent(torrent_id): text=comment_text) db.session.add(comment) - db.session.commit() + db.session.flush() - torrent_count = models.Comment.query.filter_by(torrent_id=torrent.id).count() + torrent_count = torrent.update_comment_count() + db.session.commit() flask.flash('Comment successfully posted.', 'success') @@ -686,6 +687,9 @@ def view_torrent(torrent_id): def delete_comment(torrent_id, comment_id): if not flask.g.user: flask.abort(403) + torrent = models.Torrent.by_id(torrent_id) + if not torrent: + flask.abort(404) comment = models.Comment.query.filter_by(id=comment_id).first() if not comment: @@ -695,6 +699,8 @@ def delete_comment(torrent_id, comment_id): flask.abort(403) db.session.delete(comment) + db.session.flush() + torrent.update_comment_count() db.session.commit() flask.flash('Comment successfully deleted.', 'success') From 9cd6c506ae0fc02f9bcbbf912867ec86b2ab025f Mon Sep 17 00:00:00 2001 From: TheAMM Date: Fri, 26 May 2017 16:12:47 +0300 Subject: [PATCH 3/5] Update ElasticSeach index and scripts for comment_count --- es_mapping.yml | 2 ++ import_to_es.py | 1 + sync_es.py | 1 + 3 files changed, 4 insertions(+) diff --git a/es_mapping.yml b/es_mapping.yml index 1fc72ad..3490fd9 100644 --- a/es_mapping.yml +++ b/es_mapping.yml @@ -91,6 +91,8 @@ mappings: type: long seed_count: type: long + comment_count: + type: long # these ids are really only for filtering, thus keyword uploader_id: type: keyword diff --git a/import_to_es.py b/import_to_es.py index 106cbc1..6832781 100755 --- a/import_to_es.py +++ b/import_to_es.py @@ -51,6 +51,7 @@ def mk_es(t): "uploader_id": t.uploader_id, "main_category_id": t.main_category_id, "sub_category_id": t.sub_category_id, + "comment_count": t.comment_count, # XXX all the bitflags are numbers "anonymous": bool(t.anonymous), "trusted": bool(t.trusted), diff --git a/sync_es.py b/sync_es.py index cafafb5..2f2bb09 100755 --- a/sync_es.py +++ b/sync_es.py @@ -86,6 +86,7 @@ def reindex_torrent(t, index_name): "uploader_id": t['uploader_id'], "main_category_id": t['main_category_id'], "sub_category_id": t['sub_category_id'], + "comment_count": t['comment_count'], # XXX all the bitflags are numbers "anonymous": bool(f & TorrentFlags.ANONYMOUS), "trusted": bool(f & TorrentFlags.TRUSTED), From 6f4aad56abc28f498f0c57f882d6993dd0dce83c Mon Sep 17 00:00:00 2001 From: TheAMM Date: Sat, 27 May 2017 18:05:31 +0300 Subject: [PATCH 4/5] Update comment count migration script for #201 --- .../versions/2bceb2cb4d7c_add_comment_count_to_torrent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/migrations/versions/2bceb2cb4d7c_add_comment_count_to_torrent.py b/migrations/versions/2bceb2cb4d7c_add_comment_count_to_torrent.py index 49b949a..aa88a8e 100644 --- a/migrations/versions/2bceb2cb4d7c_add_comment_count_to_torrent.py +++ b/migrations/versions/2bceb2cb4d7c_add_comment_count_to_torrent.py @@ -23,7 +23,7 @@ def upgrade(): try: op.add_column('sukebei_torrents', sa.Column('comment_count', sa.Integer(), nullable=False)) - op.create_index(op.f('ix_sukebei_torrents_comment_count'), 'sukebei_torrents', ['comment_count'], unique=False) + op.create_index(op.f('ix_sukebei_torrents_comment_count'), 'sukebei_torrents', ['comment_count'], unique=False) except (sa.exc.OperationalError, sa.exc.ProgrammingError): print('Could not upgrade sukebei!') From f7ed2c06d35e935c783ca810e912e68247c2c101 Mon Sep 17 00:00:00 2001 From: TheAMM Date: Sat, 27 May 2017 18:10:21 +0300 Subject: [PATCH 5/5] Jeez fine PEP8 I get it --- nyaa/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nyaa/models.py b/nyaa/models.py index 5d386da..18178d6 100755 --- a/nyaa/models.py +++ b/nyaa/models.py @@ -110,7 +110,7 @@ class Torrent(db.Model): return '<{0} #{1.id} \'{1.display_name}\' {1.filesize}b>'.format(type(self).__name__, self) def update_comment_count(self): - self.comment_count = Comment.query.filter_by(torrent_id = self.id).count() + self.comment_count = Comment.query.filter_by(torrent_id=self.id).count() return self.comment_count @property