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

Implemented report action

Actions: Close report, Hide torrent, Delete torrent
This commit is contained in:
nyaazi 2017-05-20 13:33:58 +03:00
parent e6083325d6
commit 354736720b
5 changed files with 265 additions and 194 deletions

View file

@ -282,7 +282,8 @@ class ReportForm(FlaskForm):
reason = TextAreaField('Report reason', [
Length(min=3, max=255,
message='Report reason must be at least %(min)d characters long '
'and %(max)d at most.')
'and %(max)d at most.'),
DataRequired('You must provide a valid report reason.')
])

View file

@ -13,6 +13,7 @@ from urllib.parse import unquote as unquote_url
if app.config['USE_MYSQL']:
from sqlalchemy.dialects import mysql
BinaryType = mysql.BINARY
DescriptionTextType = mysql.TEXT
MediumBlobType = mysql.MEDIUMBLOB
@ -27,7 +28,6 @@ else:
COL_UTF8MB4_BIN = None
COL_ASCII_GENERAL_CI = 'NOCASE'
# For property timestamps
UTC_EPOCH = datetime.utcfromtimestamp(0)
@ -327,6 +327,7 @@ class User(db.Model):
last_login_ip = db.Column(db.Binary(length=16), default=None, nullable=True)
torrents = db.relationship('Torrent', back_populates='user', lazy="dynamic")
# session = db.relationship('Session', uselist=False, back_populates='user')
def __init__(self, username, email, password):
@ -398,8 +399,8 @@ class Report(db.Model):
reason = db.Column(db.String(length=255), nullable=False)
status = db.Column(ChoiceType(ReportStatus, impl=db.Integer()), nullable=False)
user = db.relationship('User', uselist=False)
torrent = db.relationship('Torrent', uselist=False)
user = db.relationship('User', uselist=False, lazy="joined")
torrent = db.relationship('Torrent', uselist=False, lazy="joined")
def __init__(self, torrent_id, user_id, reason):
self.torrent_id = torrent_id
@ -416,8 +417,16 @@ class Report(db.Model):
return (self.created_time - UTC_EPOCH).total_seconds()
@classmethod
def not_reviewed(cls):
reports = cls.query.filter_by(status=0).all()
def by_id(cls, id):
return cls.query.get(id)
@classmethod
def not_reviewed(cls, page):
reports = cls.query.filter(cls.status == 0)\
.join(Torrent, aliased=True).filter(Torrent.flags != 36, Torrent.flags != 2)\
.order_by(db.asc(cls.id))\
.paginate(page=page, per_page=20)
# reports = cls.query.filter_by(status=0).paginate(page=page, per_page=20)
return reports
# class Session(db.Model):

View file

