GithubHelp home page GithubHelp logo

flask-pymongo's People

Contributors

achawkins avatar akhenakh avatar categulario avatar davidawad avatar dcrosta avatar eamanu avatar hbldh avatar jarus avatar jbouzekri avatar jeffbr13 avatar jeverling avatar juliascript avatar k-funk avatar markunsworth avatar mingrammer avatar ranman avatar robsonpeixoto avatar ropez avatar rspeed avatar thekoc avatar timgates42 avatar ultrabug avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flask-pymongo's Issues

create_index unique does not work

I read the pymongo official documents, pymongo doc

and create a unique index for my database, and I try to insert duplicates, and without errors, that is the unique index not working well.

mongo.db.users.create_index([("email", flask_pymongo.ASCENDING)], unique=True)
this is code based on official code example, but it reports error in flask-pymongo

URI parsing is incorrect

Looks like _ is wrong in the name of the parameters.
print parsed['options'].get('readpreference')
print parsed['options'].get('replicaset')

Gives me values but the code uses replica_set, read_preference which prints None

{'username': None, 'nodelist': [('mongo02', 27017), ('mongo03', 27017)], 'database': 'articles', 'collection': None, 'password': None, 'options': {'readpreference': 2, 'replicaset': 'rs0'}}

thorugh MONGO_URI='mongodb://mongo02,mongo03/articles?replicaSet=rs0&readPreference=secondary'

jsonify compatibility

I've been playing around with Flask and MongoDB today and really like them. Thanks very much for making integrating Flask with PyMongo easier.

I'm really surprised at how hard it is to return JSON from a view method, considering that's the first thing I tried and really only thing I want to do for this project. If this is integrating PyMongo and Flask, shouldn't there be an equivalent to flask.jsonify() that works with BSON?

As you probably know, this won't work because jsonify can't deal with ObjectId:

@app.route('/search/<query>')
def search(query):
    events = mongo.db.contributions.find({
        '_user_lower' : query.lower()
    })
    events = { 'events': list(events) }
    return jsonify(**events)

Akhenakh's awesome gist is working for me right now, but it seems like pymongo should offer this.

MONGO_URI problem

from flask import Flask, render_template
from flask_pymongo import PyMongo
import random


app = Flask(__name__)
mongo = PyMongo(app)
app.config['MONGO_URI'] = 'mongodb://username:[email protected]:55819/somedb'

@app.route('/chat/reply')
def chatReply():
    a = "lorem ipsum"
    a = mongo.db.Test1.count()
    return (str(a))

I using a mongo db at mlab. I have connected it using a flask app , it contains a collection Test1.
I have the MONGO_URI settings has shown above... but i m still getting this error.

pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused

no docs and examples in the pypi package

Hi,

The pypi package doesn't contain the docs and examples folders, it would be handy to provide them on the next release.

Cheers,

One of your Gentoo Linux packager ;)

Cannot use multiple replicaset instances which run on different ports

It's not possible to use the _HOST config key with port numbers included because the host and port values are being concatenated.

This is particularly problematic when using several seeds to connect to a replicaset:

MONGO_HOST='morton.local:30000,morton.local:30001,morton.local:30002'

The better way to do this is to pass the port number to the pymongo connection class (mongoclient or mongoreplicaset client) and leave pymongo to decide on whether to use the port

about the 'auth_database' and 'DBNAME' problem

I use MongoDB3.0 and Flask-PyMongo 0.5.1 and Python2.7

this is my DB config:
MONGO_HOST = '127.0.0.1'
MONGO_USERNAME = 'mongo'
MONGO_PASSWORD = 'xxxxxxx'
MONGO_PORT = 27017
MONGO_DBNAME = 'test'
MONGO_READ_PREFERENCE = 'SECONDARY_PREFERRED'
MONGO_REPLICA_SET = '27017'
MONGO_CONNECT_TIMEOUT_MS = 20000

I want to use 'test' DB(DBNAME = 'test'), but the program always tell me ' Authentication failed'
I check the mongoDB's log, and found the message like this " UserNotFound Could not find user mongo@test"
My user's information is in the 'admin' ,not in the 'test'.

Then I found the code in the file names 'init.py' ,and the file is in the package named 'flask_pymongo'

code in 128 lines is :
auth_database = 'admin'

code in 165-166 lines is :
if key('DBNAME') in app.config:
auth_database = app.config[key('DBNAME')]

