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:
doge 2018-07-31 12:29:39 -05:00 committed by Nicolas F
parent 18bdf465f7
commit 3190394eea
5 changed files with 99 additions and 36 deletions

View File

@ -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)

View File

@ -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'

View File

@ -6,16 +6,16 @@
<h2 style="margin-bottom: 20px;">Profile of <strong class="text-{{ g.user.userlevel_color }}">{{ g.user.username }}</strong></h2>
<div class="row" style="margin-bottom: 20px;">
<div class="col-sm-2" style="max-width: 150px;">
<img class="avatar" src="{{ g.user.gravatar_url() }}">
</div>
<div class="col-sm-10">
<dl class="row" style="margin: 20px 0 15px 0;">
<dt class="col-sm-2">User ID:</dt><dd class="col-sm-10">{{ g.user.id }}</dd>
<dt class="col-sm-2">User Class:</dt><dd class="col-sm-10">{{ g.user.userlevel_str }}</dd>
<dt class="col-sm-2">User Created on:</dt><dd class="col-sm-10">{{ g.user.created_time }}</dd>
</dl>
</div>
<div class="col-sm-2" style="max-width: 150px;">
<img class="avatar" src="{{ g.user.gravatar_url() }}">
</div>
<div class="col-sm-10">
<dl class="row" style="margin: 20px 0 15px 0;">
<dt class="col-sm-2">User ID:</dt><dd class="col-sm-10">{{ g.user.id }}</dd>
<dt class="col-sm-2">User Class:</dt><dd class="col-sm-10">{{ g.user.userlevel_str }}</dd>
<dt class="col-sm-2">User Created on:</dt><dd class="col-sm-10">{{ g.user.created_time }}</dd>
</dl>
</div>
</div>
<ul class="nav nav-tabs" id="profileTabs" role="tablist">
@ -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,11 +77,31 @@
</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>
</div>
</div>
<hr>

View File

@ -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">
@ -247,6 +250,7 @@
</div>
</form>
{% endif %}
</div>
</div>
{% if g.user and g.user.age > config['RATELIMIT_ACCOUNT_AGE'] %}

View File

@ -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(
'<strong>Email change failed!</strong> Incorrect password.'), 'danger')
return flask.redirect('/profile')
user.email = form.email.data
flask.flash(flask.Markup(
'<strong>Email change failed!</strong> Incorrect password.'), 'danger')
return flask.redirect('/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:
'<strong>Email successfully changed!</strong>'), 'success')
if new_password:
if form.current_password.data != user.password_hash:
flask.flash(flask.Markup(
'<strong>Password change failed!</strong> Incorrect password.'), 'danger')
return flask.redirect('/profile')
user.password_hash = form.new_password.data
flask.flash(flask.Markup(
'<strong>Password change failed!</strong> Incorrect password.'), 'danger')
return flask.redirect('/profile')
user.password_hash = form.new_password.data
'<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>Password successfully changed!</strong>'), 'success')
db.session.add(user)
db.session.commit()
flask.g.user = user
return flask.redirect('/profile')
'<strong>Preferences successfully changed!</strong>'), 'success')
db.session.add(user)
db.session.commit()
flask.g.user = user
return flask.redirect('/profile')
return flask.render_template('profile.html', form=form)