1
0
Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-12-22 15:19:59 +00:00

Merge pull request #101 from nyaadevs/show_edit_user_info

Show edit user info
This commit is contained in:
Yukikaze 2017-05-17 06:20:33 -07:00 committed by GitHub
commit 4bb9828291
6 changed files with 141 additions and 5 deletions

View file

@ -259,6 +259,14 @@ class UploadForm(FlaskForm):
bencoded_info_dict=bencoded_info_dict)
class UserForm(FlaskForm):
user_class = DisabledSelectField('Change User Class')
def validate_user_class(form, field):
if not field.data:
raise ValidationError('Please select a proper user class')
class TorrentFileData(object):
"""Quick and dirty class to pass data from the validator"""

View file

@ -361,6 +361,15 @@ class User(db.Model):
def is_admin(self):
return self.level is UserLevelType.ADMIN or self.level is UserLevelType.SUPERADMIN
@property
def is_superadmin(self):
return self.level is UserLevelType.SUPERADMIN
@property
def is_trusted(self):
return self.level is UserLevelType.TRUSTED
# class Session(db.Model):
# __tablename__ = 'sessions'
#

View file

@ -206,13 +206,42 @@ def home(rss):
rss_filter=rss_query_string)
@app.route('/user/<user_name>')
@app.route('/user/<user_name>', methods=['GET', 'POST'])
def view_user(user_name):
user = models.User.by_username(user_name)
if not user:
flask.abort(404)
if flask.g.user and flask.g.user.id != user.id:
admin = flask.g.user.is_admin
superadmin = flask.g.user.is_superadmin
else:
admin = False
superadmin = False
form = forms.UserForm()
form.user_class.choices = _create_user_class_choices()
if flask.request.method == 'POST' and form.validate():
selection = form.user_class.data
if selection == 'regular':
user.level = models.UserLevelType.REGULAR
elif selection == 'trusted':
user.level = models.UserLevelType.TRUSTED
db.session.add(user)
db.session.commit()
return flask.redirect('/user/' + user.username)
level = 'Regular'
if user.is_admin:
level = 'Moderator'
if user.is_superadmin: # check this second because user can be admin AND superadmin
level = 'Administrator'
elif user.is_trusted:
level = 'Trusted'
term = flask.request.args.get('q')
sort = flask.request.args.get('s')
order = flask.request.args.get('o')
@ -273,7 +302,11 @@ def view_user(user_name):
search=query_args,
user=user,
user_page=True,
rss_filter=rss_query_string)
rss_filter=rss_query_string,
level=level,
admin=admin,
superadmin=superadmin,
form=form)
# Similar logic as home page
else:
if use_elastic:
@ -287,7 +320,11 @@ def view_user(user_name):
search=query_args,
user=user,
user_page=True,
rss_filter=rss_query_string)
rss_filter=rss_query_string,
level=level,
admin=admin,
superadmin=superadmin,
form=form)
@app.template_filter('rfc822')
@ -400,6 +437,15 @@ def profile():
return flask.redirect('/') # so we dont get stuck in infinite loop when signing out
form = forms.ProfileForm(flask.request.form)
level = 'Regular'
if flask.g.user.is_admin:
level = 'Moderator'
if flask.g.user.is_superadmin: # check this second because we can be admin AND superadmin
level = 'Administrator'
elif flask.g.user.is_trusted:
level = 'Trusted'
if flask.request.method == 'POST' and form.validate():
user = flask.g.user
new_email = form.email.data.strip()
@ -431,7 +477,7 @@ def profile():
current_email = models.User.by_id(flask.g.user.id).email
return flask.render_template('profile.html', form=form, email=current_email)
return flask.render_template('profile.html', form=form, email=current_email, level=level)
@app.route('/user/activate/<payload>')
@ -631,6 +677,13 @@ def send_verification_email(to_address, activ_link):
server.quit()
def _create_user_class_choices():
choices = [('regular', 'Regular')]
if flask.g.user and flask.g.user.is_superadmin:
choices.append(('trusted', 'Trusted'))
return choices
# #################################### STATIC PAGES ####################################
@app.route('/rules', methods=['GET'])
def site_rules():

View file

@ -91,3 +91,30 @@
{% endif %}
</div>
{% endmacro %}
{% macro render_menu_with_button(field) %}
{% if field.errors %}
<div class="form-group has-error">
{% else %}
<div class="form-group">
{% endif %}
{{ field.label(class='control-label') }}
{{ field(title=field.description,**kwargs) | safe }}
<button type="submit" class="btn btn-primary">Apply</button>
{% if field.errors %}
<div class="help-block">
{% if field.errors|length < 2 %}
{% for error in field.errors %}
{{ error }}
{% endfor %}
{% else %}
<ul>
{% for error in field.errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
</div>
{% endif %}
</div>
{% endmacro %}

View file

@ -12,6 +12,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="#general-info" id="general-info-tab" role="tab" data-toggle="tab" aria-controls="profile" aria-expanded="false">My Info</a>
</li>
</ul>
<div class="tab-content">
<div class="tab-pane fade active in" role="tabpanel" id="password-change" aria-labelledby="password-change-tab">
@ -67,6 +70,16 @@
</div>
</form>
</div>
<div class="tab-pane fade" role="tabpanel" id="general-info" aria-labelledby="general-info-tab">
<dl class="dl-horizontal">
<dt>User ID:</dt>
<dd>{{g.user.id}}</dd>
<dt>Account created on:</dt>
<dd>{{g.user.created_time}}</dd>
<dt>User class:</dt>
<dd>{{level}}</dd><br>
</dl>
</div>
</div>
{% endblock %}

View file

@ -1,6 +1,32 @@
{% extends "layout.html" %}
{% block title %}{{ user.username }} :: {{ config.SITE_NAME }}{% endblock %}
{% block body %}
{% from "_formhelpers.html" import render_menu_with_button %}
{% if superadmin %}
<h2>User Information</h2><br>
<dl class="dl-horizontal">
<dt>User ID:</dt>
<dd>{{user.id}}</dd>
<dt>Account created on:</dt>
<dd>{{user.created_time}}</dd>
<dt>Email address:</dt>
<dd>{{user.email}}</dd>
<dt>User class:</dt>
<dd>{{level}}</dd><br>
</dl>
<form method="POST">
{{ form.csrf_token }}
<div class="form-group row">
<div class="col-md-6">
{{ render_menu_with_button(form.user_class)}}
</div>
</div>
</form>
<br>
{% endif %}
<h3>
Browsing {{user.username}}'s torrents
</h3>