From 2ccf23a1f3b5547c54bdf3d555216dbfa9f13fe0 Mon Sep 17 00:00:00 2001 From: TheAMM Date: Sat, 20 May 2017 21:56:22 +0300 Subject: [PATCH 1/4] Clean up models.User.level helpers --- nyaa/models.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nyaa/models.py b/nyaa/models.py index 70fa0a7..ca00e9a 100644 --- a/nyaa/models.py +++ b/nyaa/models.py @@ -382,15 +382,15 @@ class User(db.Model): @property def is_admin(self): - return self.level is UserLevelType.ADMIN or self.level is UserLevelType.SUPERADMIN + return self.level >= UserLevelType.ADMIN @property def is_superadmin(self): - return self.level is UserLevelType.SUPERADMIN + return self.level == UserLevelType.SUPERADMIN @property def is_trusted(self): - return self.level is UserLevelType.TRUSTED + return self.level >= UserLevelType.TRUSTED # class Session(db.Model): From b9d88e89604b2e4401ced78955d0720c34e34f07 Mon Sep 17 00:00:00 2001 From: TheAMM Date: Sat, 20 May 2017 22:00:45 +0300 Subject: [PATCH 2/4] Restructure upload.html and edit.html as well as route logic Rename variables and reformats user/admin logic Add an is_trusted field to upload and edit forms Restructure fields on upload and edit pages Add simple styling for checkboxes Add titles (mouseover) for checkboxes with crude explanations Show Anonymous checkbox during upload and check & disable it for guests Show Trusted checkbox for users at or above Trusted level Adjust description field rendering to show field label above it Add title (mouseover) for edit icon on torrent page Show uploader for admins on anonymous torrents Show uploader for admins when editing others' torrents --- nyaa/forms.py | 2 + nyaa/routes.py | 58 ++++++++++++-------- nyaa/templates/_formhelpers.html | 54 +++++++++--------- nyaa/templates/edit.html | 94 +++++++++++++++----------------- nyaa/templates/upload.html | 73 +++++++++++-------------- nyaa/templates/view.html | 11 +++- 6 files changed, 149 insertions(+), 143 deletions(-) diff --git a/nyaa/forms.py b/nyaa/forms.py index 0f0e073..d7cea26 100644 --- a/nyaa/forms.py +++ b/nyaa/forms.py @@ -153,6 +153,7 @@ class EditForm(FlaskForm): is_remake = BooleanField('Remake') is_anonymous = BooleanField('Anonymous') is_complete = BooleanField('Complete') + is_trusted = BooleanField('Trusted') information = StringField('Information', [ Length(max=255, message='Information must be at most %(max)d characters long.') @@ -200,6 +201,7 @@ class UploadForm(FlaskForm): is_remake = BooleanField('Remake') is_anonymous = BooleanField('Anonymous') is_complete = BooleanField('Complete') + is_trusted = BooleanField('Trusted') information = StringField('Information', [ Length(max=255, message='Information must be at most %(max)d characters long.') diff --git a/nyaa/routes.py b/nyaa/routes.py index 4bb4e8f..f41f591 100644 --- a/nyaa/routes.py +++ b/nyaa/routes.py @@ -275,7 +275,7 @@ def view_user(user_name): db.session.add(user) db.session.commit() - return flask.redirect('/user/' + user.username) + return flask.redirect(flask.url_for('view_user', user_name=user.username)) user_level = ['Regular', 'Trusted', 'Moderator', 'Administrator'][user.level] @@ -579,16 +579,17 @@ def upload(): def view_torrent(torrent_id): torrent = models.Torrent.by_id(torrent_id) + viewer = flask.g.user + if not torrent: flask.abort(404) - if torrent.deleted and (not flask.g.user or not flask.g.user.is_admin): + # Only allow admins see deleted torrents + if torrent.deleted and not (viewer and viewer.is_admin): flask.abort(404) - if flask.g.user: - can_edit = flask.g.user is torrent.user or flask.g.user.is_admin - else: - can_edit = False + # Only allow owners and admins to edit torrents + can_edit = viewer and (viewer is torrent.user or viewer.is_admin) files = None if torrent.filelist: @@ -596,6 +597,7 @@ def view_torrent(torrent_id): return flask.render_template('view.html', torrent=torrent, files=files, + viewer=viewer, can_edit=can_edit) @@ -604,15 +606,18 @@ def edit_torrent(torrent_id): torrent = models.Torrent.by_id(torrent_id) form = forms.EditForm(flask.request.form) form.category.choices = _create_upload_category_choices() - category = str(torrent.main_category_id) + "_" + str(torrent.sub_category_id) + + editor = flask.g.user if not torrent: flask.abort(404) - if torrent.deleted and (not flask.g.user or not flask.g.user.is_admin): + # Only allow admins edit deleted torrents + if torrent.deleted and not (editor and editor.is_admin): flask.abort(404) - if not flask.g.user or (flask.g.user is not torrent.user and not flask.g.user.is_admin): + # Only allow torrent owners or admins edit torrents + if not editor or not (editor is torrent.user or editor.is_admin): flask.abort(403) if flask.request.method == 'POST' and form.validate(): @@ -622,36 +627,43 @@ def edit_torrent(torrent_id): torrent.display_name = (form.display_name.data or '').strip() torrent.information = (form.information.data or '').strip() torrent.description = (form.description.data or '').strip() - if flask.g.user.is_admin: - torrent.deleted = form.is_deleted.data + torrent.hidden = form.is_hidden.data torrent.remake = form.is_remake.data torrent.complete = form.is_complete.data torrent.anonymous = form.is_anonymous.data + if editor.is_trusted: + torrent.trusted = form.is_trusted.data + if editor.is_admin: + torrent.deleted = form.is_deleted.data + db.session.commit() flask.flash(flask.Markup( 'Torrent has been successfully edited! Changes might take a few minutes to show up.'), 'info') - return flask.redirect('/view/' + str(torrent_id)) + return flask.redirect(flask.url_for('view_torrent', torrent_id=torrent.id)) else: - # Setup form with pre-formatted form. - form.category.data = category - form.display_name.data = torrent.display_name - form.information.data = torrent.information - form.description.data = torrent.description - form.is_hidden.data = torrent.hidden - if flask.g.user.is_admin: + if flask.request.method != 'POST': + # Fill form data only if the POST didn't fail + form.category.data = torrent.sub_category.id_as_string + form.display_name.data = torrent.display_name + form.information.data = torrent.information + form.description.data = torrent.description + + form.is_hidden.data = torrent.hidden + form.is_remake.data = torrent.remake + form.is_complete.data = torrent.complete + form.is_anonymous.data = torrent.anonymous + + form.is_trusted.data = torrent.trusted form.is_deleted.data = torrent.deleted - form.is_remake.data = torrent.remake - form.is_complete.data = torrent.complete - form.is_anonymous.data = torrent.anonymous return flask.render_template('edit.html', form=form, torrent=torrent, - admin=flask.g.user.is_admin) + editor=editor) @app.route('/view//magnet') diff --git a/nyaa/templates/_formhelpers.html b/nyaa/templates/_formhelpers.html index a84581e..2588d04 100644 --- a/nyaa/templates/_formhelpers.html +++ b/nyaa/templates/_formhelpers.html @@ -1,10 +1,12 @@ -{% macro render_field(field) %} +{% macro render_field(field, render_label=True) %} {% if field.errors %}
{% else %}
{% endif %} + {% if render_label %} {{ field.label(class='control-label') }} + {% endif %} {{ field(title=field.description,**kwargs) | safe }} {% if field.errors %}
@@ -27,33 +29,33 @@ {% macro render_markdown_editor(field, field_name='') %} {% if field.errors %} -
+
{% else %} -
+
{% endif %} -
- -
-
- {{ render_field(field, class_='form-control markdown-source') }} -
-
- {{ field.label(class='control-label') }} -
-
-
-
+
+ {{ field.label(class='control-label') }} + +
+
+ {{ render_field(field, False, class_='form-control markdown-source') }} +
+
+
+
+
+
{% endmacro %} diff --git a/nyaa/templates/edit.html b/nyaa/templates/edit.html index 6ea01d4..d35b752 100644 --- a/nyaa/templates/edit.html +++ b/nyaa/templates/edit.html @@ -4,79 +4,73 @@ {% from "_formhelpers.html" import render_field %} {% from "_formhelpers.html" import render_markdown_editor %} -

