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 %}