Giter VIP home page Giter VIP logo

eve-mongoengine's Introduction

MongoEngine

Info:MongoEngine is an ORM-like layer on top of PyMongo.
Repository:https://github.com/MongoEngine/mongoengine
Author: Harry Marr (http://github.com/hmarr)
Maintainer:Bastien Gerard (http://github.com/bagerard)
https://travis-ci.org/MongoEngine/mongoengine.svg?branch=master https://coveralls.io/repos/github/MongoEngine/mongoengine/badge.svg?branch=master https://pepy.tech/badge/mongoengine/month https://readthedocs.org/projects/mongoengine-odm/badge/?version=latest

About

MongoEngine is a Python Object-Document Mapper for working with MongoDB. Documentation is available at https://mongoengine-odm.readthedocs.io - there is currently a tutorial, a user guide, and an API reference.

Supported MongoDB Versions

MongoEngine is currently tested against MongoDB v3.6, v4.0, v4.4, v5.0, v6.0 and v7.0. Future versions should be supported as well, but aren't actively tested at the moment. Make sure to open an issue or submit a pull request if you experience any problems with a more recent MongoDB versions.

Installation

We recommend the use of virtualenv and of pip. You can then use python -m pip install -U mongoengine. You may also have setuptools and thus you can use easy_install -U mongoengine. Another option is pipenv. You can then use pipenv install mongoengine to both create the virtual environment and install the package. Otherwise, you can download the source from GitHub and run python setup.py install.

The support for Python2 was dropped with MongoEngine 0.20.0

Dependencies

All of the dependencies can easily be installed via python -m pip. At the very least, you'll need these two packages to use MongoEngine:

  • pymongo>=3.4

If you utilize a DateTimeField, you might also use a more flexible date parser:

  • dateutil>=2.1.0

If you need to use an ImageField or ImageGridFsProxy:

  • Pillow>=7.0.0

If you need to use signals:

  • blinker>=1.3

Examples

Some simple examples of what MongoEngine code looks like:

from mongoengine import *
connect('mydb')

class BlogPost(Document):
    title = StringField(required=True, max_length=200)
    posted = DateTimeField(default=datetime.datetime.utcnow)
    tags = ListField(StringField(max_length=50))
    meta = {'allow_inheritance': True}

class TextPost(BlogPost):
    content = StringField(required=True)

class LinkPost(BlogPost):
    url = StringField(required=True)

# Create a text-based post
>>> post1 = TextPost(title='Using MongoEngine', content='See the tutorial')
>>> post1.tags = ['mongodb', 'mongoengine']
>>> post1.save()

# Create a link-based post
>>> post2 = LinkPost(title='MongoEngine Docs', url='hmarr.com/mongoengine')
>>> post2.tags = ['mongoengine', 'documentation']
>>> post2.save()

# Iterate over all posts using the BlogPost superclass
>>> for post in BlogPost.objects:
...     print('===', post.title, '===')
...     if isinstance(post, TextPost):
...         print(post.content)
...     elif isinstance(post, LinkPost):
...         print('Link:', post.url)
...

# Count all blog posts and its subtypes
>>> BlogPost.objects.count()
2
>>> TextPost.objects.count()
1
>>> LinkPost.objects.count()
1

# Count tagged posts
>>> BlogPost.objects(tags='mongoengine').count()
2
>>> BlogPost.objects(tags='mongodb').count()
1

Tests

To run the test suite, ensure you are running a local instance of MongoDB on the standard port and have pytest installed. Then, run pytest tests/.

To run the test suite on every supported Python and PyMongo version, you can use tox. You'll need to make sure you have each supported Python version installed in your environment and then:

# Install tox
$ python -m pip install tox
# Run the test suites
$ tox

Community

Contributing

We welcome contributions! See the Contribution guidelines

eve-mongoengine's People

Contributors

dmitryax avatar hellerstanislav avatar kcaylor avatar mmellison avatar moul 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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eve-mongoengine's Issues

FileField

Hello,
I was wondering what the issue was with supporting the FileField? Is it on the Eve side, or is there some other issue?

I'd love to help solve this.

Thanks,
James

unique constraint returns 500 instead of 409

If a field has a unique constraint, Eve would return 500 if a duplicate value is submitted, instead of 409.
I think this also applies to other constraints as well, since Eve only catches exceptions raised by PyMongo, not by MongoEngine.

Is there any chance that this can be fixed?

Is the project dead?

Hi all,

I was wondering whether there is any ideas of developing the project. Indeed, the requirements.txt date back to a <=0.9 version of mongoengine which is currently at its v0.10.6.
Just in case I volunteer in taking over it.

Luca

PATCH with empty content raises OperationError

When issuing PATCH request with empty content {}, eve-mongoengine fails with

Traceback (most recent call last):
  File "/home/vagrant/.virtualenvs/shop-demo/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/home/vagrant/.virtualenvs/shop-demo/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/vagrant/.virtualenvs/shop-demo/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/vagrant/.virtualenvs/shop-demo/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/vagrant/.virtualenvs/shop-demo/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/vagrant/.virtualenvs/shop-demo/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/vagrant/.virtualenvs/shop-demo/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/vagrant/workspace/shop-demo/src/magnetpro/server/magnetpro/services/common/base.py", line 549, in catcher
    return func(*args, **kwargs)
  File "/home/vagrant/workspace/shop-demo/src/magnetpro/server/magnetpro/services/common/base.py", line 626, in proxy
    response = view(**request.view_args)
  File "/home/vagrant/workspace/shop-demo/src/magnetpro/server/magnetpro/services/menu.py", line 382, in patch
    current_app.data.update('MenuItem', _id, payload)
  File "/home/vagrant/.virtualenvs/shop-demo/lib/python2.7/site-packages/eve_mongoengine/datalayer.py", line 279, in update
    qry.update_one(write_concern=self._wc(resource), **kwargs)
  File "/home/vagrant/.virtualenvs/shop-demo/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 467, in update_one
    upsert=upsert, multi=False, write_concern=write_concern, **update)
  File "/home/vagrant/.virtualenvs/shop-demo/lib/python2.7/site-packages/mongoengine/queryset/base.py", line 423, in update
    raise OperationError("No update parameters, would remove data")
OperationError: No update parameters, would remove data

KeyError _etag

I am getting this exception with a simple code. It breaks the /person endpoint and all of other endpoints I receive a 404 error.

screen shot 2015-09-14 at 5 22 28 pm

Using:

  • eve 0.5.3 or in-dev

  • eve-Mongoengine 0.0.9

    class Person(mongoengine.Document):
    name = mongoengine.StringField()
    age = mongoengine.IntField()

    app = Eve()
    ext = EveMongoengine(app)
    ext.add_model(Person)

how to add put to a item or a resource.

i have tried using the item_methods=['GET', 'PUT', 'DELETE', 'PATCH' ] approach at both the resource and item level. resource level doesnt even let me add a put. python compiler throws up.

ext.add_model(Item, item_methods=['GET', 'PUT', 'DELETE', 'PATCH' ])

this succeeds but i still get a 405. in the response i see "allow:
OPTIONS, HEAD, DELETE, POST, GET" . so no put essentially.

i just want to update a record. i tried using post for it but it complains about the_id field being not present. suggestions?

Set default query manager attribute into datalayer class

Datalayer class should contain some attribute, which would define default queryset (query manager) attribute. Nowadays, it's objects. But one could want something else, some non-default queryset. Maybe this should work:

class MongoengineDataLayer(Mongo):
    """
    Data layer for eve-mongoengine extension.
    Most of functionality is copied from :class:`eve.io.mongo.Mongo`.
    """
    json_encoder_class = MongoengineJsonEncoder
    _structured_fields = (EmbeddedDocumentField, DictField, MapField)
    # new attribute! ------------------------------------------------------
    default_queryset_attr = 'objects'
    # ---------------------------------------------------------------------
    ....

ability to exclude fields using projection:{ 'field': 0}

It would be nice to be able to use Eve's projection definitions to exclude fields rather than only defining fields to include. Right now, _projection() assumes the list of keys in projection are fields to include. However,

datasource: {
    projection: { 'field': 0 }
}

should return all fields except 'field'. Perhaps a simple check on the value of keys in projection and then using either qs.only() or qs.exclude() would work?

Is it possible listField with Embedded fields ?

Hi,

Is it possible to implement ListField with EmbeddedField? Something:

stars = ListField(EmbeddedDocumentField(StarsModel))

where...

class StarsModel(EmbeddedDocument):
    score = db.IntField(default=0)
    voter = db.ReferenceField(User)

How to make this schema?

No authorization support for MongoDB

I could not get my connections to work and then noticed that there is no authenticate operation even if you supply a proper MONGO_USERNAME and MONGO_PASSWORD variable.

I traced the connection to the class MongoengineDataLayer and re-wrote the start of the init to the following:

def init(self, ext):
"""
Constructor.

    :param ext: instance of :class:`EveMongoengine`.
    """

    username = ext.app.config['MONGO_USERNAME']
    password = ext.app.config['MONGO_PASSWORD']
    auth = (username, password)

    if any(auth) and not all(auth):
        raise Exception('Must set both USERNAME and PASSWORD or neither')

    self.conn = connect(ext.app.config['MONGO_DBNAME'],
                        host=ext.app.config['MONGO_HOST'],
                        port=ext.app.config['MONGO_PORT'])

    db = self.conn[ext.app.config['MONGO_DBNAME']]
    if any(auth):
        db.authenticate(username, password)

This now accounts for a passed username and password, and does connect if proper.

I would submit a patch/pull, except I dont work in indents with spacing (SCiTE autoscript saves them as TABS) so on a save it blew the diff out to every line.

HTH

Deal with Document creation

Its impossible for me to create document based on Eve Mongoengine.

Because .save() is checking for existing fields and not _etag and _deleted.

If I want to have thoses two fields in my database I have to specify them in the schema, but Eve don't agree with this.

Connection to replica sets

The connection assumes a single host and does not accommodate the possibility of a replica set. The easiest fix would be to allow for the use of a MONGO_URI when setting up the connection, which - if set - would be used to make the connection rather than building the connection using the MONGO_HOST, and MONGO_PORT variables. If MONGO_URI is not set, then the behavior would remain as in the current implementation. Thoughts?

Compatibility Problem Between Eve-Mongoengine and Eve-Swagger

The Eve-Swagger is a python library used to create swagger doc:
https://github.com/pyeve/eve-swagger

I had eve and eve-swagger working together fine. After I tried to install Eve-Mongoengine in my virtualenv, the eve-swagger produced an internal error when I tried to visit /api-docs endpoint. I've realized that there must be some version conflict between eve-swagger and Eve-Mongoengine that makes problems. It's output of eve when I tried to get /api-docs endpoint:
127.0.0.1 - - [28/Aug/2017 10:31:46] "GET /api-docs HTTP/1.1" 500 -
I tried eve in DEBUG mode and got nothing.

It's is my requirements.txt file before installing Eve-Mongoengine that was fine:
Cerberus==0.9.2
click==6.7
Eve==0.7.4
Eve-Swagger==0.0.7
Events==0.2.2
Flask==0.12
Flask-PyMongo==0.5.1
itsdangerous==0.24
Jinja2==2.9.6
MarkupSafe==0.23
pymongo==3.5.0
simplejson==3.11.1
Werkzeug==0.11.15

This is my requirements.txt after installing Eve-Mongoengine that makes /api-docs unavailable:
blinker==1.4
Cerberus==0.7.2
click==6.7
Eve==0.4
Eve-Mongoengine==0.0.9
Eve-Swagger==0.0.7
Events==0.2.1
Flask==0.10.1
Flask-PyMongo==0.3.0
itsdangerous==0.24
Jinja2==2.7.3
MarkupSafe==0.23
mongoengine==0.13.0
pymongo==2.7.1
simplejson==3.5.2
six==1.10.0
Werkzeug==0.9.6

I already filed this issue in eve-swagger git project:
pyeve/eve-swagger#50

Pagination _meta

When requesting with "max_results" less than the total resources in DB, the _meta field returns an incorrect "total" value (it's the same as the "max_results" value).. Should be the total number of resources in DB.

Requests using etag returned by POST is bound to get a 412

The etag returned by POST is inserted before the a document is saved into DB. So this etag is calculated without the _id field:
https://github.com/nicolaiarocci/eve/blob/master/eve/methods/post.py#L225
Though later there's point the document does have _id, the etag is not updated since there's already one.
https://github.com/nicolaiarocci/eve/blob/master/eve/methods/common.py#L384

If you use the returned etag here to do another request, a DELETE for example, the etag comparison is bound to fail because the server-side etag is now calculated with _id:
https://github.com/nicolaiarocci/eve/blob/master/eve/methods/common.py#L64

Example:

POST /people HTTP/1.1
Content-Type: application/json

{
    "name": "test"
}

Response:

{
    "created_at": "2015-03-26 05:55:17", 
    "updated_at": "2015-03-26 05:55:17", 
    "_links": {"self": {"href": "people/55139f45a54d75532e2ed06f", "title": "People"}}, 
    "_status": "OK", 
    "_id": "55139f45a54d75532e2ed06f", 
    "_etag": "87f7a17318bed13ba0ad0dcd259b3b22a4c9375a"
}

With the above etag:

DELETE /people/55139f45a54d75532e2ed06f HTTP/1.1
Content-Type: application/json
If-Match: 87f7a17318bed13ba0ad0dcd259b3b22a4c9375a

Now we get a 412.

Mongoengine hooks + ETag fail

When defined some mongoengine pre_save hook, which modifies saved data (data, which are about to be saved), etag is computed from non-changed data -- FAIL. Eve-mongoengine datalayer should cover this use-case.

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.