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/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..aa88a8e --- /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 e34c767..9b20c42 100755 --- a/nyaa/models.py +++ b/nyaa/models.py @@ -97,6 +97,8 @@ class TorrentBase(DeclarativeHelperBase): 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) @@ -171,6 +173,10 @@ class TorrentBase(DeclarativeHelperBase): 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 ''' @@ -213,6 +219,8 @@ class TorrentBase(DeclarativeHelperBase): 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 @@ -261,6 +269,8 @@ class TorrentBase(DeclarativeHelperBase): 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) 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') 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),