Permalink
Browse files

drop ReadPreference constants and validation, rely on pymongo's inste…

…ad. update the docs.
  • Loading branch information...
ultrabug committed Feb 11, 2013
1 parent 632c160 commit 5916cfcf25398111fc874b31ff9bc998a1e6d8cb
Showing with 2 additions and 48 deletions.
  1. +0 −13 docs/index.rst
  2. +2 −35 flask_pymongo/__init__.py
View
@@ -100,13 +100,6 @@ directives:
deteremined by the `isMaster
<http://www.mongodb.org/display/DOCS/Replica+Set+Commands#ReplicaSetCommands-isMaster>`_
command). Default: ``None``.
``MONGO_READ_PREFERENCE`` Determines how read queries are routed to the
replica set members. Must be one of
:data:`~flask_pymongo.PRIMARY`,
:data:`~flask_pymongo.SECONDARY`, or
:data:`~flask_pymongo.SECONDARY_ONLY`, or the
string names thereof. Default
:data:`~flask_pymongo.PRIMARY`.
``MONGO_DOCUMENT_CLASS`` This tells pymongo to return custom objects instead
of dicts, for example ``bson.son.SON``. Default: ``dict``
============================ ===================================================
@@ -165,12 +158,6 @@ Constants
.. autodata:: flask_pymongo.DESCENDING
.. autodata:: flask_pymongo.PRIMARY
.. autodata:: flask_pymongo.SECONDARY
.. autodata:: flask_pymongo.SECONDARY_ONLY
Classes
-------
View
@@ -24,8 +24,7 @@
# POSSIBILITY OF SUCH DAMAGE.
__all__ = ('PyMongo', 'ASCENDING', 'DESCENDING', 'PRIMARY',
'SECONDARY', 'SECONDARY_ONLY')
__all__ = ('PyMongo', 'ASCENDING', 'DESCENDING')
from bson.errors import InvalidId
from bson.objectid import ObjectId
@@ -43,33 +42,12 @@
PRIMARY = pymongo.ReadPreference.PRIMARY
"""Send all queries to the replica set primary, and fail if none exists."""
SECONDARY = pymongo.ReadPreference.SECONDARY
"""Distribute queries among replica set secondaries unless none exist or
are up, in which case send queries to the primary."""
SECONDARY_ONLY = pymongo.ReadPreference.SECONDARY_ONLY
"""Distribute queries among replica set secondaries, and fail if none
exist."""
DESCENDING = pymongo.DESCENDING
"""Descending sort order."""
ASCENDING = pymongo.ASCENDING
"""Ascending sort order."""
READ_PREFERENCE_MAP = {
# this handles defaulting to PRIMARY for us
None: PRIMARY,
# alias the string names to the correct constants
'PRIMARY': PRIMARY,
'SECONDARY': SECONDARY,
'SECONDARY_ONLY': SECONDARY_ONLY,
}
class BSONObjectIdConverter(BaseConverter):
"""A simple converter for the RESTful URL routing system of Flask.
@@ -112,8 +90,7 @@ def init_app(self, app, config_prefix='MONGO'):
The app is configured according to the configuration variables
``PREFIX_HOST``, ``PREFIX_PORT``, ``PREFIX_DBNAME``,
``PREFIX_AUTO_START_REQUEST``,
``PREFIX_REPLICA_SET``, ``PREFIX_READ_PREFERENCE``,
``PREFIX_AUTO_START_REQUEST``, ``PREFIX_REPLICA_SET``,
``PREFIX_USERNAME``, ``PREFIX_PASSWORD``, and ``PREFIX_URI`` where
"PREFIX" defaults to "MONGO". If ``PREFIX_URL`` is set, it is
assumed to have all appropriate configurations, and the other
@@ -140,7 +117,6 @@ def key(suffix):
if 'database' not in parsed:
raise ValueError('MongoDB URI does not contain database name')
app.config[key('DBNAME')] = parsed['database']
app.config[key('READ_PREFERENCE')] = parsed['options'].get('read_preference')
app.config[key('AUTO_START_REQUEST')] = parsed['options'].get('auto_start_request', True)
app.config[key('USERNAME')] = parsed['username']
app.config[key('PASSWORD')] = parsed['password']
@@ -158,7 +134,6 @@ def key(suffix):
app.config.setdefault(key('HOST'), 'localhost')
app.config.setdefault(key('PORT'), 27017)
app.config.setdefault(key('DBNAME'), app.name)
app.config.setdefault(key('READ_PREFERENCE'), None)
app.config.setdefault(key('AUTO_START_REQUEST'), True)
app.config.setdefault(key('SOCKET_TIMEOUT_MS'), None)
app.config.setdefault(key('CONNECT_TIMEOUT_MS'), None)
@@ -183,13 +158,6 @@ def key(suffix):
if any(auth) and not all(auth):
raise Exception('Must set both USERNAME and PASSWORD or neither')
read_preference = app.config[key('READ_PREFERENCE')]
read_preference = READ_PREFERENCE_MAP.get(read_preference, read_preference)
if read_preference not in (PRIMARY, SECONDARY, SECONDARY_ONLY):
raise Exception('"%s_READ_PREFERENCE" must be one of '
'PRIMARY, SECONDARY, SECONDARY_ONLY (was %r)'
% (config_prefix, read_preference))
replica_set = app.config[key('REPLICA_SET')]
dbname = app.config[key('DBNAME')]
auto_start_request = app.config[key('AUTO_START_REQUEST')]
@@ -206,7 +174,6 @@ def key(suffix):
args = [host]
kwargs = {
'auto_start_request': auto_start_request,
'read_preference': read_preference,
'tz_aware': True,
}

0 comments on commit 5916cfc

Please sign in to comment.