1
0
Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-12-22 08:49:59 +00:00

[DB Changes! Read PR!] Merge pull request #196 from nyaadevs/elasticsearch-comment-count

Elasticsearch comment count. Read PR #201 as well.
This commit is contained in:
Anna-Maria Meriniemi 2017-05-27 20:12:43 +03:00 committed by GitHub
commit d65602ee9f
6 changed files with 66 additions and 2 deletions

View file

@ -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

View file

@ -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),

View file

@ -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 ###

View file

@ -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)

View file

@ -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')

View file

@ -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),