diff --git a/nyaa/forms.py b/nyaa/forms.py index 292fb8f..97726ab 100644 --- a/nyaa/forms.py +++ b/nyaa/forms.py @@ -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""" diff --git a/nyaa/models.py b/nyaa/models.py index 038b13f..9ae3597 100644 --- a/nyaa/models.py +++ b/nyaa/models.py @@ -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' # diff --git a/nyaa/routes.py b/nyaa/routes.py index 1423296..274e4df 100644 --- a/nyaa/routes.py +++ b/nyaa/routes.py @@ -206,13 +206,42 @@ def home(rss): rss_filter=rss_query_string) -@app.route('/user/') +@app.route('/user/', 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/') @@ -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(): diff --git a/nyaa/templates/_formhelpers.html b/nyaa/templates/_formhelpers.html index 2ae1d4d..a84581e 100644 --- a/nyaa/templates/_formhelpers.html +++ b/nyaa/templates/_formhelpers.html @@ -91,3 +91,30 @@ {% endif %} {% endmacro %} + +{% macro render_menu_with_button(field) %} +{% if field.errors %} +
+{% else %} +
+{% endif %} + {{ field.label(class='control-label') }} + {{ field(title=field.description,**kwargs) | safe }} + + {% if field.errors %} +
+ {% if field.errors|length < 2 %} + {% for error in field.errors %} + {{ error }} + {% endfor %} + {% else %} +
    + {% for error in field.errors %} +
  • {{ error }}
  • + {% endfor %} +
+ {% endif %} +
+ {% endif %} +
+{% endmacro %} \ No newline at end of file diff --git a/nyaa/templates/profile.html b/nyaa/templates/profile.html index 8c84cbf..f07d0a5 100644 --- a/nyaa/templates/profile.html +++ b/nyaa/templates/profile.html @@ -12,6 +12,9 @@
  • +
  • + +
  • @@ -67,6 +70,16 @@
    +
    +
    +
    User ID:
    +
    {{g.user.id}}
    +
    Account created on:
    +
    {{g.user.created_time}}
    +
    User class:
    +
    {{level}}

    +
    +
    {% endblock %} diff --git a/nyaa/templates/user.html b/nyaa/templates/user.html index bcf48f7..cf9a0eb 100644 --- a/nyaa/templates/user.html +++ b/nyaa/templates/user.html @@ -1,8 +1,34 @@ {% extends "layout.html" %} {% block title %}{{ user.username }} :: {{ config.SITE_NAME }}{% endblock %} {% block body %} +{% from "_formhelpers.html" import render_menu_with_button %} + +{% if superadmin %} +

    User Information


    +
    +
    User ID:
    +
    {{user.id}}
    +
    Account created on:
    +
    {{user.created_time}}
    +
    Email address:
    +
    {{user.email}}
    +
    User class:
    +
    {{level}}

    +
    +
    + {{ form.csrf_token }} + +
    +
    + {{ render_menu_with_button(form.user_class)}} +
    +
    +
    +
    +{% endif %} +

    - Browsing {{user.username}}'s torrents + Browsing {{user.username}}'s torrents

    {% include "search_results.html" %}