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

PEP8 api_handler.py (E501)

E501 line too long
This commit is contained in:
sharkykh 2017-05-14 10:51:13 +03:00 committed by Kfir Hadas
parent b0325bc681
commit a3be6ee89f

View file

@ -112,17 +112,22 @@ def api_upload(upload_request):
if DEBUG_API: if DEBUG_API:
print(json.dumps(j, indent=4)) print(json.dumps(j, indent=4))
_json_keys = ['username', 'password', _json_keys = ['username',
'display_name', 'main_cat', 'sub_cat', 'flags'] # 'information' and 'description' are not required 'password',
'display_name',
'main_cat',
'sub_cat',
'flags'] # 'information' and 'description' are not required
# Check that required fields are present # Check that required fields are present
for _k in _json_keys: for _k in _json_keys:
if _k not in j.keys(): if _k not in j.keys():
return flask.make_response(flask.jsonify({"Error": "Missing JSON field: {0}.".format(_k)}), 400) return flask.make_response(flask.jsonify(
{"Error": "Missing JSON field: {0}.".format(_k)}), 400)
# Check that no extra fields are present # Check that no extra fields are present
for k in j.keys(): for k in j.keys():
if k not in ['username', 'password', if k not in set(_json_keys + ['information', 'description']):
'display_name', 'main_cat', 'sub_cat', 'information', 'description', 'flags']: return flask.make_response(flask.jsonify(
return flask.make_response(flask.jsonify({"Error": "Incorrect JSON field(s)."}), 400) {"Error": "Incorrect JSON field(s)."}), 400)
else: else:
return flask.make_response(flask.jsonify({"Error": "No metadata."}), 400) return flask.make_response(flask.jsonify({"Error": "No metadata."}), 400)
if 'torrent' in upload_request.files: if 'torrent' in upload_request.files:
@ -143,14 +148,17 @@ def api_upload(upload_request):
if not user: if not user:
user = models.User.by_email(username) user = models.User.by_email(username)
if not user or password != user.password_hash or user.status == models.UserStatusType.INACTIVE: if (not user or password != user.password_hash
return flask.make_response(flask.jsonify({"Error": "Incorrect username or password"}), 403) or user.status == models.UserStatusType.INACTIVE):
return flask.make_response(flask.jsonify(
{"Error": "Incorrect username or password"}), 403)
current_user = user current_user = user
display_name = j['display_name'] display_name = j['display_name']
if (len(display_name) < 3) or (len(display_name) > 1024): if (len(display_name) < 3) or (len(display_name) > 1024):
return flask.make_response(flask.jsonify({"Error": "Torrent name must be between 3 and 1024 characters."}), 400) return flask.make_response(flask.jsonify(
{"Error": "Torrent name must be between 3 and 1024 characters."}), 400)
main_cat_name = j['main_cat'] main_cat_name = j['main_cat']
sub_cat_name = j['sub_cat'] sub_cat_name = j['sub_cat']
@ -158,14 +166,16 @@ def api_upload(upload_request):
cat_subcat_status, cat_id, sub_cat_id = validate_main_sub_cat( cat_subcat_status, cat_id, sub_cat_id = validate_main_sub_cat(
main_cat_name, sub_cat_name) main_cat_name, sub_cat_name)
if not cat_subcat_status: if not cat_subcat_status:
return flask.make_response(flask.jsonify({"Error": "Incorrect Category / Sub-Category."}), 400) return flask.make_response(flask.jsonify(
{"Error": "Incorrect Category / Sub-Category."}), 400)
# TODO Sanitize information # TODO Sanitize information
information = None information = None
try: try:
information = j['information'] information = j['information']
if len(information) > 255: if len(information) > 255:
return flask.make_response(flask.jsonify({"Error": "Information is limited to 255 characters."}), 400) return flask.make_response(flask.jsonify(
{"Error": "Information is limited to 255 characters."}), 400)
except Exception as e: except Exception as e:
information = '' information = ''
@ -173,8 +183,10 @@ def api_upload(upload_request):
description = None description = None
try: try:
description = j['description'] description = j['description']
if len(description) > (10 * 1024): limit = 10 * 1024
return flask.make_response(flask.jsonify({"Error": "Description is limited to {0} characters.".format(10 * 1024)}), 403) if len(description) > limit:
return flask.make_response(flask.jsonify(
{"Error": "Description is limited to {0} characters.".format(limit)}), 403)
except Exception as e: except Exception as e:
description = '' description = ''
@ -182,13 +194,15 @@ def api_upload(upload_request):
if v_flags: if v_flags:
torrent_flags = j['flags'] torrent_flags = j['flags']
else: else:
return flask.make_response(flask.jsonify({"Error": "Incorrect torrent flags."}), 400) return flask.make_response(flask.jsonify(
{"Error": "Incorrect torrent flags."}), 400)
torrent_status, torrent_data = validate_torrent_file( torrent_status, torrent_data = validate_torrent_file(
torrent_file.filename, torrent_file.read()) # Needs validation torrent_file.filename, torrent_file.read()) # Needs validation
if not torrent_status: if not torrent_status:
return flask.make_response(flask.jsonify({"Error": "Invalid or Duplicate torrent file."}), 400) return flask.make_response(flask.jsonify(
{"Error": "Invalid or Duplicate torrent file."}), 400)
# The torrent has been validated and is safe to access with ['foo'] etc - all relevant # 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) # keys and values have been checked for (see UploadForm in forms.py for details)
@ -310,9 +324,11 @@ def api_upload(upload_request):
torrent_file.close() torrent_file.close()
# print('Success? {0}'.format(torrent.id)) # print('Success? {0}'.format(torrent.id))
return flask.make_response(flask.jsonify({"Success": "Request was processed {0}".format(torrent.id)}), 200) return flask.make_response(flask.jsonify(
{"Success": "Request was processed {0}".format(torrent.id)}), 200)
except Exception as e: except Exception as e:
print('Exception: {0}'.format(e)) print('Exception: {0}'.format(e))
return flask.make_response(flask.jsonify({"Error": "Incorrect JSON. Please see HELP page for examples."}), 400) return flask.make_response(flask.jsonify(
{"Error": "Incorrect JSON. Please see HELP page for examples."}), 400)
else: else:
return flask.make_response(flask.jsonify({"Error": "Bad request"}), 400) return flask.make_response(flask.jsonify({"Error": "Bad request"}), 400)