Skip to content

Commit 3bc2469

Browse files
authored
Merge pull request #116 from IThinkImOKAY/master
Fix README Syntax
2 parents b1d0ff3 + ec86341 commit 3bc2469

File tree

1 file changed

+91
-92
lines changed

1 file changed

+91
-92
lines changed

README.md

Lines changed: 91 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ pip install flask-file-upload==0.2.0-rc.1
2222
#### General Flask config options
2323
(Important: The below configuration variables need to be set before initiating `FileUpload`)
2424
````python
25-
# This is the directory that flask-file-upload saves files to. Make sure the UPLOAD_FOLDER is the same as Flasks's static_folder or a child. For example:
26-
app.config["UPLOAD_FOLDER"] = join(dirname(realpath(__file__)), "static/uploads")
27-
28-
# Other FLASK config varaibles ...
29-
app.config["ALLOWED_EXTENSIONS"] = ["jpg", "png", "mov", "mp4", "mpg"]
30-
app.config["MAX_CONTENT_LENGTH"] = 1000 * 1024 * 1024 # 1000mb
31-
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://localhost:5432/blog_db"
25+
# This is the directory that flask-file-upload saves files to. Make sure the UPLOAD_FOLDER is the same as Flasks's static_folder or a child. For example:
26+
app.config["UPLOAD_FOLDER"] = join(dirname(realpath(__file__)), "static/uploads")
27+
28+
# Other FLASK config varaibles ...
29+
app.config["ALLOWED_EXTENSIONS"] = ["jpg", "png", "mov", "mp4", "mpg"]
30+
app.config["MAX_CONTENT_LENGTH"] = 1000 * 1024 * 1024 # 1000mb
31+
app.config["SQLALCHEMY_DATABASE_URI"] = "postgresql://localhost:5432/blog_db"
3232
````
3333

3434
#### Setup
@@ -89,10 +89,10 @@ class blogModel(db.Model):
8989

9090
#### define files to be uploaded:
9191
````python
92-
# A common scenario could be a video with placeholder image.
93-
# So first lets grab the files from Flask's request object:
94-
my_video = request.files["my_video"]
95-
placeholder_img = request.files["placeholder_img"]
92+
# A common scenario could be a video with placeholder image.
93+
# So first lets grab the files from Flask's request object:
94+
my_video = request.files["my_video"]
95+
placeholder_img = request.files["placeholder_img"]
9696
````
9797

9898

@@ -102,14 +102,14 @@ name(s) defined in your SqlAlchemy model & values that are your files.
102102
For Example:
103103

