From 3190394eea2a12c288b88aa0ea8c4028a67176f4 Mon Sep 17 00:00:00 2001 From: doge Date: Tue, 31 Jul 2018 12:29:39 -0500 Subject: [PATCH] Comment Hiding This commit adds the ability hide comments on torrent pages, and a user preferences tab to allow registered users to make this the default behavior. --- nyaa/forms.py | 4 +++ nyaa/models.py | 19 ++++++++++++ nyaa/templates/profile.html | 47 ++++++++++++++++++++-------- nyaa/templates/view.html | 4 +++ nyaa/views/account.py | 61 ++++++++++++++++++++++--------------- 5 files changed, 99 insertions(+), 36 deletions(-) diff --git a/nyaa/forms.py b/nyaa/forms.py index 649ad50..d9e2620 100644 --- a/nyaa/forms.py +++ b/nyaa/forms.py @@ -204,6 +204,10 @@ class ProfileForm(FlaskForm): ]) password_confirm = PasswordField('Repeat New Password') + hide_comments = BooleanField('Hide comments by default') + + authorized_submit = SubmitField('Update') + submit_settings = SubmitField('Update') # Classes for a SelectField that can be set to disable options (id, name, disabled) diff --git a/nyaa/models.py b/nyaa/models.py index 2075575..a5e4ed8 100644 --- a/nyaa/models.py +++ b/nyaa/models.py @@ -498,6 +498,9 @@ class User(db.Model): bans = db.relationship('Ban', uselist=True, foreign_keys='Ban.user_id') + preferences = db.relationship('UserPreferences', + back_populates='user', uselist=False, lazy='joined') + def __init__(self, username, email, password): self.username = username self.email = email @@ -641,6 +644,22 @@ class User(db.Model): return (self.created_time - UTC_EPOCH).total_seconds() +class UserPreferences(db.Model): + __tablename__ = 'user_preferences' + + id = db.Column(db.Integer, primary_key=True) + user_id = db.Column(db.Integer, db.ForeignKey('users.id'), unique=True, nullable=False) + + def __init__(self, user_id): + self.user_id = user_id + + def __repr__(self): + return '' % self.id + + user = db.relationship('User', back_populates='preferences') + hide_comments = db.Column(db.Boolean, nullable=False, default=False) + + class AdminLogBase(DeclarativeHelperBase): __tablename_base__ = 'adminlog' diff --git a/nyaa/templates/profile.html b/nyaa/templates/profile.html index dcba274..157d3b7 100644 --- a/nyaa/templates/profile.html +++ b/nyaa/templates/profile.html @@ -6,16 +6,16 @@

Profile of {{ g.user.username }}

-
- -
-
-
-
User ID:
{{ g.user.id }}
-
User Class:
{{ g.user.userlevel_str }}
-
User Created on:
{{ g.user.created_time }}
-
-
+
+ +
+
+
+
User ID:
{{ g.user.id }}
+
User Class:
{{ g.user.userlevel_str }}
+
User Created on:
{{ g.user.created_time }}
+
+
@@ -48,7 +51,7 @@
- + {{ form.authorized_submit(class_='btn btn-primary') }}
@@ -74,11 +77,31 @@
- + {{ form.authorized_submit(class_='btn btn-primary') }}
+
+
+ {{ form.csrf_token }} +
+
+ {% if g.user.preferences.hide_comments %} + {{ form.hide_comments(checked='') }} + {% else %} + {{ form.hide_comments }} + {% endif %} + {{ form.hide_comments.label }} +
+
+
+
+ {{ form.submit_settings(class_='btn btn-primary') }} +
+
+
+

diff --git a/nyaa/templates/view.html b/nyaa/templates/view.html index 57ac6ae..bc41bd1 100644 --- a/nyaa/templates/view.html +++ b/nyaa/templates/view.html @@ -136,10 +136,13 @@
+
{% for comment in comments %}
@@ -247,6 +250,7 @@
{% endif %} +
{% if g.user and g.user.age > config['RATELIMIT_ACCOUNT_AGE'] %} diff --git a/nyaa/views/account.py b/nyaa/views/account.py index 9479252..6f02c06 100644 --- a/nyaa/views/account.py +++ b/nyaa/views/account.py @@ -178,34 +178,47 @@ def profile(): form = forms.ProfileForm(flask.request.form) - if flask.request.method == 'POST' and form.validate(): - user = flask.g.user - new_email = form.email.data.strip() - new_password = form.new_password.data + if flask.request.method == 'POST': + if form.authorized_submit and form.validate(): + user = flask.g.user + new_email = form.email.data.strip() + new_password = form.new_password.data - if new_email: - # enforce password check on email change too - if form.current_password.data != user.password_hash: + if new_email: + if form.current_password.data != user.password_hash: + flask.flash(flask.Markup( + 'Email change failed! Incorrect password.'), 'danger') + return flask.redirect('/profile') + user.email = form.email.data flask.flash(flask.Markup( - 'Email change failed! Incorrect password.'), 'danger') - return flask.redirect('/profile') - user.email = form.email.data - flask.flash(flask.Markup( - 'Email successfully changed!'), 'success') - if new_password: - if form.current_password.data != user.password_hash: + 'Email successfully changed!'), 'success') + + if new_password: + if form.current_password.data != user.password_hash: + flask.flash(flask.Markup( + 'Password change failed! Incorrect password.'), 'danger') + return flask.redirect('/profile') + user.password_hash = form.new_password.data flask.flash(flask.Markup( - 'Password change failed! Incorrect password.'), 'danger') - return flask.redirect('/profile') - user.password_hash = form.new_password.data + 'Password successfully changed!'), 'success') + db.session.add(user) + db.session.commit() + flask.g.user = user + return flask.redirect('/profile') + + elif form.submit_settings: + user = flask.g.user + if user.preferences is None: + preferences = models.UserPreferences(user.id) + db.session.add(preferences) + db.session.commit() + user.preferences.hide_comments = form.hide_comments.data flask.flash(flask.Markup( - 'Password successfully changed!'), 'success') - - db.session.add(user) - db.session.commit() - - flask.g.user = user - return flask.redirect('/profile') + 'Preferences successfully changed!'), 'success') + db.session.add(user) + db.session.commit() + flask.g.user = user + return flask.redirect('/profile') return flask.render_template('profile.html', form=form)