mirror of
				https://github.com/sb745/NyaaV3.git
				synced 2025-10-30 23:45:46 +02:00 
			
		
		
		
	pep8
This commit is contained in:
		
							parent
							
								
									e554f9ae97
								
							
						
					
					
						commit
						2a1f8fab87
					
				
					 5 changed files with 43 additions and 36 deletions
				
			
		|  | @ -9,12 +9,12 @@ from nyaa import torrents | |||
| import functools | ||||
| import json | ||||
| import os.path | ||||
| #from orderedset import OrderedSet | ||||
| #from werkzeug import secure_filename | ||||
| 
 | ||||
| api_blueprint = flask.Blueprint('api', __name__) | ||||
| 
 | ||||
| # #################################### API HELPERS #################################### | ||||
| 
 | ||||
| 
 | ||||
| def basic_auth_user(f): | ||||
|     ''' A decorator that will try to validate the user into g.user from basic auth. | ||||
|         Note: this does not set user to None on failure, so users can also authorize | ||||
|  | @ -30,6 +30,7 @@ def basic_auth_user(f): | |||
|         return f(*args, **kwargs) | ||||
|     return decorator | ||||
| 
 | ||||
| 
 | ||||
| def api_require_user(f): | ||||
|     ''' Returns an error message if flask.g.user is None. | ||||
|         Remember to put after basic_auth_user. ''' | ||||
|  | @ -40,6 +41,7 @@ def api_require_user(f): | |||
|         return f(*args, **kwargs) | ||||
|     return decorator | ||||
| 
 | ||||
| 
 | ||||
| def validate_user(upload_request): | ||||
|     auth_info = None | ||||
|     try: | ||||
|  | @ -55,7 +57,8 @@ def validate_user(upload_request): | |||
|             if not user: | ||||
|                 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 or \ | ||||
|                     user.status == models.UserStatusType.INACTIVE: | ||||
|                 return False, None, None | ||||
| 
 | ||||
|             return True, user, None | ||||
|  | @ -85,23 +88,21 @@ def api_upload(upload_request, user): | |||
|         form_info_as_dict = [] | ||||
|         for k, v in form_info.items(): | ||||
|             if k in ['is_anonymous', 'is_hidden', 'is_remake', 'is_complete']: | ||||
|                 if v == True: | ||||
|                 if v: | ||||
|                     form_info_as_dict.append((k, v)) | ||||
|             else: | ||||
|                 form_info_as_dict.append((k, v)) | ||||
|         form_info = ImmutableMultiDict(form_info_as_dict) | ||||
| 
 | ||||
|         # print(repr(form_info)) | ||||
|     except Exception as e: | ||||
|         return flask.make_response(flask.jsonify({'Failure': ['Invalid data. See HELP in api_uploader.py']}), 400) | ||||
|         return flask.make_response(flask.jsonify( | ||||
|             {'Failure': ['Invalid data. See HELP in api_uploader.py']}), 400) | ||||
| 
 | ||||
|     try: | ||||
|         torrent_file = upload_request.files['torrent_file'] | ||||
|         torrent_file = ImmutableMultiDict([('torrent_file', torrent_file)]) | ||||
| 
 | ||||
|         # print(repr(torrent_file)) | ||||
|     except Exception as e: | ||||
|         return flask.make_response(flask.jsonify({'Failure': ['No torrent file was attached.']}), 400) | ||||
|         return flask.make_response(flask.jsonify( | ||||
|             {'Failure': ['No torrent file was attached.']}), 400) | ||||
| 
 | ||||
|     form = forms.UploadForm(CombinedMultiDict((torrent_file, form_info))) | ||||
|     form.category.choices = _create_upload_category_choices() | ||||
|  | @ -111,16 +112,15 @@ def api_upload(upload_request, user): | |||
| 
 | ||||
|         return flask.make_response(flask.jsonify({'Success': int('{0}'.format(torrent.id))}), 200) | ||||
|     else: | ||||
|         # print(form.errors) | ||||
|         return_error_messages = [] | ||||
|         for error_name, error_messages in form.errors.items(): | ||||
|             # print(error_messages) | ||||
|             return_error_messages.extend(error_messages) | ||||
| 
 | ||||
|         return flask.make_response(flask.jsonify({'Failure': return_error_messages}), 400) | ||||
| 
 | ||||
| # V2 below | ||||
| 
 | ||||
| 
 | ||||
| # Map UploadForm fields to API keys | ||||
| UPLOAD_API_FORM_KEYMAP = { | ||||
|     'torrent_file': 'torrent', | ||||
|  | @ -144,6 +144,7 @@ UPLOAD_API_KEYS = [ | |||
|     'description' | ||||
| ] | ||||
| 
 | ||||
| 
 | ||||
| @api_blueprint.route('/v2/upload', methods=['POST']) | ||||
| @basic_auth_user | ||||
| @api_require_user | ||||
|  |  | |||
|  | @ -71,7 +71,8 @@ def handle_torrent_upload(upload_form, uploading_user=None, fromAPI=False): | |||
|     torrent.trusted = (uploading_user.level >= | ||||
|                        models.UserLevelType.TRUSTED) if uploading_user else False | ||||
|     # Set category ids | ||||
|     torrent.main_category_id, torrent.sub_category_id = upload_form.category.parsed_data.get_category_ids() | ||||
|     torrent.main_category_id, torrent.sub_category_id = \ | ||||
|         upload_form.category.parsed_data.get_category_ids() | ||||
| 
 | ||||
|     # To simplify parsing the filelist, turn single-file torrent into a list | ||||
|     torrent_filelist = info_dict.get('files') | ||||
|  |  | |||
|  | @ -7,7 +7,8 @@ import re | |||
| from flask_wtf import FlaskForm | ||||
| from flask_wtf.file import FileField, FileRequired | ||||
| from wtforms import StringField, PasswordField, BooleanField, TextAreaField, SelectField | ||||
| from wtforms.validators import DataRequired, Optional, Email, Length, EqualTo, ValidationError, Regexp | ||||
| from wtforms.validators import DataRequired, Optional, Email, Length, EqualTo, ValidationError | ||||
| from wtforms.validators import Regexp | ||||
| 
 | ||||
| # For DisabledSelectField | ||||
| from wtforms.widgets import Select as SelectWidget | ||||
|  |  | |||
|  | @ -116,6 +116,7 @@ def get_utc_timestamp(datetime_str): | |||
| def get_display_time(datetime_str): | ||||
|     return datetime.strptime(datetime_str, '%Y-%m-%dT%H:%M:%S').strftime('%Y-%m-%d %H:%M') | ||||
| 
 | ||||
| 
 | ||||
| @utils.cached_function | ||||
| def get_category_id_map(): | ||||
|     ''' Reads database for categories and turns them into a dict with | ||||
|  | @ -131,8 +132,10 @@ def get_category_id_map(): | |||
| 
 | ||||