I mean,after that code the 'auth_database' is not I wanted.
I want the auth_database='admin' and MONGO_DBNAME = 'test'
They are different.
Can you give us the config paramer like ``MONGO_AUTH_DBNAME='admin'`?
Thanks

Pass "connect=False" to MongoClient by default in PyMongo 3+

Hi Dan! First, a little background: MongoClient is not fork-safe. Programs that fork should create their MongoClients after forking. If that's not possible, the best workaround is to call MongoClient(connect=False), then fork, then start using the MongoClient.

A user asks how to pass "connect=False" to MongoClient with flask-pymongo:

http://stackoverflow.com/questions/41905472/pymongo-create-mongoclient-with-connect-false-or-create-client-after-forking

Is this currently possible?

I propose always passing "connect=False" to MongoClient in PyMongo 3 and later in flask-pymongo. If users frequently follow the pattern that the StackOverflow user shows, then "connect=False" will save some trouble.

Pagination Helper

If a query set is sorted, we can paginate efficiently using .min(...).skip(1).limit(page_size). Not sure how to make this work generically yet.

save file Unable to define _id

建议修改为:
def save_file(self, filename, fileobj, base='fs', content_type=None, kwargs):
example:
mongo.save_file(filename,open(filename,'r'),
{"_id":uuid})

Pip installer broken?

Running the pip installer seems not to work, at least presently:

Downloading/unpacking Flask-PyMongo (from -r requirements.txt (line 53))
    Downloading Flask-PyMongo-0.2.1.tar.gz
    Running setup.py egg_info for package Flask-PyMongo
      Download error on http://pypi.python.org/simple/nose/: [Errno 111] Connection refused -- Some packages may not be found!
      Download error on http://pypi.python.org/simple/nose/: [Errno 111] Connection refused -- Some packages may not be found!
      Couldn't find index page for 'nose' (maybe misspelled?)
      Download error on http://pypi.python.org/simple/: [Errno 111] Connection refused -- Some packages may not be found!
      No local packages or download links found for nose
      Traceback (most recent call last):
        File "<string>", line 14, in <module>
        File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/build/Flask-PyMongo/setup.py", line 65, in <module>
          test_suite='nose.collector',
        File "/usr/lib/python2.7/distutils/core.py", line 112, in setup
          _setup_distribution = dist = klass(attrs)
        File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/setuptools/dist.py", line 221, in __init__
          self.fetch_build_eggs(attrs.pop('setup_requires'))
        File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/setuptools/dist.py", line 245, in fetch_build_eggs
          parse_requirements(requires), installer=self.fetch_build_egg
        File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 576, in resolve
          dist = best[req.key] = env.best_match(req, self, installer)
        File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 821, in best_match
          return self.obtain(req, installer) # try and download/install
        File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 833, in obtain
          return installer(requirement)
        File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/setuptools/dist.py", line 294, in fetch_build_egg
          return cmd.easy_install(req)
        File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/setuptools/command/easy_install.py", line 583, in easy_install
          raise DistutilsError(msg)
      distutils.errors.DistutilsError: Could not find suitable distribution for Requirement.parse('nose')
      Complete output from command python setup.py egg_info:
      Download error on http://pypi.python.org/simple/nose/: [Errno 111] Connection refused -- Some packages may not be found!

  Download error on http://pypi.python.org/simple/nose/: [Errno 111] Connection refused -- Some packages may not be found!

  Couldn't find index page for 'nose' (maybe misspelled?)

  Download error on http://pypi.python.org/simple/: [Errno 111] Connection refused -- Some packages may not be found!

  No local packages or download links found for nose

  Traceback (most recent call last):

    File "<string>", line 14, in <module>

    File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/build/Flask-PyMongo/setup.py", line 65, in <module>

      test_suite='nose.collector',

    File "/usr/lib/python2.7/distutils/core.py", line 112, in setup

      _setup_distribution = dist = klass(attrs)

    File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/setuptools/dist.py", line 221, in __init__

      self.fetch_build_eggs(attrs.pop('setup_requires'))

    File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/setuptools/dist.py", line 245, in fetch_build_eggs

      parse_requirements(requires), installer=self.fetch_build_egg

    File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 576, in resolve

      dist = best[req.key] = env.best_match(req, self, installer)

    File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 821, in best_match

      return self.obtain(req, installer) # try and download/install

    File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/pkg_resources.py", line 833, in obtain

      return installer(requirement)

    File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/setuptools/dist.py", line 294, in fetch_build_egg

      return cmd.easy_install(req)

    File "/home/mac/repos/blpython/rpmbuild/SOURCES/py-virt/lib/python2.7/site-packages/distribute-0.6.24-py2.7.egg/setuptools/command/easy_install.py", line 583, in easy_install

      raise DistutilsError(msg)

  distutils.errors.DistutilsError: Could not find suitable distribution for Requirement.parse('nose')

Hard to understand error message when there is no database in URI

Mongodb documentation says that database part is optional:

/databse
Optional. The name of the database to authenticate if the connection string includes authentication credentials in the form of username:password@. If /database is not specified and the connection string includes credentials, the driver will authenticate to the admin database.

this library should default to that.
Also the error message is very hard to understand:

Traceback (most recent call last):
  File "connect_to_mongodb.py", line 12, in <module>
    mongo = PyMongo(app)
  File "/home/walkman/.virtualenvs/project/lib/python3.4/site-packages/flask_pymongo/__init__.py", line 98, in __init__
    self.init_app(app, config_prefix)
  File "/home/walkman/.virtualenvs/project/lib/python3.4/site-packages/flask_pymongo/__init__.py", line 235, in init_app
    db = cx[dbname]
  File "/home/walkman/.virtualenvs/project/lib/python3.4/site-packages/pymongo/mongo_client.py", line 1320, in __getitem__
    return self.__getattr__(name)
  File "/home/walkman/.virtualenvs/project/lib/python3.4/site-packages/flask_pymongo/wrappers.py", line 40, in __getattr__
    attr = super(MongoClient, self).__getattr__(name)
  File "/home/walkman/.virtualenvs/project/lib/python3.4/site-packages/pymongo/mongo_client.py", line 1309, in __getattr__
    return database.Database(self, name)
  File "/home/walkman/.virtualenvs/project/lib/python3.4/site-packages/pymongo/database.py", line 76, in __init__
    "of %s" % (str.__name__,))
TypeError: name must be an instance of str

The Exception thrown because database name will be None based on parsed MONGO_URI

ServerSelectionTimeoutError: No servers found yet with pymongo 3.1

I received the following warning/error messages below for Flask app using pymongo. Monkey patching https://github.com/mongodb/mongo-python-driver/blob/master/pymongo/mongo_client.py#L81 to connect=False fixes the issue. Also see http://stackoverflow.com/questions/31030307/why-is-pymongo-3-giving-serverselectiontimeouterror.

warning message

/usr/lib64/python2.7/site-packages/pymongo/topology.py:74: UserWarning: MongoClient opened before fork. Create MongoClient with connect=False, or create client after forking. See PyMongo's documentation for details: http://api.mongodb.org/python/current/faq.html#using-pymongo-with-multiprocessing>
  "MongoClient opened before fork. Create MongoClient "

traceback

  File "/usr/lib64/python2.7/site-packages/pymongo/collection.py", line 1654, in distinct
    with self._socket_for_reads() as (sock_info, slave_ok):
  File "/usr/lib64/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/usr/lib64/python2.7/site-packages/pymongo/mongo_client.py", line 720, in _socket_for_reads
    with self._get_socket(read_preference) as sock_info:
  File "/usr/lib64/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "/usr/lib64/python2.7/site-packages/pymongo/mongo_client.py", line 684, in _get_socket
    server = self._get_topology().select_server(selector)
  File "/usr/lib64/python2.7/site-packages/pymongo/topology.py", line 141, in select_server
    address))
  File "/usr/lib64/python2.7/site-packages/pymongo/topology.py", line 117, in select_servers
    self._error_message(selector))
ServerSelectionTimeoutError: No servers found yet

pip freeze versions

Flask-PyMongo==0.4.0
pymongo==3.1

Can Flask-Pymongo we smart enough to detect >1 cores/threads and call the __init__ of PyMongo with an appropriate connect argument?

Nose required to install

Hi!

In our production environment we don't have nose installed, and Flask-PyMongo is requiring it to be there.

Is there a problem in removing setup_requires=['nose'] from setup.py?

Thanks...

suggestion for tutorial

Hi, I just ran into an issue and thought I'd share it with you. I tried something like below, this is from the tutorial, as a development run from the command line. That worked fine. Then I tried to use it with mod_wsgi, but that broke it because name of the app is then different, so it connects to a different database. Maybe this is just obvious, but maybe you can mention this in the tutorial or somewhere else. Thanks for the great work!

from flask import Flask
from flask.ext.pymongo import PyMongo

app = Flask(__name__)
mongo = PyMongo(app)

MONGO_CONNECT ignored if MONGO_URI used

This may turn into a general discussion of how options should work in this library.

I'm using MONGO_URI, but I want to set MONGO_CONNECT to False. When I call init_app, it sees that I am using URI and thus all other options are ignored (

if key('URI') in app.config:
). I have to manually add ?connect=false into my connection uri. But my connection uri is just an environment variable and I don't want to manipulate it.

I personally think if, when using MONGO_URI, a config value is supplied and the corresponding uri option is not, the config value should be set instead of being thrown away.

when use MONGO_URI, error: Another user is already authenticated

I use a cloud mongodb server, version is 3.0.7. But its authMechanism is 'MONGODB-CR', not default 'SCRAM-SHA-1'. So when I used 'MONGODB-CR', occured error 'Another user is already authenticated'. My MONGO_URI:
'mongodb://demo:[email protected]:27017/demo?authMechanism=MONGODB-CR'

I read source code, and found the reason,

# flask_pymongo/__init__.py 252-253
if any(auth):
    db.authenticate(username, password)

authenticate not assign mechanism agument, so 'DEFAULT‘ and occured my error.

Support Python 3.4

Flask-PyMongo is compatible with and tested on Python 2.6, 2.7, and 3.3. It should be nice to have it compatible and tested on Python 3.4 as well.

Problem with authentication

I modified a line of your library because I can't log into MongoDB. I don't know if it's right but it works for me.

In file: flask_pymongo/init.py
line: 224

your line:
db.authenticate(username,password)

my changes:
cx['admin'].authenticate(username,password)

I hope it helps you.

Best

Add support for the authentication source parameter

To prevent the duplication of authentication data across databases, MongoDB has two mechanisms to delegate authentication to a specific separate database. Both use a specific key in the system.users collection, but act in the opposite direction. The first method is the otherDBRoles key in the admin database. The value names both the table that receives authentication and the roles assigned to the user. The other method is the userSource key, which goes in any database and simply refers to another database with the same user.

For both methods, when the client authenticates, it has to do so against the other database – admin for the first method, the named DB in the other. To accomplish this in PyMongo, a source parameter is included when calling MongoDB.authenticate.

Adding support for this feature should be fairly straightforward. IT requires the addition of a new config value with a name like PREFIX_SOURCE, as well as parsing the authSource URI parameter. That value is then passed to the authentication method, and that's all there is to it.

Improve documentation

You show some DB search examples on https://flask-pymongo.readthedocs.org/en/latest/, however, it would be nice to see some examples on how to create/insert a Document.

Sorry for sounding naive, but I'm completely new to NoSQL, and I'm use to SQLAlchemy and model representation. I guess things are different here... Since flask-pymongo was the first to pop up when Googling "flask mongodb" it is likely that more MongoDB beginners will be coming here to learn how to use MongoDB with Flask. For example, how would one create a model such as this:

class Post(db.Document):
    created_at = db.DateTimeField(default=datetime.datetime.now, required=True)
    title = db.StringField(max_length=255, required=True)
    slug = db.StringField(max_length=255, required=True)
    body = db.StringField(required=True)
    comments = db.ListField(db.EmbeddedDocumentField('Comment'))

Add GridFS support

This API probably needs at least two "levels":

  1. low-level GridFS helpers through the PyMongo object, for folks who want to roll their own
  2. high-level view functions for accepting uploads and serving GridFS content (with conditional GET support)

The high-level API might look like this:

@app.route('/uploads/<path:filename>')
mongo.serve_gridfs(collection=uploads)

where serve_gridfs returns a view function that expects a single "filename" argument. Need to figure out how to set endpoint name so that url_for works as expected/as configured.

Support PyMongo 3.0+

I apologize if this has been covered already but searching through PRs and closed tickets I could not find any reason why PyMongo is limited to pre-3.0 versions nor can I see any public discussion about why Flask-Pymongo wouldn't be trying to update to use 3.0+ driver versions.

No LICENSE file

Could you please add an explicit LICENSE file to the repository?
I'm working on a project for Cisco, and their internal tool that is used for describing assets used in projects requires that you add a text of the license for open-source components.

Failing to instantiate PyMongo objects from @app.before_request

Hey,

I'm not sure if this is because I'm doing something wrong or there is actually an issue. I'm creating the Flask application object from a factory function located in the application.py module. In the views I try to use g.mongo.db.... to access the database operations. When I'm trying to put the PyMongo object on the flask g object it throws me an exception (see below)

applications.py

def create(extra_config=None):
    app = Flask(__name__)

    app.config.from_object('authz.default_settings')
    app.config.from_envvar('AUTHZ_SETTINGS_OVERRIDE', silent=True)

    if extra_config:
        app.config.from_object(extra_config)

    from views.api import api_endpoints
    app.register_blueprint(api_endpoints, url_prefix='/api')

    return app

web.py

from authz import application
app = application.create()

@app.before_request
def before_request():
    g.mongo = PyMongo(app)

On request the following exception is raised: http://paste.pocoo.org/show/q0lUlcRBREC1AqtjBxqm/
What am I doing wrong here? If there is a better pattern to integrate PyMongo into my modular app, can you please advise me? My requirements are:

  • modular views with blueprints
  • need to be able to create unittests for the views

setup.py setup_requires=['nose'] prevents offline installation.

this line prevents offline install via pip. the documented way to do offline installs for pip is to capture all deps to a dir and then to install from them ala

pip install -d tools/dist -r requirements.txt
pip install --no-index --find-links=file://$PWD/tools/dist -r requirements.txt

the flask-pymongo setup_requires attempts to resolve and install nose, even if it is already installed and won't pick it up. yes this is a distribute/setuptools bug, but removing this line and everything works and afaics this declaration isn't even true.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.