-
Notifications
You must be signed in to change notification settings - Fork 175
/
__init__.py
201 lines (158 loc) · 7.89 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Copyright (c) 2011-2017, Dan Crosta
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
__all__ = ("PyMongo", "ASCENDING", "DESCENDING")
from functools import partial
from mimetypes import guess_type
from flask import abort, current_app, request
from gridfs import GridFS, NoFile
from pymongo import uri_parser
from werkzeug.wsgi import wrap_file
import pymongo
from flask_pymongo.helpers import BSONObjectIdConverter, JSONEncoder
from flask_pymongo.wrappers import MongoClient
DESCENDING = pymongo.DESCENDING
"""Descending sort order."""
ASCENDING = pymongo.ASCENDING
"""Ascending sort order."""
class PyMongo(object):
"""Manages MongoDB connections for your Flask app.
PyMongo objects provide access to the MongoDB server via the :attr:`db`
and :attr:`cx` attributes. You must either pass the :class:`~flask.Flask`
app to the constructor, or call :meth:`init_app`.
PyMongo accepts a MongoDB URI via the ``MONGO_URI`` Flask configuration
variable, or as an argument to the constructor or ``init_app``. See
:meth:`init_app` for more detail.
"""
def __init__(self, app=None, uri=None, json_options=None, *args, **kwargs):
self.cx = None
self.db = None
self._json_encoder = partial(JSONEncoder, json_options=json_options)
if app is not None:
self.init_app(app, uri, *args, **kwargs)
def init_app(self, app, uri=None, *args, **kwargs):
"""Initialize this :class:`PyMongo` for use.
Configure a :class:`~pymongo.mongo_client.MongoClient`
in the following scenarios:
1. If ``uri`` is not ``None``, pass the ``uri`` and any positional
or keyword arguments to :class:`~pymongo.mongo_client.MongoClient`
2. If ``uri`` is ``None``, and a Flask config variable named
``MONGO_URI`` exists, use that as the ``uri`` as above.
The caller is responsible for ensuring that additional positional
and keyword arguments result in a valid call.
.. versionchanged:: 2.2
The ``uri`` is no longer required to contain a database name. If it
does not, then the :attr:`db` attribute will be ``None``.
.. versionchanged:: 2.0
Flask-PyMongo no longer accepts many of the configuration variables
it did in previous versions. You must now use a MongoDB URI to
configure Flask-PyMongo.
"""
if uri is None:
uri = app.config.get("MONGO_URI", None)
if uri is not None:
args = tuple([uri] + list(args))
else:
raise ValueError(
"You must specify a URI or set the MONGO_URI Flask config variable",
)
parsed_uri = uri_parser.parse_uri(uri)
database_name = parsed_uri["database"]
# Try to delay connecting, in case the app is loaded before forking, per
# http://api.mongodb.com/python/current/faq.html#is-pymongo-fork-safe
kwargs.setdefault("connect", False)
self.cx = MongoClient(*args, **kwargs)
if database_name:
self.db = self.cx[database_name]
app.url_map.converters["ObjectId"] = BSONObjectIdConverter
app.json_encoder = self._json_encoder
# view helpers
def send_file(self, filename, base="fs", version=-1, cache_for=31536000):
"""Respond with a file from GridFS.
Returns an instance of the :attr:`~flask.Flask.response_class`
containing the named file, and implement conditional GET semantics
(using :meth:`~werkzeug.wrappers.ETagResponseMixin.make_conditional`).
.. code-block:: python
@app.route("/uploads/<path:filename>")
def get_upload(filename):
return mongo.send_file(filename)
:param str filename: the filename of the file to return
:param str base: the base name of the GridFS collections to use
:param bool version: if positive, return the Nth revision of the file
identified by filename; if negative, return the Nth most recent
revision. If no such version exists, return with HTTP status 404.
:param int cache_for: number of seconds that browsers should be
instructed to cache responses
"""
if not isinstance(base, str):
raise TypeError("'base' must be string or unicode")
if not isinstance(version, int):
raise TypeError("'version' must be an integer")
if not isinstance(cache_for, int):
raise TypeError("'cache_for' must be an integer")
storage = GridFS(self.db, base)
try:
fileobj = storage.get_version(filename=filename, version=version)
except NoFile:
abort(404)
# mostly copied from flask/helpers.py, with
# modifications for GridFS
data = wrap_file(request.environ, fileobj, buffer_size=1024 * 255)
response = current_app.response_class(
data,
mimetype=fileobj.content_type,
direct_passthrough=True,
)
response.content_length = fileobj.length
response.last_modified = fileobj.upload_date
response.set_etag(fileobj.md5)
response.cache_control.max_age = cache_for
response.cache_control.public = True
response.make_conditional(request)
return response
def save_file(self, filename, fileobj, base="fs", content_type=None, **kwargs):
"""Save a file-like object to GridFS using the given filename.
.. code-block:: python
@app.route("/uploads/<path:filename>", methods=["POST"])
def save_upload(filename):
mongo.save_file(filename, request.files["file"])
return redirect(url_for("get_upload", filename=filename))
:param str filename: the filename of the file to return
:param file fileobj: the file-like object to save
:param str base: base the base name of the GridFS collections to use
:param str content_type: the MIME content-type of the file. If
``None``, the content-type is guessed from the filename using
:func:`~mimetypes.guess_type`
:param kwargs: extra attributes to be stored in the file's document,
passed directly to :meth:`gridfs.GridFS.put`
"""
if not isinstance(base, str):
raise TypeError("'base' must be string or unicode")
if not (hasattr(fileobj, "read") and callable(fileobj.read)):
raise TypeError("'fileobj' must have read() method")
if content_type is None:
content_type, _ = guess_type(filename)
storage = GridFS(self.db, base)
id = storage.put(fileobj, filename=filename, content_type=content_type, **kwargs)
return id