mirror of
https://gitlab.com/SIGBUS/nyaa.git
synced 2025-01-02 15:45:40 +00:00
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.
This commit is contained in:
parent
18bdf465f7
commit
3190394eea
|
@ -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)
|
||||
|
|
|
@ -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 '<UserPreferences %r>' % 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'
|
||||
|
||||
|
|
|
@ -25,6 +25,9 @@
|
|||
<li role="presentation">
|
||||
<a href="#email-change" id="email-change-tab" role="tab" data-toggle="tab" aria-controls="profile" aria-expanded="false">Email</a>
|
||||
</li>
|
||||
<li role="presentation">
|
||||
<a href="#preferences-change" id="preferences-change-tab" role="tab" data-toggle="tab" aria-controls="profile" aria-expanded="false">Preferences</a>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
<div class="tab-content">
|
||||
|
@ -48,7 +51,7 @@
|
|||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
<input type="submit" value="Update" class="btn btn-primary">
|
||||
{{ form.authorized_submit(class_='btn btn-primary') }}
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
@ -74,7 +77,27 @@
|
|||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
<input type="submit" value="Update" class="btn btn-primary">
|
||||
{{ form.authorized_submit(class_='btn btn-primary') }}
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div class="tab-pane fade" role="tabpanel" id="preferences-change" aria-labelledby="preferences-change-tab">
|
||||
<form method="POST">
|
||||
{{ form.csrf_token }}
|
||||
<div class="row">
|
||||
<div class="form-group col-md-4">
|
||||
{% if g.user.preferences.hide_comments %}
|
||||
{{ form.hide_comments(checked='') }}
|
||||
{% else %}
|
||||
{{ form.hide_comments }}
|
||||
{% endif %}
|
||||
{{ form.hide_comments.label }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
{{ form.submit_settings(class_='btn btn-primary') }}
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
|
|
@ -136,10 +136,13 @@
|
|||
|
||||
<div id="comments" class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
<a data-toggle="collapse" href="#collapse-comments" role="button" aria-expanded="{% if g.user and g.user.preferences.hide_comments %}false{% else %}true{% endif %}" aria-controls="collapse-comments">
|
||||
<h3 class="panel-title">
|
||||
Comments - {{ comments | length }}
|
||||
</h3>
|
||||
</a>
|
||||
</div>
|
||||
<div class="collapse {% if g.user and g.user.preferences.hide_comments %}{% else %}in{% endif %}" id="collapse-comments">
|
||||
{% for comment in comments %}
|
||||
<div class="panel panel-default comment-panel" id="com-{{ loop.index }}">
|
||||
<div class="panel-body">
|
||||
|
@ -248,6 +251,7 @@
|
|||
</form>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% if g.user and g.user.age > config['RATELIMIT_ACCOUNT_AGE'] %}
|
||||
<div class="modal fade" id="reportModal" tabindex="-1" role="dialog" aria-labelledby="reportModalLabel">
|
||||
|
|
|
@ -178,13 +178,13 @@ def profile():
|
|||
|
||||
form = forms.ProfileForm(flask.request.form)
|
||||
|
||||
if flask.request.method == 'POST' and form.validate():
|
||||
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:
|
||||
flask.flash(flask.Markup(
|
||||
'<strong>Email change failed!</strong> Incorrect password.'), 'danger')
|
||||
|
@ -192,6 +192,7 @@ def profile():
|
|||
user.email = form.email.data
|
||||
flask.flash(flask.Markup(
|
||||
'<strong>Email successfully changed!</strong>'), 'success')
|
||||
|
||||
if new_password:
|
||||
if form.current_password.data != user.password_hash:
|
||||
flask.flash(flask.Markup(
|
||||
|
@ -200,10 +201,22 @@ def profile():
|
|||
user.password_hash = form.new_password.data
|
||||
flask.flash(flask.Markup(
|
||||
'<strong>Password successfully changed!</strong>'), '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(
|
||||
'<strong>Preferences successfully changed!</strong>'), 'success')
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
flask.g.user = user
|
||||
return flask.redirect('/profile')
|
||||
|
||||
|
|
Loading…
Reference in a new issue