mirror of
https://gitlab.com/SIGBUS/nyaa.git
synced 2024-12-22 09:10:00 +00:00
Adding report table, route and view.
This commit is contained in:
parent
375dcf15ef
commit
98fbe6efb1
|
@ -278,6 +278,14 @@ class TorrentFileData(object):
|
|||
# https://wiki.theory.org/BitTorrentSpecification#Metainfo_File_Structure
|
||||
|
||||
|
||||
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.')
|
||||
])
|
||||
|
||||
|
||||
def _validate_trackers(torrent_dict, tracker_to_check_for=None):
|
||||
announce = torrent_dict.get('announce')
|
||||
announce_string = _validate_bytes(announce, 'announce', 'utf-8')
|
||||
|
|
|
@ -380,6 +380,39 @@ class User(db.Model):
|
|||
return self.level is UserLevelType.TRUSTED
|
||||
|
||||
|
||||
class ReportStatus(IntEnum):
|
||||
IN_REVIEW = 0
|
||||
VALID = 1
|
||||
INVALID = 2
|
||||
|
||||
|
||||
class Report(db.Model):
|
||||
__tablename__ = DB_TABLE_PREFIX + 'reports'
|
||||
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
torrent = db.Column(db.Integer, db.ForeignKey(
|
||||
DB_TABLE_PREFIX + 'torrents.id'))
|
||||
user_id = db.Column(db.Integer, db.ForeignKey(
|
||||
'users.id'))
|
||||
created_time = db.Column(db.DateTime(timezone=False), default=datetime.utcnow)
|
||||
reason = db.Column(db.String(length=255), nullable=False)
|
||||
status = db.Column(ChoiceType(ReportStatus, impl=db.Integer()), nullable=False)
|
||||
|
||||
def __init__(self, torrent, user_id, reason):
|
||||
self.torrent = torrent
|
||||
self.user_id = user_id
|
||||
self.reason = reason
|
||||
self.status = ReportStatus.IN_REVIEW
|
||||
|
||||
def __repr__(self):
|
||||
return '<Report %r>' % self.id
|
||||
|
||||
@property
|
||||
def created_utc_timestamp(self):
|
||||
''' Returns a UTC POSIX timestamp, as seconds '''
|
||||
return (self.created_time - UTC_EPOCH).total_seconds()
|
||||
|
||||
|
||||
# class Session(db.Model):
|
||||
# __tablename__ = 'sessions'
|
||||
#
|
||||
|
|
|
@ -578,9 +578,11 @@ def view_torrent(torrent_id):
|
|||
if torrent.filelist:
|
||||
files = json.loads(torrent.filelist.filelist_blob.decode('utf-8'))
|
||||
|
||||
report_form = forms.ReportForm()
|
||||
return flask.render_template('view.html', torrent=torrent,
|
||||
files=files,
|
||||
can_edit=can_edit)
|
||||
can_edit=can_edit,
|
||||
form=report_form)
|
||||
|
||||
|
||||
@app.route('/view/<int:torrent_id>/edit', methods=['GET', 'POST'])
|
||||
|
@ -663,6 +665,29 @@ def download_torrent(torrent_id):
|
|||
return resp
|
||||
|
||||
|
||||
@app.route('/view/<int:torrent_id>/submit_report', methods=['POST'])
|
||||
def submit_report(torrent_id):
|
||||
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:
|
||||
current_user_id = flask.g.user.id
|
||||
report = models.Report(
|
||||
torrent=torrent_id,
|
||||
user_id=current_user_id,
|
||||
reason=report_reason)
|
||||
|
||||
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))
|
||||
|
||||
|
||||
def _get_cached_torrent_file(torrent):
|
||||
# Note: obviously temporary
|
||||
cached_torrent = os.path.join(app.config['BASE_DIR'],
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
{% extends "layout.html" %}
|
||||
{% block title %}{{ torrent.display_name }} :: {{ config.SITE_NAME }}{% endblock %}
|
||||
{% block body %}
|
||||
{% from "_formhelpers.html" import render_field %}
|
||||
<div class="panel panel-{% if torrent.deleted %}deleted{% elif torrent.remake %}danger{% elif torrent.trusted %}success{% else %}default{% endif %}">
|
||||
<div class="panel-heading"{% if torrent.hidden %} style="background-color: darkgray;"{% endif %}>
|
||||
<h3 class="panel-title">
|
||||
|
@ -54,6 +55,8 @@
|
|||
</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>
|
||||
<div class="clearfix"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@ -119,6 +122,31 @@
|
|||
</div>
|
||||
{% endif %}
|
||||
|
||||
<div class="modal fade" id="reportModal" tabindex="-1" role="dialog" aria-labelledby="reportModalLabel">
|
||||
<div class="modal-dialog" role="document">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
|
||||
aria-hidden="true">×</span></button>
|
||||
<h4 class="modal-title">Report torrent #{{ torrent.id }}</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form method="POST" action="{{ request.url }}/submit_report">
|
||||
{{ form.csrf_token }}
|
||||
{{ render_field(form.reason, class_='form-control') }}
|
||||
<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>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
</div>
|
||||
<div class="modal-footer" style="border-top: none;">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
var target = document.getElementById('torrent-description');
|
||||
var text = target.innerHTML;
|
||||
|
|
Loading…
Reference in a new issue