@ -27,7 +27,6 @@ from email.utils import formatdate
from flask_paginate import Pagination
DEBUG_API = False
DEFAULT_MAX_SEARCH_RESULT = 1000
DEFAULT_PER_PAGE = 75
@ -582,7 +581,7 @@ def view_torrent(torrent_id):
return flask.render_template('view.html', torrent=torrent,
files=files,
can_edit=can_edit,
form=report_form)
report_form=report_form)
@app.route('/view/<int:torrent_id>/edit', methods=['GET', 'POST'])
@ -667,12 +666,13 @@ def download_torrent(torrent_id):
@app.route('/view/<int:torrent_id>/submit_report', methods=['POST'])
def submit_report(torrent_id):
if not flask.g.user:
flask.abort(403)
form = forms.ReportForm(flask.request.form)
if flask.request.method == 'POST' and form.validate():
report_reason = (form.reason.data or '').strip()
if flask.g.user is not None:
report_reason = form.reason.data
current_user_id = flask.g.user.id
report = models.Report(
torrent_id=torrent_id,
@ -682,19 +682,38 @@ def submit_report(torrent_id):
db.session.add(report)
db.session.commit()
flask.flash('Successfully reported torrent!', 'success')
else:
flask.abort(403)
return flask.redirect(flask.url_for('view_torrent', torrent_id=torrent_id))
@app.route('/reports', methods=['GET'])
@app.route('/reports', methods=['GET', 'POST'])
def view_reports():
reports = models.Report.not_reviewed()
if not flask.g.user or not flask.g.user.is_admin:
flask.abort(403)
page = flask.request.args.get('p', flask.request.args.get('offset', 1, int), int)
reports = models.Report.not_reviewed(page)
if flask.request.method == 'POST':
data = flask.request.form
torrent = models.Torrent.by_id(data['torrent'])
report = models.Report.by_id(data['report'])
if not torrent or not report or report.status != 0:
flask.abort(404)
else:
if data['action'] == 'delete':
torrent.deleted = True
report.status = 1
elif data['action'] == 'hide':
torrent.hidden = True
report.status = 1
else:
report.status = 2
db.session.commit()
flask.flash('Closed report #{}'.format(report.id), 'success')
return flask.redirect(flask.url_for('view_reports'))
return flask.render_template('reports.html',
reports=reports)

View file

@ -7,24 +7,45 @@
<thead>
<tr>
<th>#</th>
<th>Reported by:</th>
<th>Torrent:</th>
<th>Reason:</th>
<th>Date:</th>
<th>Action:</th>
<th>Reported by</th>
<th>Torrent</th>
<th>Reason</th>
<th>Date</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{% for report in reports %}
{% for report in reports.items %}
<tr>
<td>{{ report.id }}</td>
<td><a href="{{ url_for('view_user', user_name=report.user.username) }}">{{ report.user.username }}</a></td>
<td><a href="{{ url_for('view_torrent', torrent_id=report.torrent.id) }}">{{ report.torrent.display_name }}</a></td>
<td>
<a href="{{ url_for('view_user', user_name=report.user.username) }}">{{ report.user.username }}</a>
</td>
<td>
<a href="{{ url_for('view_torrent', torrent_id=report.torrent.id) }}">{{ report.torrent.display_name }}</a>
</td>
<td>{{ report.reason }}</td>
<td>{{ report.created_time }}</td>
<td style="width: 15%">
<form method="post">
<select name="action" class="pull-left">
<option value="close">Close</option>
<option value="delete">Delete</option>
<option value="hide">Hide</option>
</select>
<input type="hidden" value="{{ report.torrent.id }}" name="torrent">
<input type="hidden" value="{{ report.id }}" name="report">
<button type="submit" class="btn btn-primary pull-right">Review</button>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class=pagination>
{% from "bootstrap/pagination.html" import render_pagination %}
{{ render_pagination(reports) }}
</div>
{% endblock %}

View file

@ -15,19 +15,25 @@
<div class="row">
<div class="col-md-1">Category:</div>
<div class="col-md-5">
<a href="{{ url_for("home", c=torrent.main_category.id_as_string) }}">{{ torrent.main_category.name }}</a> - <a href="{{ url_for("home", c=torrent.sub_category.id_as_string) }}">{{ torrent.sub_category.name }}</a>
<a href="{{ url_for("home", c=torrent.main_category.id_as_string) }}">{{ torrent.main_category.name }}</a>
- <a
href="{{ url_for("home", c=torrent.sub_category.id_as_string) }}">{{ torrent.sub_category.name }}</a>
</div>
<div class="col-md-1">Date:</div>
<div class="col-md-5" data-timestamp="{{ torrent.created_utc_timestamp|int }}">{{ torrent.created_time.strftime('%Y-%m-%d %H:%M UTC') }}</div>
<div class="col-md-5"
data-timestamp="{{ torrent.created_utc_timestamp|int }}">{{ torrent.created_time.strftime('%Y-%m-%d %H:%M UTC') }}</div>
</div>
<div class="row">
<div class="col-md-1">Submitter:</div>
<div class="col-md-5">{% if not torrent.anonymous and torrent.user %}<a href="{{ url_for('view_user', user_name=torrent.user.username) }}">{{ torrent.user.username }}</a>{% else %}Anonymous{% endif %}</div>
<div class="col-md-5">{% if not torrent.anonymous and torrent.user %}
<a href="{{ url_for('view_user', user_name=torrent.user.username) }}">{{ torrent.user.username }}</a>{% else %}
Anonymous{% endif %}</div>
<div class="col-md-1">Seeders:</div>
<div class="col-md-5"><span style="color: green;">{% if config.ENABLE_SHOW_STATS %}{{ torrent.stats.seed_count }}{% else %}Coming soon{% endif %}</span></div>
<div class="col-md-5"><span style="color: green;">{% if config.ENABLE_SHOW_STATS %}
{{ torrent.stats.seed_count }}{% else %}Coming soon{% endif %}</span></div>
</div>
@ -42,7 +48,8 @@
</div>
<div class="col-md-1">Leechers:</div>
<div class="col-md-5"><span style="color: red;">{% if config.ENABLE_SHOW_STATS %}{{ torrent.stats.leech_count }}{% else %}Coming soon{% endif %}</span></div>
<div class="col-md-5"><span style="color: red;">{% if config.ENABLE_SHOW_STATS %}
{{ torrent.stats.leech_count }}{% else %}Coming soon{% endif %}</span></div>
</div>
<div class="row">
@ -50,12 +57,19 @@
<div class="col-md-5">{{ torrent.filesize | filesizeformat(True) }}</div>
<div class="col-md-1">Downloads:</div>
<div class="col-md-5">{% if config.ENABLE_SHOW_STATS %}{{ torrent.stats.download_count }}{% else %}Coming soon{% endif %}</div>
<div class="col-md-5">
{% if config.ENABLE_SHOW_STATS %}{{ torrent.stats.download_count }}{% else %}Coming
soon{% endif %}</div>
</div>
</div>
<div class="panel-footer">
{% if torrent.has_torrent %}<a href="/view/{{ torrent.id }}/torrent"><i class="fa fa-download fa-fw"></i>Download Torrent</a> or {% endif %}<a href="{{ torrent.magnet_uri }}" class="card-footer-item"><i class="fa fa-magnet fa-fw"></i>Magnet</a>
<button type="button" class="btn btn-danger pull-right" data-toggle="modal" data-target="#reportModal">Report</button>
{% if torrent.has_torrent %}
<a href="/view/{{ torrent.id }}/torrent"><i class="fa fa-download fa-fw"></i>Download Torrent</a>
or {% endif %}<a href="{{ torrent.magnet_uri }}" class="card-footer-item"><i
class="fa fa-magnet fa-fw"></i>Magnet</a>
<button type="button" class="btn btn-danger pull-right" data-toggle="modal" data-target="#reportModal">
Report
</button>
<div class="clearfix"></div>
</div>
</div>
@ -76,7 +90,8 @@
<div class="panel-heading panel-heading-collapse">
<h3 class="panel-title">
<div class="row">
<a class="collapsed col-md-12" data-target="#collapseFileList" data-toggle="collapse" style="color:inherit;text-decoration:none;">File list</a>
<a class="collapsed col-md-12" data-target="#collapseFileList" data-toggle="collapse"
style="color:inherit;text-decoration:none;">File list</a>
</div>
</h3>
</div>
@ -91,11 +106,13 @@
{%- for key, value in files.items() recursive %}
<tr>
{%- if value is iterable %}
<td colspan="2" {% if loop.depth0 is greaterthan 0 %}style="padding-left: {{ loop.depth0 * 20 }}px"{% endif %}>
<td colspan="2"
{% if loop.depth0 is greaterthan 0 %}style="padding-left: {{ loop.depth0 * 20 }}px"{% endif %}>
<i class="glyphicon glyphicon-folder-open"></i>&nbsp;&nbsp;<b>{{ key }}</b></td>
{{ loop(value.items()) }}
{%- else %}
<td{% if loop.depth0 is greaterthan 0 %} style="padding-left: {{ loop.depth0 * 20 }}px"{% endif %}>
<td{% if loop.depth0 is greaterthan 0 %}
style="padding-left: {{ loop.depth0 * 20 }}px"{% endif %}>
<i class="glyphicon glyphicon-file"></i>&nbsp;{{ key }}</td>
<td class="col-md-2">{{ value | filesizeformat(True) }}</td>
{%- endif %}
@ -108,7 +125,9 @@
<div class="panel panel-default">
<div class="panel-heading panel-heading-collapse">
<h3 class="panel-title">
<div class="row"><div class="col-md-12">Too many files to display.</div></div>
<div class="row">
<div class="col-md-12">Too many files to display.</div>
</div>
</h3>
</div>
</div>
@ -116,7 +135,9 @@
<div class="panel panel-default">
<div class="panel-heading panel-heading-collapse">
<h3 class="panel-title">
<div class="row"><div class="col-md-12">File list is not available for this torrent.</div></div>
<div class="row">
<div class="col-md-12">File list is not available for this torrent.</div>
</div>
</h3>
</div>
</div>
@ -132,8 +153,8 @@
</div>
<div class="modal-body">
<form method="POST" action="{{ request.url }}/submit_report">
{{ form.csrf_token }}
{{ render_field(form.reason, class_='form-control', maxlength=255) }}
{{ report_form.csrf_token }}
{{ render_field(report_form.reason, class_='form-control', maxlength=255) }}
<div style="float: right;">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-danger">Report</button>