Skip to content

Commit 04106ce

Browse files
achawkinsdcrosta
authored andcommitted
Actually fixed style, added tests, and added docstring note
1 parent 0fb688d commit 04106ce

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

flask_pymongo/__init__.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,14 @@ def get_upload(filename):
214214
response.make_conditional(request)
215215
return response
216216

217-
def save_file(self, filename, fileobj, base="fs", content_type=None,
218-
**kwargs):
217+
def save_file(
218+
self,
219+
filename,
220+
fileobj,
221+
base="fs",
222+
content_type=None,
223+
**kwargs,
224+
):
219225
"""Save a file-like object to GridFS using the given filename.
220226
221227
.. code-block:: python
@@ -231,6 +237,7 @@ def save_upload(filename):
231237
:param str content_type: the MIME content-type of the file. If
232238
``None``, the content-type is guessed from the filename using
233239
:func:`~mimetypes.guess_type`
240+
:param **kwargs: extra attributes to be stored in the file's document
234241
"""
235242
if not isinstance(base, text_type):
236243
raise TypeError("'base' must be string or unicode")
@@ -241,5 +248,9 @@ def save_upload(filename):
241248
content_type, _ = guess_type(filename)
242249

243250
storage = GridFS(self.db, base)
244-
return storage.put(fileobj, filename=filename,
245-
content_type=content_type, **kwargs)
251+
return storage.put(
252+
fileobj,
253+
filename=filename,
254+
content_type=content_type,
255+
**kwargs,
256+
)

flask_pymongo/tests/test_gridfs.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from hashlib import md5
22
from io import BytesIO
33

4+
from bson.objectid import ObjectId
45
from gridfs import GridFS
56
from werkzeug.exceptions import NotFound
67
import pytest
@@ -38,6 +39,22 @@ def test_it_guesses_type_from_filename(self):
3839
gridfile = gridfs.find_one({"filename": "my-file.txt"})
3940
assert gridfile.content_type == "text/plain"
4041

42+
def test_it_saves_files_with_props(self):
43+
fileobj = BytesIO(b"these are the bytes")
44+
45+
self.mongo.save_file("my-file", fileobj, foo="bar")
46+
47+
gridfs = GridFS(self.mongo.db)
48+
gridfile = gridfs.find_one({"filename": "my-file"})
49+
assert gridfile.foo == "bar"
50+
51+
def test_it_returns_id(self):
52+
fileobj = BytesIO(b"these are the bytes")
53+
54+
_id = self.mongo.save_file("my-file", fileobj, foo="bar")
55+
56+
assert type(_id) is ObjectId
57+
4158

4259
class TestSendFile(GridFSCleanupMixin, FlaskPyMongoTest):
4360

0 commit comments

Comments
 (0)