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

Added comments

This commit is contained in:
Sn0wCrack 2017-05-13 22:24:42 +10:00 committed by nyaadev
parent 1e230584ab
commit 6d608ab2f4
4 changed files with 108 additions and 1 deletions

View file

@ -126,6 +126,13 @@ class DisabledSelectField(SelectField):
raise ValueError(self.gettext('Not a valid choice')) raise ValueError(self.gettext('Not a valid choice'))
class CommentForm(FlaskForm):
comment = TextAreaField('Make a comment', [
Length(max=255, message='Comment must be at most %(max)d characters long.'),
Required()
])
class EditForm(FlaskForm): class EditForm(FlaskForm):
display_name = StringField('Torrent display name', [ display_name = StringField('Torrent display name', [
Length(min=3, max=255, Length(min=3, max=255,

View file

@ -317,6 +317,22 @@ class SubCategory(db.Model):
return cls.query.get((sub_cat_id, main_cat_id)) return cls.query.get((sub_cat_id, main_cat_id))
class Comment(db.Model):
__tablename__ = DB_TABLE_PREFIX + 'comments'
id = db.Column(db.Integer, primary_key=True)
torrent = db.Column(db.Integer, db.ForeignKey(
DB_TABLE_PREFIX + 'torrents.id'), primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey(
'users.id', ondelete='CASCADE'))
text = db.Column(db.String(length=255), nullable=False)
user = db.relationship('User', uselist=False, back_populates='comments')
def __repr__(self):
return '<Comment %r>' % self.id
class UserLevelType(IntEnum): class UserLevelType(IntEnum):
REGULAR = 0 REGULAR = 0
TRUSTED = 1 TRUSTED = 1
@ -346,7 +362,8 @@ class User(db.Model):
last_login_date = db.Column(db.DateTime(timezone=False), default=None, nullable=True) last_login_date = db.Column(db.DateTime(timezone=False), default=None, nullable=True)
last_login_ip = db.Column(db.Binary(length=16), default=None, nullable=True) last_login_ip = db.Column(db.Binary(length=16), default=None, nullable=True)
torrents = db.relationship('Torrent', back_populates='user', lazy="dynamic") torrents = db.relationship('Torrent', back_populates='user', lazy='dynamic')
comments = db.relationship('Comment', back_populates='user', lazy='dynamic')
# session = db.relationship('Session', uselist=False, back_populates='user') # session = db.relationship('Session', uselist=False, back_populates='user')
def __init__(self, username, email, password): def __init__(self, username, email, password):

View file

@ -573,6 +573,7 @@ def upload():
@app.route('/view/<int:torrent_id>') @app.route('/view/<int:torrent_id>')
def view_torrent(torrent_id): def view_torrent(torrent_id):
torrent = models.Torrent.by_id(torrent_id) torrent = models.Torrent.by_id(torrent_id)
form = forms.CommentForm()
viewer = flask.g.user viewer = flask.g.user
@ -590,12 +591,43 @@ def view_torrent(torrent_id):
if torrent.filelist: if torrent.filelist:
files = json.loads(torrent.filelist.filelist_blob.decode('utf-8')) files = json.loads(torrent.filelist.filelist_blob.decode('utf-8'))
if flask.g.user is not None and flask.g.user.is_admin:
comments = models.Comment.query.filter(models.Comment.torrent == torrent_id)
else:
comments = models.Comment.query.filter(models.Comment.torrent == torrent_id,
models.Comment.deleted == False)
comment_count = comments.count()
return flask.render_template('view.html', torrent=torrent, return flask.render_template('view.html', torrent=torrent,
files=files, files=files,
viewer=viewer, viewer=viewer,
form=form,
comments=comments,
comment_count=comment_count,
can_edit=can_edit) can_edit=can_edit)
@app.route('/view/<int:torrent_id>/submit_comment', methods=['POST'])
def submit_comment(torrent_id):
form = forms.CommentForm(flask.request.form)
if flask.request.method == 'POST' and form.validate():
comment_text = (form.comment.data or '').strip()
# Null entry for User just means Anonymous
current_user_id = flask.g.user.id if flask.g.user else None
comment = models.Comment(
torrent=torrent_id,
user_id=current_user_id,
text=comment_text)
db.session.add(comment)
db.session.commit()
return flask.redirect(flask.url_for('view_torrent', torrent_id=torrent_id))
@app.route('/view/<int:torrent_id>/edit', methods=['GET', 'POST']) @app.route('/view/<int:torrent_id>/edit', methods=['GET', 'POST'])
def edit_torrent(torrent_id): def edit_torrent(torrent_id):
torrent = models.Torrent.by_id(torrent_id) torrent = models.Torrent.by_id(torrent_id)

View file

@ -1,6 +1,7 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% block title %}{{ torrent.display_name }} :: {{ config.SITE_NAME }}{% endblock %} {% block title %}{{ torrent.display_name }} :: {{ config.SITE_NAME }}{% endblock %}
{% block body %} {% 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 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 %}> <div class="panel-heading"{% if torrent.hidden %} style="background-color: darkgray;"{% endif %}>
<h3 class="panel-title"> <h3 class="panel-title">
@ -129,6 +130,56 @@
</div> </div>
{% endif %} {% endif %}
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
Comments - {{ comment_count }}
</h3>
</div>
<div class="panel-collapse">
<table class="table table-bordered table-striped">
<thead>
{% if g.user.is_admin %}
<th style="width:auto;">Delete</th>
{% endif %}
<th style="width:auto;">User</th>
<th style="width:100%;">Comment</th>
</thead>
<tbody>
{% for comment in comments %}
<tr>
{% if g.user.is_admin %}
<td class="col-md-1">
{% if not comment.deleted %}
<a href="/"><i class="fa fa-ban"></i></a>
{% else %}
<a href="/"><i class="fa fa-circle-o"></i></a>
{% endif %}
</td>
{% endif %}
<td class="col-md-1">
{% if comment.user %}
<a href="{{ url_for('view_user', user_name=comment.user.username) }}">
{{ comment.user.username }}
</a>
{% else %}
<span>Anonymous</span>
{% endif %}
</td>
<td class="col-md-10">{{ comment.text }}</td>
</tr>
{% endfor %}
<tbody>
</table>
</div>
</div>
<form method="POST" action="{{ request.url }}/submit_comment">
{{ form.csrf_token }}
{{ render_field(form.comment, class_='form-control') }}
<input type="submit" value="Submit" class="btn btn-primary">
</form>
<script> <script>
var target = document.getElementById('torrent-description'); var target = document.getElementById('torrent-description');
var text = target.innerHTML; var text = target.innerHTML;