| # Routes start here # | ||||
| 
 | ||||
| 
 | ||||
| app.register_blueprint(api_handler.api_blueprint, url_prefix='/api') | ||||
| 
 | ||||
| 
 | ||||
| @app.route('/rss', defaults={'rss': True}) | ||||
| @app.route('/', defaults={'rss': False}) | ||||
| def home(rss): | ||||
|  | @ -501,7 +504,8 @@ def profile(): | |||
|     username = _user.username | ||||
|     current_email = _user.email | ||||
| 
 | ||||
|     return flask.render_template('profile.html', form=form, name=username, email=current_email, level=level) | ||||
|     return flask.render_template('profile.html', form=form, name=username, email=current_email, | ||||
|                                  level=level) | ||||
| 
 | ||||
| 
 | ||||
| @app.route('/user/activate/<payload>') | ||||
|  | @ -543,8 +547,8 @@ def _create_upload_category_choices(): | |||
| @app.route('/upload', methods=['GET', 'POST']) | ||||
| def upload(): | ||||
|     form = forms.UploadForm(CombinedMultiDict((flask.request.files, flask.request.form))) | ||||
|     #print('{0} - {1}'.format(flask.request.files, flask.request.form)) | ||||
|     form.category.choices = _create_upload_category_choices() | ||||
| 
 | ||||
|     if flask.request.method == 'POST' and form.validate(): | ||||
|         torrent = backend.handle_torrent_upload(form, flask.g.user) | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		
		Reference in a new issue
	
	 nyaadev
						nyaadev