diff --git a/nyaa/forms.py b/nyaa/forms.py index 356fc40..af15003 100644 --- a/nyaa/forms.py +++ b/nyaa/forms.py @@ -346,6 +346,7 @@ class BanForm(FlaskForm): class NukeForm(FlaskForm): nuke_torrents = SubmitField("\U0001F4A3 Nuke Torrents") + nuke_comments = SubmitField("\U0001F4A3 Nuke Comments") class UploadForm(FlaskForm): diff --git a/nyaa/models.py b/nyaa/models.py index 44697a0..24a9769 100644 --- a/nyaa/models.py +++ b/nyaa/models.py @@ -199,9 +199,15 @@ class TorrentBase(DeclarativeHelperBase): 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 = db.session.query(func.count( + Comment.id)).filter_by(torrent_id=self.id).first()[0] return self.comment_count + @classmethod + def update_comment_count_db(cls, torrent_id): + cls.query.filter_by(id=torrent_id).update({'comment_count': db.session.query( + func.count(Comment.id)).filter_by(torrent_id=torrent_id).as_scalar()}, False) + @property def created_utc_timestamp(self): ''' Returns a UTC POSIX timestamp, as seconds ''' diff --git a/nyaa/template_utils.py b/nyaa/template_utils.py index ff2904a..5a1473d 100644 --- a/nyaa/template_utils.py +++ b/nyaa/template_utils.py @@ -29,7 +29,7 @@ def create_magnet_from_es_torrent(): flask_url_for = flask.url_for -@functools.lru_cache(maxsize=1024*4) +@functools.lru_cache(maxsize=1024 * 4) def _caching_url_for(endpoint, **values): return flask_url_for(endpoint, **values) diff --git a/nyaa/templates/user.html b/nyaa/templates/user.html index 3becb30..9fbf91e 100644 --- a/nyaa/templates/user.html +++ b/nyaa/templates/user.html @@ -138,6 +138,9 @@
{{ nuke_form.nuke_torrents(class="btn btn-danger", formaction=url_for('users.nuke_user_torrents', user_name=user.username)) }}
+
+ {{ nuke_form.nuke_comments(class="btn btn-danger", formaction=url_for('users.nuke_user_comments', user_name=user.username)) }} +
{% endif %} diff --git a/nyaa/views/users.py b/nyaa/views/users.py index 775bf95..87ca471 100644 --- a/nyaa/views/users.py +++ b/nyaa/views/users.py @@ -301,6 +301,50 @@ def nuke_user_torrents(user_name): return flask.redirect(url) +@bp.route('/user//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): choices = [('regular', 'Regular')] default = 'regular'