24
24
# POSSIBILITY OF SUCH DAMAGE.
25
25
from __future__ import annotations
26
26
27
- __all__ = ("PyMongo" , "ASCENDING" , "DESCENDING" )
27
+ __all__ = ("PyMongo" , "ASCENDING" , "DESCENDING" , "BSONObjectIdConverter" , "BSONProvider" )
28
28
29
29
import hashlib
30
30
from mimetypes import guess_type
31
+ from typing import Any
31
32
32
33
import pymongo
33
- from flask import abort , current_app , request
34
+ from flask import Flask , Response , abort , current_app , request
34
35
from gridfs import GridFS , NoFile
35
36
from pymongo import uri_parser
37
+ from pymongo .driver_info import DriverInfo
36
38
from werkzeug .wsgi import wrap_file
37
39
38
- # DriverInfo was added in PyMongo 3.7
39
- try :
40
- from pymongo .driver_info import DriverInfo
41
- except ImportError :
42
- DriverInfo = None
43
-
44
40
from flask_pymongo ._version import __version__
45
41
from flask_pymongo .helpers import BSONObjectIdConverter , BSONProvider
46
- from flask_pymongo .wrappers import MongoClient
42
+ from flask_pymongo .wrappers import Database , MongoClient
47
43
48
44
DESCENDING = pymongo .DESCENDING
49
45
"""Descending sort order."""
@@ -65,15 +61,16 @@ class PyMongo:
65
61
66
62
"""
67
63
68
- def __init__ (self , app = None , uri = None , * args , ** kwargs ):
69
- self .cx = None
70
- self .db = None
71
- self ._json_provider = BSONProvider (app )
64
+ def __init__ (
65
+ self , app : Flask | None = None , uri : str | None = None , * args : Any , ** kwargs : Any
66
+ ) -> None :
67
+ self .cx : MongoClient | None = None
68
+ self .db : Database | None = None
72
69
73
70
if app is not None :
74
71
self .init_app (app , uri , * args , ** kwargs )
75
72
76
- def init_app (self , app , uri = None , * args , ** kwargs ) :
73
+ def init_app (self , app : Flask , uri : str | None = None , * args : Any , ** kwargs : Any ) -> None :
77
74
"""Initialize this :class:`PyMongo` for use.
78
75
79
76
Configure a :class:`~pymongo.mongo_client.MongoClient`
@@ -122,10 +119,12 @@ def init_app(self, app, uri=None, *args, **kwargs):
122
119
self .db = self .cx [database_name ]
123
120
124
121
app .url_map .converters ["ObjectId" ] = BSONObjectIdConverter
125
- app .json = self . _json_provider
122
+ app .json = BSONProvider ( app )
126
123
127
124
# view helpers
128
- def send_file (self , filename , base = "fs" , version = - 1 , cache_for = 31536000 ):
125
+ def send_file (
126
+ self , filename : str , base : str = "fs" , version : int = - 1 , cache_for : int = 31536000
127
+ ) -> Response :
129
128
"""Respond with a file from GridFS.
130
129
131
130
Returns an instance of the :attr:`~flask.Flask.response_class`
@@ -153,6 +152,7 @@ def get_upload(filename):
153
152
if not isinstance (cache_for , int ):
154
153
raise TypeError ("'cache_for' must be an integer" )
155
154
155
+ assert self .db is not None , "Please initialize the app before calling send_file!"
156
156
storage = GridFS (self .db , base )
157
157
158
158
try :
@@ -183,7 +183,14 @@ def get_upload(filename):
183
183
response .make_conditional (request )
184
184
return response
185
185
186
- def save_file (self , filename , fileobj , base = "fs" , content_type = None , ** kwargs ):
186
+ def save_file (
187
+ self ,
188
+ filename : str ,
189
+ fileobj : Any ,
190
+ base : str = "fs" ,
191
+ content_type : str | None = None ,
192
+ ** kwargs : Any ,
193
+ ) -> Any :
187
194
"""Save a file-like object to GridFS using the given filename.
188
195
Return the "_id" of the created file.
189
196
@@ -211,8 +218,7 @@ def save_upload(filename):
211
218
if content_type is None :
212
219
content_type , _ = guess_type (filename )
213
220
221
+ assert self .db is not None , "Please initialize the app before calling save_file!"
214
222
storage = GridFS (self .db , base )
215
- id = storage .put (
216
- fileobj , filename = filename , content_type = content_type , ** kwargs
217
- )
223
+ id = storage .put (fileobj , filename = filename , content_type = content_type , ** kwargs )
218
224
return id
0 commit comments