Edit Torrent

+{% set torrent_url = url_for('view_torrent', torrent_id=torrent.id) %} +

+ Edit Torrent #{{torrent.id}} + {% if (torrent.user != None) and (torrent.user != editor) %} + (by {{ torrent.user.username }}) + {% endif %} +

{{ form.csrf_token }} +
-
- {{ render_field(form.category, class_='form-control')}} +
+ {{ render_field(form.display_name, class_='form-control', placeholder='Display name') }} +
+
+ {{ render_field(form.category, class_='form-control')}}
-
-
- {{ render_field(form.display_name, class_='form-control', placeholder='Display name') }} +
+ {{ render_field(form.information, class_='form-control', placeholder='Your website or IRC channel') }}
-
+
+ +
+ {% if editor.is_admin %} + + {% endif %} -
-
- {{ render_field(form.information, class_='form-control', placeholder='Your website or IRC channel') }} -
-
- -
-
- {{ render_markdown_editor(form.description, field_name='description') }} -
-
- - {% if admin %} -
-
- -
-
- {% endif %} - -
-
-
-
- -
-
-
-
- -
-
-
-
- +
+ {{ render_markdown_editor(form.description, field_name='description') }}
diff --git a/nyaa/templates/upload.html b/nyaa/templates/upload.html index 0a49282..803fec4 100644 --- a/nyaa/templates/upload.html +++ b/nyaa/templates/upload.html @@ -16,68 +16,57 @@ {% if config.ENFORCE_MAIN_ANNOUNCE_URL %}

