diff --git a/nyaa/backend.py b/nyaa/backend.py index 0c20ecf..e777e23 100644 --- a/nyaa/backend.py +++ b/nyaa/backend.py @@ -45,6 +45,11 @@ def _replace_utf8_values(dict_or_list): def handle_torrent_upload(upload_form, uploading_user=None, fromAPI=False): torrent_data = upload_form.torrent_file.parsed_data + # Delete exisiting torrent which is marked as deleted + if torrent_data.db_id is not None: + models.Torrent.query.filter_by(id=torrent_data.db_id).delete() + db.session.commit() + # The torrent has been validated and is safe to access with ['foo'] etc - all relevant # keys and values have been checked for (see UploadForm in forms.py for details) info_dict = torrent_data.torrent_dict['info'] @@ -62,7 +67,8 @@ def handle_torrent_upload(upload_form, uploading_user=None, fromAPI=False): # In case no encoding, assume UTF-8. torrent_encoding = torrent_data.torrent_dict.get('encoding', b'utf-8').decode('utf-8') - torrent = models.Torrent(info_hash=torrent_data.info_hash, + torrent = models.Torrent(id=torrent_data.db_id, + info_hash=torrent_data.info_hash, display_name=display_name, torrent_name=torrent_data.filename, information=information, diff --git a/nyaa/forms.py b/nyaa/forms.py index 528f303..879dbd8 100644 --- a/nyaa/forms.py +++ b/nyaa/forms.py @@ -8,7 +8,7 @@ from flask_wtf.file import FileField, FileRequired from flask_wtf.recaptcha import RecaptchaField from flask_wtf.recaptcha.validators import Recaptcha as RecaptchaValidator from wtforms import (BooleanField, HiddenField, PasswordField, SelectField, StringField, - TextAreaField) + SubmitField, TextAreaField) from wtforms.validators import (DataRequired, Email, EqualTo, Length, Optional, Regexp, StopValidation, ValidationError) from wtforms.widgets import Select as SelectWidget # For DisabledSelectField @@ -186,6 +186,13 @@ class EditForm(FlaskForm): ]) +class DeleteForm(FlaskForm): + delete = SubmitField("Delete") + ban = SubmitField("Delete & Ban") + undelete = SubmitField("Undelete") + unban = SubmitField("Unban") + + class UploadForm(FlaskForm): torrent_file = FileField('Torrent file', [ FileRequired() @@ -281,14 +288,18 @@ class UploadForm(FlaskForm): # Check if the info_hash exists already in the database existing_torrent = models.Torrent.by_info_hash(info_hash) - if existing_torrent: - raise ValidationError('That torrent already exists (#{})'.format(existing_torrent.id)) + existing_torrent_id = existing_torrent.id if existing_torrent else None + if existing_torrent and not existing_torrent.deleted: + raise ValidationError('This torrent already exists (#{})'.format(existing_torrent.id)) + if existing_torrent and existing_torrent.banned: + raise ValidationError('This torrent is banned'.format(existing_torrent.id)) # Torrent is legit, pass original filename and dict along field.parsed_data = TorrentFileData(filename=os.path.basename(field.data.filename), torrent_dict=torrent_dict, info_hash=info_hash, - bencoded_info_dict=bencoded_info_dict) + bencoded_info_dict=bencoded_info_dict, + db_id=existing_torrent_id) class UserForm(FlaskForm): diff --git a/nyaa/models.py b/nyaa/models.py index 1f26cbc..bd14aed 100644 --- a/nyaa/models.py +++ b/nyaa/models.py @@ -98,6 +98,7 @@ class TorrentFlags(IntEnum): REMAKE = 8 COMPLETE = 16 DELETED = 32 + BANNED = 64 class TorrentBase(DeclarativeHelperBase): @@ -250,6 +251,7 @@ class TorrentBase(DeclarativeHelperBase): anonymous = FlagProperty(TorrentFlags.ANONYMOUS) hidden = FlagProperty(TorrentFlags.HIDDEN) deleted = FlagProperty(TorrentFlags.DELETED) + banned = FlagProperty(TorrentFlags.BANNED) trusted = FlagProperty(TorrentFlags.TRUSTED) remake = FlagProperty(TorrentFlags.REMAKE) complete = FlagProperty(TorrentFlags.COMPLETE) diff --git a/nyaa/templates/edit.html b/nyaa/templates/edit.html index adce139..3232499 100644 --- a/nyaa/templates/edit.html +++ b/nyaa/templates/edit.html @@ -15,7 +15,6 @@
+