joegasewicz / flask-file-upload Goto Github PK
View Code? Open in Web Editor NEWEasy file uploads for Flask.
License: MIT License
Easy file uploads for Flask.
License: MIT License
Setup Sphinx
Delete files only method would be more useful when the user wants to drop a row from the db containing the file data but afterwards needs to cleanup the files from the server.
save_files
should add updates to session as we require the primary key value for directory path on the server.
Perhaps the api could have an option set as default to auto commit, if False then let the user commit first manually.
Remove .idea dir from repo & update .gitignore
I'm not able to delete files due to the filename not being correct. It's showing as image.image/png but the real file I uploaded is called 'binder.png'. I use 'image' on the form as name='image' as well as in the model file and the dict when saving the file originally.
I updated my config to:
UPLOAD_FOLDER = Path.cwd().joinpath("smartHomeDevicesForDisabled", "smartify", "static", "uploads")
The error I'm getting when trying to delete:
INFO in routes: Device could not be deleted.
[2020-01-01 18:22:24,918] INFO in routes: [Errno 2] No such file or directory: '/home/laurin/Projects/smartHomeDevicesForDisabled/smartify/static/uploads/device/7/image.image/png'
How I'm trying to delete:
def deleteDevice(id):
try:
device = Device.query.get(id)
file_upload.delete_files(device, db=db, files=["image"])
db.session.delete(device)
db.session.commit()
flash('Device successfully deleted.', 'success')
app.logger.info('Device deleted.')
except Exception as e:
flash('Device could not be deleted.', 'danger')
app.logger.info('Device could not be deleted.')
app.logger.info(e)
return redirect(url_for('editDevices'))
How I'm originally saving the files:
device = Device(name=form.name.data,
description=form.description.data,
price=form.price.data,
recurring_price=form.recurring_price.data,
payment_occurence_id=po.id,
link=form.link.data,
category_id=dc.id,
rating=form.rating.data,
narrative=form.narrative.data
)
image = request.files["image"]
device = file_upload.save_files(device, files={
"image": image
})
Is this a bug or am I doing something wrong?
Explain file location on server, e.g.
perhaps generate a small diagram from terminal of dirs etc.
in ReadME & inline Docs.
Add a update_model_clean_up
public method.
The idea is that if the files on the server are updated or removed but for any reason the model didn't get updated the user can evoke this method in a try except block.
update_model_clean_up
will go and look in the dir related to the filenames
, check to see if the name has been changed or the files no longer exist and then updated the model and commit the session.
file_upload.update_model_clean_up(your_model, filenames=["my_video", "placeholder"])
File Url paths
file_upload.get_file_url(blog_post, filename="placeholder_img")
Get filename in database when streaming file content. This fix should result in a new function to retrieve *__file_name
We currently only have the standard / default id
representing a model's primary key. We should add an options when using our Api's model decorator:
@file_upload.Model(primary_key="user_id")
class ModelTest(db.Model):
...
my_placeholder = file_upload.Column(db)
my_video = file_upload.Column(db)
OR
@file_upload.Model
class ModelTest(db.Model):
...
my_placeholder = file_upload.Column(db, primary_key="user_id")
my_video = file_upload.Column(db) # We only need to define once on a model
Rollback for save_files
method. If there is an exception commiting the changes of the model to the db, then the user of this library should be able to undo the changes made to files on the server.
delete method needs the cleanup logic not to flow through to the main function body
update with tests
Add a update_files_clean_up
public method.
The idea is that if the model / table gets updated or removed but for any reason the files didn't get updated / removed.
update_files_clean_up
will check that the files are in sync with the current model state. If not, then the changes will be carried out on the files on the server to reflect the changes on the model. If the changes cannot be fixed then a warning should be logged
file_upload.update_files_clean_up(your_model, filenames={"my_video": my_video})
Documentation for Config class
Set up library Pypi, install twine, setup.py etc.
Add a method index (quick links FFU methods) t the top of the read me so users dont have to scroll all the way down to read a specific methods docs.
Update file name
file_upload.update_file_name(BlogPostModel, my_video, new_filename="new_name")
As the title states...
Multiple file uploads on a single attribute.
teacher_img = file_upload.Column(many=True)
Currently the tests rely on 2 media files. the tests/test_path/blogs/1/my_placeholder.png
is missing though.
Needs to check why this file isn't in this location on test set up.
Update files etc.
file_upload.update_files(BlogPostModel, files=[my_video])
Column doc strings
Add db
to the inline docs should be the 2nd argument).
directory with name of primary key doesn't get created..
So does that set the file size or the filename size? Because I can upload the image 'binder.png':
And it works great. But if I try to upload '8percenttrimmed_heightmap.png':
It gives me the error:
(pymysql.err.DataError) (1406, "Data too long for column 'image__file_name' at row 1")And to test it I changed the config to be:
MAX_CONTENT_LENGTH = 1000 * 1000 * 1024 * 1024
can you past the actual file name . this is actually a MySQL error saying that the string we save in the image__file_name
column is too long..
Originally posted by @joegasewicz in #46 (comment)
mimetype being set instead of file type when streaming media content
Unit test for get_primary_key
public method
If I try to save a file more than once on the server I get this Error message:
ValueError: read of closed file
This test should identify the MySQL string length bug & remove the need to declare the current default 1000 string length
Decide if the SQLAlchemy instance requires passing & where
e.g
Column,
FileUpload
Id attribute needs to come from _ModelUtils.get_primary_key
No such table error
This issue will resolve appending the relative file urls to entities with one to many relations, specifically relational back references where file metadata is present.
The Api will look like:
blogs = add_file_urls_to_models(
blogs,
filename="blog_image",
backref={
"name": "blog_news",
"filename": "blog_news_image",
}
)
Explain how the configuration can be set up without passing in all kwargs as these can be assigned to app.config
and we can just pass app
to FileUpload()
The work to do with working with the session needs to be consolidated to a single or method. This is because there still exists session logic that is not referencing the current session (it must call object_session
:
So to fix this issue , create a method in model utils class, and move any session logic from Fileupload class to this single point with a unit test(s).
Unit Tests for _ModelUtils
class.
This might be worth waiting until the public api is completed and the methods are stable..
Add changelog
Setup Travis CI
Add & configure Pylint to this repo.
Public method delete_files
sqlalchemy is giving me:
VARCHAR requires a length on dialect mysql
I'm assuming it is as simple as adding a length when connecting to flask sql alchemy. Could you please put this in a release soon?
Hello again, you fixed compatibility with MySQL - thanks!
However I am having a new problem. I believe I'm almost to a fully working solution with this last hurdle. The error I'm getting is:
INFO in routes: [FLASK_FILE_UPLOAD_ERROR]: Couldn't create file path: /static/uploads/device/9/binder.png
My configuration is:
UPLOAD_FOLDER = '/static/uploads'
ALLOWED_EXTENSIONS = ["jpg", "png"]
MAX_CONTENT_LENGTH = 1000 * 1000 * 1024 * 1024
In my models.py:
@file_upload.Model
class Device(db.Model):
image = file_upload.Column(db)
In my routes.py:
image = request.files["image"]
device = file_upload.save_files(device, files={
"image": image
})
init.py:
db = SQLAlchemy()
file_upload = FileUpload(db=db)
def create_app():
#create and configure the app
app = Flask(__name__, instance_relative_config=True,
template_folder="templates", static_folder="static")
app.config.from_envvar('APP_CONFIG_FILE')
db.init_app(app)
file_upload.init_app(app)
tox set up for Python 3.6, 3.7, 3.8
We should be able to pass in SqlAlchemy's session object instead of the full SqlAlchemy engine object and perform updates and commits to the session.
pipenv dependencies
TypeError If backref is not set calling add_file_urls_to_models
Currently Flask-FileUpload only interfaces with Flask-SQLAlchemy.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.