Important: Please include {{config.MAIN_ANNOUNCE_URL}} in your trackers

{% endif %}
-
- {{ render_upload(form.torrent_file, accept=".torrent") }} +
+ {{ render_upload(form.torrent_file, accept=".torrent") }}
-
-
- {{ render_field(form.category, class_='form-control')}} +
+ {{ render_field(form.display_name, class_='form-control', placeholder='Display name') }} +
+
+ {{ render_field(form.category, class_='form-control')}}
-
-
- {{ render_field(form.display_name, class_='form-control', placeholder='Display name') }} -
+
- -
-
+
+
{{ render_field(form.information, class_='form-control', placeholder='Your website or IRC channel') }}
-
- -
-
- {{ render_markdown_editor(form.description, field_name='description') }} -
-
- -
-
-
+
+
+ {{ render_markdown_editor(form.description, field_name='description') }}
diff --git a/nyaa/templates/view.html b/nyaa/templates/view.html index 6c835b8..cfc0530 100644 --- a/nyaa/templates/view.html +++ b/nyaa/templates/view.html @@ -5,7 +5,7 @@

{% if can_edit %} - + {% endif %} {{ torrent.display_name }}

@@ -23,7 +23,14 @@
Submitter:
-
{% if not torrent.anonymous and torrent.user %}{{ torrent.user.username }}{% else %}Anonymous{% endif %}
+
+ {% set user_url = torrent.user and url_for('view_user', user_name=torrent.user.username) %} + {%- if not torrent.anonymous and torrent.user -%} + {{ torrent.user.username }} + {%- else -%} + Anonymous {% if torrent.user and (viewer == torrent.user or viewer.is_admin) %}({{ torrent.user.username }}){% endif %} + {%- endif -%} +
Seeders:
{% if config.ENABLE_SHOW_STATS %}{{ torrent.stats.seed_count }}{% else %}Coming soon{% endif %}
From af0cca2f8c936cdde98f0db0d03ea80ae416e8ce Mon Sep 17 00:00:00 2001 From: TheAMM Date: Sat, 20 May 2017 22:50:40 +0300 Subject: [PATCH 3/4] Display full category names on upload/edit category lists --- nyaa/routes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nyaa/routes.py b/nyaa/routes.py index f41f591..23a68d2 100644 --- a/nyaa/routes.py +++ b/nyaa/routes.py @@ -555,7 +555,8 @@ def _create_upload_category_choices(): cat_names = id_map[key] is_main_cat = key.endswith('_0') - cat_name = is_main_cat and cat_names[0] or (' - ' + cat_names[1]) + # cat_name = is_main_cat and cat_names[0] or (' - ' + cat_names[1]) + cat_name = ' - '.join(cat_names) choices.append((key, cat_name, is_main_cat)) return choices From f31efcdaa1c6ad7e8898f2e93690f2c2f6573e66 Mon Sep 17 00:00:00 2001 From: TheAMM Date: Sat, 20 May 2017 22:50:49 +0300 Subject: [PATCH 4/4] Align fields better, move Anonymous as first flag --- nyaa/templates/edit.html | 4 ++-- nyaa/templates/upload.html | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/nyaa/templates/edit.html b/nyaa/templates/edit.html index d35b752..44dfd51 100644 --- a/nyaa/templates/edit.html +++ b/nyaa/templates/edit.html @@ -25,10 +25,10 @@
-
+
{{ render_field(form.information, class_='form-control', placeholder='Your website or IRC channel') }}
-
+
{% if editor.is_admin %} diff --git a/nyaa/templates/upload.html b/nyaa/templates/upload.html index 803fec4..778a37d 100644 --- a/nyaa/templates/upload.html +++ b/nyaa/templates/upload.html @@ -32,12 +32,16 @@
-
+
{{ render_field(form.information, class_='form-control', placeholder='Your website or IRC channel') }}
-
+
+ - {% if user.is_trusted %}