104104
````python
105-
file_upload.add_files(blog_post, files={
106-
"my_video": my_video,
107-
"placeholder_img": placeholder_img,
108-
})
109-
110-
# Now commit the changes to your db
111-
db.session.add(blog_post)
112-
db.session.commit()
105+
file_upload.add_files(blog_post, files={
106+
"my_video": my_video,
107+
"placeholder_img": placeholder_img,
108+
})
109+
110+
# Now commit the changes to your db
111+
db.session.add(blog_post)
112+
db.session.commit()
113113
````
114114
It's always good practise to commit the changes to your db as close to the end
115115
of your view handlers as possible (we encourage you to use `add_files` over the `save_files`
@@ -120,20 +120,20 @@ the current session then use `file_upload.save_files` - this method is only reco
120120
if you are sure nothing else needs committing after you have added you files.
121121
For example:
122122
```python
123-
file_upload.save_files(blog_post, files={
124-
"my_video": my_video,
125-
"placeholder_img": placeholder_img,
126-
})
123+
file_upload.save_files(blog_post, files={
124+
"my_video": my_video,
125+
"placeholder_img": placeholder_img,
126+
})
127127
```
128128
##### If you followed the setup above you will see the following structure saved to your app:
129129
![FlaskFileUpload](assets/dir1.png?raw=true "Directory example")
130130

131131
#### Update files
132132
````python
133-
blog_post = file_upload.update_files(blog_post, files={
134-
"my_video": new_my_video,
135-
"placeholder_img": new_placeholder_img,
136-
})
133+
blog_post = file_upload.update_files(blog_post, files={
134+
"my_video": new_my_video,
135+
"placeholder_img": new_placeholder_img,
136+
})
137137
````
138138

139139

@@ -149,45 +149,45 @@ provide 2 types of clean up functionality:
149149
See [delete_files Docs](https://flask-file-upload.readthedocs.io/en/latest/file_upload.html#flask_file_upload.file_upload.FileUpload.delete_files)
150150
for more details
151151
````python
152-
# Example using a SqlAlchemy model with an appended
153-
# method that fetches a single `blog`
154-
blogModel = BlogModel()
155-
blog_results = blogModel.get_one()
156-
157-
# We pass the blog & files
158-
blog = file_upload.delete_files(blog_result, files=["my_video"])
159-
160-
# If parent kwarg is set to True then the root primary directory & all its contents will be removed.
161-
# The model will also get cleaned up by default unless set to `False`.
162-
blog_result = file_upload.delete_files(blog_result, parent=True, files=["my_video"])
152+
# Example using a SqlAlchemy model with an appended
153+
# method that fetches a single `blog`
154+
blogModel = BlogModel()
155+
blog_results = blogModel.get_one()
163156

157+
# We pass the blog & files
158+
blog = file_upload.delete_files(blog_result, files=["my_video"])
164159

165-
# If the kwarg `commit` is not set or set to True then the updates are persisted.
166-
# to the session. And therefore the session has been commited.
167-
blog = file_upload.delete_files(blog_result, files=["my_video"])
168-
169-
# Example of cleaning up files but not updating the model:
170-
blog = file_upload.delete_files(blog_result, files=["my_video"], clean_up="files")
160+
# If parent kwarg is set to True then the root primary directory & all its contents will be removed.
161+
# The model will also get cleaned up by default unless set to `False`.
162+
blog_result = file_upload.delete_files(blog_result, parent=True, files=["my_video"])
163+
164+
165+
# If the kwarg `commit` is not set or set to True then the updates are persisted.
166+
# to the session. And therefore the session has been commited.
167+
blog = file_upload.delete_files(blog_result, files=["my_video"])
168+
169+
# Example of cleaning up files but not updating the model:
170+
blog = file_upload.delete_files(blog_result, files=["my_video"], clean_up="files")
171171
````
172172

173173

174174
#### Stream a file
175175
````python
176-
file_upload.stream_file(blog_post, filename="my_video")
176+
file_upload.stream_file(blog_post, filename="my_video")
177177
````
178178

179179

180180
#### File Url paths
181181
````python
182-
file_upload.get_file_url(blog_post, filename="placeholder_img")
182+
file_upload.get_file_url(blog_post, filename="placeholder_img")
183183
````
184184

185185
Example for getting file urls from many objects:
186186
```python
187-
# If blogs_model are many blogs:
188-
for blog in blog_models:
189-
blog_image_url = file_upload.get_file_url(blog, filename="blog_image")
190-
setattr(blog, "blog_image", blog_image_url)
187+
# If blogs_model are many blogs:
188+
for blog in blog_models:
189+
blog_image_url = file_upload.get_file_url(blog, filename="blog_image")
190+
setattr(blog, "blog_image", blog_image_url)
191191
```
192192

193193
#### Set file paths to multiple objects - *Available in `0.1.0-rc.6` & `v0.1.0`*
@@ -210,19 +210,19 @@ a `_url` tag. e.g `blog_image` becomes `blog_image_url`
210210

211211
Example for many SQLAlchemy entity objects (*or rows in your table*)::
212212
```python
213-
@file_upload.Model
214-
class BlogModel(db.Model):
213+
@file_upload.Model
214+
class BlogModel(db.Model):
215215

216-
blog_image = file_upload.Column()
216+
blog_image = file_upload.Column()
217217
```
218218

219219
Now we can use the `file_upload.add_file_urls_to_models` to add file urls to
220220
each SQLAlchemy object. For example::
221221
```python
222-
blogs = add_file_urls_to_models(blogs, filenames="blog_image")
222+
blogs = add_file_urls_to_models(blogs, filenames="blog_image")
223223

224-
# Notice that we can get the file path `blog_image` + `_url`
225-
assert blogs[0].blog_image_url == "path/to/blogs/1/blog_image_url.png"
224+
# Notice that we can get the file path `blog_image` + `_url`
225+
assert blogs[0].blog_image_url == "path/to/blogs/1/blog_image_url.png"
226226
```
227227

228228
To set filename attributes to a a single or multiple SQLAlchemy parent models with backrefs
@@ -236,59 +236,58 @@ To use backrefs we need to declare a kwarg of `backref` & pass 2 keys:
236236

237237
For example::
238238
```python
239-
# Parent model
240-
@file_upload.Model
241-
class BlogModel(db.Model):
242-
# The backref:
243-
blog_news = db.relationship("BlogNewsModel", backref="blogs")
244-
blog_image = file_upload.Column()
245-
blog_video = file_upload.Column()
246-
247-
# Model that has a foreign key back up to `BlogModel
248-
@file_upload.Model
249-
class BlogNewsModel(db.Model):
250-
# The foreign key assigned to this model:
251-
blog_id = db.Column(db.Integer, db.ForeignKey("blogs.blog_id"))
252-
news_image = file_upload.Column()
253-
news_video = file_upload.Column()
239+
# Parent model
240+
@file_upload.Model
241+
class BlogModel(db.Model):
242+
# The backref:
243+
blog_news = db.relationship("BlogNewsModel", backref="blogs")
244+
blog_image = file_upload.Column()
245+
blog_video = file_upload.Column()
246+
247+
# Model that has a foreign key back up to `BlogModel
248+
@file_upload.Model
249+
class BlogNewsModel(db.Model):
250+
# The foreign key assigned to this model:
251+
blog_id = db.Column(db.Integer, db.ForeignKey("blogs.blog_id"))
252+
news_image = file_upload.Column()
253+
news_video = file_upload.Column()
254254
```
255255

256256
The kwarg `backref` keys represent the backref model or entity (in the above example
257257
this would be the `BlogNewsModel` which we have named `blog_news`. Example::
258258
```python
259-
blogs = add_file_urls_to_models(blogs, filenames=["blog_image, blog_video"],
260-
backref={
261-
"name": "blog_news",`
262-
"filenames": ["news_image", "news_video],
263-
})
259+
blogs = add_file_urls_to_models(blogs, filenames=["blog_image, blog_video"],
260+
backref={
261+
"name": "blog_news",`
262+
"filenames": ["news_image", "news_video],
263+
})
264264
```
265265

266266
WARNING: You must not set the relationship kwarg: `lazy="dynamic"`!
267267
If `backref` is set to *"dynamic"* then back-referenced entity's
268268
filenames will not get set. Example::
269269
```python
270-
# This will work
271-
blog_news = db.relationship("BlogNewsModel", backref="blog")
272-
273-
# this will NOT set filenames on your model class
274-
blog_news = db.relationship("BlogNewsModel", backref="blog", lazy="dynamic")
270+
# This will work
271+
blog_news = db.relationship("BlogNewsModel", backref="blog")
275272

273+
# this will NOT set filenames on your model class
274+
blog_news = db.relationship("BlogNewsModel", backref="blog", lazy="dynamic")
276275
```
277276

278277
### Running Flask-Migration After including Flask-File-Upload in your project
279278
The arguments below will also run if you're using vanilla Alembic.
280279
```bash
281-
export FLASK_APP=flask_app.py # Path to your Flask app
282-
283-
# with pip
284-
flask db stamp head
285-
flask db migrate
286-
flask db upgrade
287-
288-
# with pipenv
289-
pipenv run flask db stamp head
290-
pipenv run flask db migrate
291-
pipenv run flask db upgrade
280+
export FLASK_APP=flask_app.py # Path to your Flask app
281+
282+
# with pip
283+
flask db stamp head
284+
flask db migrate
285+
flask db upgrade
286+
287+
# with pipenv
288+
pipenv run flask db stamp head
289+
pipenv run flask db migrate
290+
pipenv run flask db upgrade
292291
```
293292

294293
### Upgrading from v0.1 to v0.2

0 commit comments

Comments
 (0)