diff --git a/nyaa/forms.py b/nyaa/forms.py index 1a61706..a440092 100644 --- a/nyaa/forms.py +++ b/nyaa/forms.py @@ -126,6 +126,13 @@ class DisabledSelectField(SelectField): 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): display_name = StringField('Torrent display name', [ Length(min=3, max=255, diff --git a/nyaa/models.py b/nyaa/models.py index 07f75da..1cb8824 100644 --- a/nyaa/models.py +++ b/nyaa/models.py @@ -317,6 +317,22 @@ class SubCategory(db.Model): 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 '' % self.id + + class UserLevelType(IntEnum): REGULAR = 0 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_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') def __init__(self, username, email, password): diff --git a/nyaa/routes.py b/nyaa/routes.py index cc2c508..bc43e4c 100644 --- a/nyaa/routes.py +++ b/nyaa/routes.py @@ -573,6 +573,7 @@ def upload(): @app.route('/view/') def view_torrent(torrent_id): torrent = models.Torrent.by_id(torrent_id) + form = forms.CommentForm() viewer = flask.g.user @@ -590,12 +591,43 @@ def view_torrent(torrent_id): if torrent.filelist: 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, files=files, viewer=viewer, + form=form, + comments=comments, + comment_count=comment_count, can_edit=can_edit) +@app.route('/view//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//edit', methods=['GET', 'POST']) def edit_torrent(torrent_id): torrent = models.Torrent.by_id(torrent_id) diff --git a/nyaa/templates/view.html b/nyaa/templates/view.html index e32fc7d..658402e 100644 --- a/nyaa/templates/view.html +++ b/nyaa/templates/view.html @@ -1,6 +1,7 @@ {% extends "layout.html" %} {% block title %}{{ torrent.display_name }} :: {{ config.SITE_NAME }}{% endblock %} {% block body %} +{% from "_formhelpers.html" import render_field %}

@@ -129,6 +130,56 @@

{% endif %} +
+
+

+ Comments - {{ comment_count }} +

+
+
+ + + {% if g.user.is_admin %} + + {% endif %} + + + + + {% for comment in comments %} + + {% if g.user.is_admin %} + + {% endif %} + + + + {% endfor %} + +
DeleteUserComment
+ {% if not comment.deleted %} + + {% else %} + + {% endif %} + + {% if comment.user %} + + {{ comment.user.username }} + + {% else %} + Anonymous + {% endif %} + {{ comment.text }}
+
+
+ +
+ {{ form.csrf_token }} + {{ render_field(form.comment, class_='form-control') }} + +
+