{ "info": { "author": "Alec Nikolas Reiter", "author_email": "alecreiter@gmail.com", "bugtrack_url": null, "classifiers": [ "Development Status :: 3 - Alpha", "Environment :: Web Environment", "Framework :: Flask", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.4" ], "description": "\nFlask-Transfer\n==============\n\nValidate, process and persist file uploads easily through a single\nobject instead of cramming all that stuff into your routes.\n\nTired of this?\n--------------\n\n.. code:: python\n\n @app.route('/upload', methods=['GET', 'POST'])\n def handle_upload():\n form = FileUploadForm()\n \n if form.validate_on_submit():\n filehandle = form.uploaded.data\n allowed_exts = ('md', 'txt', 'rst')\n if not os.path.splitext(filehandle.filename)[1] in allowed_exts:\n raise SomeError('Unallowed extension!')\n if filehandle.read() != b'Hello World!':\n raise SomeError('File contents not allowed!')\n filehandle.seek(0)\n \n username = g.current_user.name\n upload_dir = current_app.config['UPLOAD_DIR']\n full_path = os.path.join(upload_dir, username, secure_filename(filehandle.filename))\n filehandle.save(full_path)\n flash(\"Uploaded {}!\".format(filehandle.filename), 'success')\n return redirect(url_for('handle_upload'))\n else:\n return render_template('upload.html', form=form)\n\nThat's a mess. Your test runner is literally running away from you.\nThere's like four or five different things going on in this single\nroute! Argh.\n\nThere's a better way\n--------------------\n\n.. code:: python\n\n from flask_transfer import Transfer, UploadError\n from flask_transfer.validators import AllowedExts\n \n TextFileTransfer = Transfer(validators=[AllowedExts('md', 'rst', 'txt')])\n \n @TextFileTransfer.destination\n def save_to_user_dir(filehandle, metadata):\n username = g.current_user.name\n upload_path = current_app.config['UPLOAD_DIR']\n full_path = os.path.join(upload_dir, username, secure_filename(filehandle.filename))\n filehandle.save(full_path)\n \n \n @TextFileTransfer.validator\n def check_file_contents(filehandle, metadata):\n if filehandle.read() != metadata['allowed_contents']:\n raise UploadError('File contents not allowed!')\n filehandle.seek(0)\n return True\n \n \n @app.route('/upload', methods=['GET', 'POST'])\n def handle_upload():\n form = FileUploadForm()\n \n if form.validate_on_submit():\n filehandle = form.uploaded.data\n TextFileTransfer.save(filehandle, metadata={'allowed_contents': b'Hello World!'})\n flash('Uploaded {}!'.format(filehandle.filename), 'success')\n return redirect(url_for('handle_upload'))\n else:\n return render_template('upload.html', form=form)\n\nAaaah. Sure, it's a little bit more code. But it's separated out into\nbits and pieces. It's easy to test each bit and the intent in the route\nis very clear.\n\nMore Power\n----------\n\nFlask-Transfer supplies hooks for validation, preprocessing and\npostprocessing file uploads via decorators. If you need to always create\nthumbnails of uploaded images, you can supply a callable to\n``MyTransfer.preprocessor`` or ``MyTransfer.postprocessor`` that'll do\nthat for you.\n\nAnd validation beyond just simple extension checking is at your\nfingertips as well. Perhaps, you've limited your user to a certain\namount of disk space and they should be told to delete data before\nuploading more. Write a simple function to check current disk usage and\nif the upload would exceed the cap. Then hook it to your Transfer object\nwith ``MyTransfer.validator``.\n\nFinally, persisting files is easy! Maybe you're running on Heroku and\ncan't rely on the local filesystem. Just write a callable that'll pass\nthe file to your S3 bucket! Hook it in with ``MyTransfer.destination``.\nFlask-Transfer handles using string paths and writable objects as\ndestinations as well.\n\nCheck out the `quickstart `__ for some more information,\nas well!\n\nTodo\n----\n\nThere's still quite a bit to do. For example, better error handle.\nPerhaps a tighter integration with Flask, or running the opposite way\nand cleaving the already few dependencies on werkzeug to become\nframework independent.\n\nContributions\n-------------\n\nGiven the infancy of this project, pull requests and issue are more than\nwelcome. Just add yourself to the authors file, write some tests for the\nadded or change functionality and submit it!\n", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/justanr/Flask-Transfer", "keywords": "flask,uploads", "license": "MIT", "maintainer": null, "maintainer_email": null, "name": "flask-transfer", "package_url": "https://pypi.org/project/flask-transfer/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/flask-transfer/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/justanr/Flask-Transfer" }, "release_url": "https://pypi.org/project/flask-transfer/0.1.0/", "requires_dist": null, "requires_python": null, "summary": "Validate and process file uploads in Flask easily", "version": "0.1.0" }, "last_serial": 1816297, "releases": { "0.0.1": [ { "comment_text": "", "digests": { "md5": "1e8586bad2bd8a38b18985a53ff6700b", "sha256": "f7838af5d191555acb3f08c5b745f85dbec57066dac76e1148e78503c297e074" }, "downloads": -1, "filename": "flask-transfer-0.0.1.tar.gz", "has_sig": false, "md5_digest": "1e8586bad2bd8a38b18985a53ff6700b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6468, "upload_time": "2015-06-16T17:31:18", "url": "https://files.pythonhosted.org/packages/c9/90/87e11f9019fdf1237f31794f20212e05aec8467e0e77fc5ba5a698e77312/flask-transfer-0.0.1.tar.gz" } ], "0.1.0": [ { "comment_text": "", "digests": { "md5": "302a95196089815bc6a1e900dbdec890", "sha256": "a5adab0fe558d54285142611e36af0c2e264637be5fc08a7d942672e5a8b1cf5" }, "downloads": -1, "filename": "flask-transfer-0.1.0.tar.gz", "has_sig": false, "md5_digest": "302a95196089815bc6a1e900dbdec890", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8308, "upload_time": "2015-11-14T15:15:00", "url": "https://files.pythonhosted.org/packages/f0/7d/55327a4332f93ee4dc4e7b99cf0d7210a0368227d56db31d278999ed418a/flask-transfer-0.1.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "302a95196089815bc6a1e900dbdec890", "sha256": "a5adab0fe558d54285142611e36af0c2e264637be5fc08a7d942672e5a8b1cf5" }, "downloads": -1, "filename": "flask-transfer-0.1.0.tar.gz", "has_sig": false, "md5_digest": "302a95196089815bc6a1e900dbdec890", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8308, "upload_time": "2015-11-14T15:15:00", "url": "https://files.pythonhosted.org/packages/f0/7d/55327a4332f93ee4dc4e7b99cf0d7210a0368227d56db31d278999ed418a/flask-transfer-0.1.0.tar.gz" } ] }