From 6d608ab2f480b3778812bdaec4aa6cdf1e1b8d68 Mon Sep 17 00:00:00 2001 From: Sn0wCrack Date: Sat, 13 May 2017 22:24:42 +1000 Subject: [PATCH 1/9] Added comments --- nyaa/forms.py | 7 ++++++ nyaa/models.py | 19 ++++++++++++++- nyaa/routes.py | 32 +++++++++++++++++++++++++ nyaa/templates/view.html | 51 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 1 deletion(-) 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') }} + +
+ + {% endfor %}
-
+{% if g.user %} + {{ form.csrf_token }} {{ render_field(form.comment, class_='form-control') }} - {{ render_field(form.is_anonymous) }}
+{% endif %} {% endfor %} + {% if g.user %} +
+ {{ form.csrf_token }} + {{ render_field(form.comment, class_='form-control') }} + +
+ {% endif %} -{% if g.user %} -
- {{ form.csrf_token }} - {{ render_field(form.comment, class_='form-control') }} - -
-{% endif %} - {% endfor %} - {% if g.user %} -
- {{ form.csrf_token }} - {{ render_field(form.comment, class_='form-control') }} + {% if comment_form %} + + {{ comment_form.csrf_token }} + {{ render_field(comment_form.comment, class_='form-control') }}
{% endif %} diff --git a/nyaa/torrents.py b/nyaa/torrents.py index 648683f..3a466a9 100644 --- a/nyaa/torrents.py +++ b/nyaa/torrents.py @@ -14,6 +14,7 @@ USED_TRACKERS = OrderedSet() # Limit the amount of trackers added into .torrent files MAX_TRACKERS = 5 + def read_trackers_from_file(file_object): USED_TRACKERS.clear() From 3fc347d049f9d7839a440fa040c9a67395c45026 Mon Sep 17 00:00:00 2001 From: nyaadev Date: Tue, 23 May 2017 00:04:27 +0200 Subject: [PATCH 9/9] Move posting comments to view_torrent to fix displaying form errors. --- nyaa/routes.py | 66 +++++++++++++++++++--------------------- nyaa/templates/view.html | 2 +- 2 files changed, 33 insertions(+), 35 deletions(-) diff --git a/nyaa/routes.py b/nyaa/routes.py index ecc3df9..0168b2a 100644 --- a/nyaa/routes.py +++ b/nyaa/routes.py @@ -571,13 +571,16 @@ def upload(): return flask.render_template('upload.html', upload_form=upload_form), status_code -@app.route('/view/') +@app.route('/view/', methods=['GET', 'POST']) def view_torrent(torrent_id): - torrent = models.Torrent.query \ - .options(joinedload('filelist'), - joinedload('comments')) \ - .filter_by(id=torrent_id) \ - .first() + if flask.request.method == 'POST': + torrent = models.Torrent.by_id(torrent_id) + else: + torrent = models.Torrent.query \ + .options(joinedload('filelist'), + joinedload('comments')) \ + .filter_by(id=torrent_id) \ + .first() if not torrent: flask.abort(404) @@ -585,6 +588,29 @@ def view_torrent(torrent_id): if torrent.deleted and not (flask.g.user and flask.g.user.is_moderator): flask.abort(404) + comment_form = None + if flask.g.user: + comment_form = forms.CommentForm() + + if flask.request.method == 'POST': + if not flask.g.user: + flask.abort(403) + + if comment_form.validate(): + comment_text = (comment_form.comment.data or '').strip() + + comment = models.Comment( + torrent_id=torrent_id, + user_id=flask.g.user.id, + text=comment_text) + + db.session.add(comment) + db.session.commit() + + flask.flash('Comment successfully posted.', 'success') + + return flask.redirect(flask.url_for('view_torrent', torrent_id=torrent_id)) + # Only allow owners and admins to edit torrents can_edit = flask.g.user and (flask.g.user is torrent.user or flask.g.user.is_moderator) @@ -592,10 +618,6 @@ def view_torrent(torrent_id): if torrent.filelist: files = json.loads(torrent.filelist.filelist_blob.decode('utf-8')) - comment_form = None - if flask.g.user: - comment_form = forms.CommentForm() - return flask.render_template('view.html', torrent=torrent, files=files, comment_form=comment_form, @@ -603,30 +625,6 @@ def view_torrent(torrent_id): can_edit=can_edit) -@app.route('/view//comment', methods=['POST']) -def submit_comment(torrent_id): - if not flask.g.user: - flask.abort(403) - - torrent = models.Torrent.by_id(torrent_id) - if not torrent: - flask.abort(404) - - form = forms.CommentForm(flask.request.form) - if form.validate(): - comment_text = (form.comment.data or '').strip() - - comment = models.Comment( - torrent_id=torrent_id, - user_id=flask.g.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//comment//delete', methods=['POST']) def delete_comment(torrent_id, comment_id): if not flask.g.user: diff --git a/nyaa/templates/view.html b/nyaa/templates/view.html index a2d7c7e..3494703 100644 --- a/nyaa/templates/view.html +++ b/nyaa/templates/view.html @@ -176,7 +176,7 @@ {% endfor %} {% if comment_form %} -
+ {{ comment_form.csrf_token }} {{ render_field(comment_form.comment, class_='form-control') }}