nuke comments

This commit is contained in:
nyaadev 2019-07-08 21:56:12 +02:00
parent 80c9d580eb
commit 93dffcd663
5 changed files with 59 additions and 3 deletions

View File

@ -11,8 +11,8 @@ from wtforms import (BooleanField, HiddenField, PasswordField, SelectField, Stri
SubmitField, TextAreaField) SubmitField, TextAreaField)
from wtforms.validators import (DataRequired, Email, EqualTo, Length, Optional, Regexp, from wtforms.validators import (DataRequired, Email, EqualTo, Length, Optional, Regexp,
StopValidation, ValidationError) StopValidation, ValidationError)
from wtforms.widgets import HTMLString # For DisabledSelectField
from wtforms.widgets import Select as SelectWidget # For DisabledSelectField from wtforms.widgets import Select as SelectWidget # For DisabledSelectField
from wtforms.widgets import HTMLString # For DisabledSelectField
from wtforms.widgets import html_params from wtforms.widgets import html_params
import dns.exception import dns.exception
@ -346,6 +346,7 @@ class BanForm(FlaskForm):
class NukeForm(FlaskForm): class NukeForm(FlaskForm):
nuke_torrents = SubmitField("\U0001F4A3 Nuke Torrents") nuke_torrents = SubmitField("\U0001F4A3 Nuke Torrents")
nuke_comments = SubmitField("\U0001F4A3 Nuke Comments")
class UploadForm(FlaskForm): class UploadForm(FlaskForm):

View File

@ -199,9 +199,17 @@ class TorrentBase(DeclarativeHelperBase):
return '<{0} #{1.id} \'{1.display_name}\' {1.filesize}b>'.format(type(self).__name__, self) return '<{0} #{1.id} \'{1.display_name}\' {1.filesize}b>'.format(type(self).__name__, self)
def update_comment_count(self): def update_comment_count(self):
self.comment_count = Comment.query.filter_by(torrent_id=self.id).count() self.comment_count = db.session.query(func.count(
Comment.id)).filter_by(torrent_id=self.id).first()[0]
return self.comment_count return self.comment_count
@classmethod
def update_comment_count_db(cls, torrent_id):
comment_count = db.session.query(func.count(Comment.id)).filter_by(
torrent_id=torrent_id).first()[0]
db.session.query(cls).filter_by(id=torrent_id).update({'comment_count': comment_count})
return comment_count
@property @property
def created_utc_timestamp(self): def created_utc_timestamp(self):
''' Returns a UTC POSIX timestamp, as seconds ''' ''' Returns a UTC POSIX timestamp, as seconds '''

View File

@ -29,7 +29,7 @@ def create_magnet_from_es_torrent():
flask_url_for = flask.url_for flask_url_for = flask.url_for
@functools.lru_cache(maxsize=1024*4) @functools.lru_cache(maxsize=1024 * 4)
def _caching_url_for(endpoint, **values): def _caching_url_for(endpoint, **values):
return flask_url_for(endpoint, **values) return flask_url_for(endpoint, **values)

View File

@ -138,6 +138,9 @@
<div class="col-md-6 text-left"> <div class="col-md-6 text-left">
{{ nuke_form.nuke_torrents(class="btn btn-danger", formaction=url_for('users.nuke_user_torrents', user_name=user.username)) }} {{ nuke_form.nuke_torrents(class="btn btn-danger", formaction=url_for('users.nuke_user_torrents', user_name=user.username)) }}
</div> </div>
<div class="col-md-6 text-right">
{{ nuke_form.nuke_comments(class="btn btn-danger", formaction=url_for('users.nuke_user_comments', user_name=user.username)) }}
</div>
</div> </div>
</form> </form>
{% endif %} {% endif %}

View File

@ -301,6 +301,50 @@ def nuke_user_torrents(user_name):
return flask.redirect(url) return flask.redirect(url)
@bp.route('/user/<user_name>/nuke/comments', methods=['POST'])
@admin_only
def nuke_user_comments(user_name):
user = models.User.by_username(user_name)
if not user:
flask.abort(404)
nuke_form = forms.NukeForm(flask.request.form)
if not nuke_form.validate():
flask.abort(401)
url = flask.url_for('users.view_user', user_name=user.username)
nyaa_deleted = 0
sukebei_deleted = 0
nyaa_torrents = set()
sukebei_torrents = set()
for c in chain(user.nyaa_comments, user.sukebei_comments):
nyaa_torrents.add(c.torrent_id)
sukebei_torrents.add(c.torrent_id)
db.session.delete(c)
if isinstance(c, models.NyaaComment):
nyaa_deleted += 1
else:
sukebei_deleted += 1
for tid in nyaa_torrents:
models.NyaaTorrent.update_comment_count_db(tid)
for tid in sukebei_torrents:
models.SukebeiTorrent.update_comment_count_db(tid)
for log_flavour, num in ((models.NyaaAdminLog, nyaa_deleted),
(models.SukebeiAdminLog, sukebei_deleted)):
if num > 0:
log = "Nuked {0} comments of [{1}]({2})".format(num,
user.username,
url)
adminlog = log_flavour(log=log, admin_id=flask.g.user.id)
db.session.add(adminlog)
db.session.commit()
flask.flash('Comments of {0} have been nuked.'.format(user.username),
'success')
return flask.redirect(url)
def _create_user_class_choices(user): def _create_user_class_choices(user):
choices = [('regular', 'Regular')] choices = [('regular', 'Regular')]
default = 'regular' default = 'regular'