Need Help, for string _id field

16 views
Skip to first unread message

Cesare Ghirelli

unread,
Feb 13, 2018, 4:46:25 AM2/13/18
to Eve REST Framework
Hello everybody

I already read the docs at  http://python-eve.org/tutorials/custom_idfields.html but I'm still stucked in. 

My docs in MongoDb are like this:

    "_id" : "20150807", 
    "filed" : "value"
    
}

So, following the docs I've wrtitten an encoder and a validator:

class _IDEncoder(BaseJSONEncoder):

def default(self, obj):
    if isinstance(obj, int) or isinstance(obj, str):
    return str(obj)
else:
    return super(_IDEncoder, self).default(obj)

class _IDValidator(Validator):
    """ Validator for integer _id """
    def _validate_type_integer(self, value):
    try:
        int(value)
    except ValueError:
        pass

app = Eve(json_encoder=_IDEncoder, validator=_IDValidator)
app.run()

Then, in my settings.py:

DOMAIN = {
    'documents': {
        'item_url': 'regex("^[0-9]{8}$")'
    }
}

But with http://localhost:5000/documents/20150807  it still doesn't find any document (http 404). 

A look at http://localhost:5000/documents, reveals:

<resource href="documents" title="documents">
<link rel="last" href="documents?page=46" title="last page"/>
<link rel="next" href="documents?page=2" title="next page"/>
<link rel="parent" href="/" title="home"/>
<_meta>
<max_results>25</max_results>
    <page>1</page>
    <total>1139</total>
</_meta>
<resource href="documents/20150807" title="documents">
    <_created>Thu, 01 Jan 1970 00:00:00 GMT</_created>
    <_etag>01445b87b53db3e6abc026dabcc320279c101b00</_etag>
    <_id>20150807</_id>
    <_updated>Thu, 01 Jan 1970 00:00:00 GMT</_updated>
</resource>
<resource href="documents/20150808" title="documents">
    <_created>Thu, 01 Jan 1970 00:00:00 GMT</_created>
    <_etag>1bde134512631e32ab073052d8dc5c0e9310a711</_etag>
    <_id>20150808</_id>
    <_updated>Thu, 01 Jan 1970 00:00:00 GMT</_updated>
</resource>

Did anyone knows where I'm wrong?

thank you in advance,
Cesare





Cesare Ghirelli

unread,
Feb 14, 2018, 4:56:17 AM2/14/18
to Eve REST Framework
Found it! I've end up with this and it works:

** settings.py **

DOMAIN = {
    'bollettini_qa': {
        'item_url': 'regex("[0-9]{8}")',
        'schema': {
             '_id': {'type': 'mystring'}
        },
    }
}

** run.py **

from eve import Eve

from eve.io.base import BaseJSONEncoder
from eve.io.mongo import Validator
from datetime import datetime


class _IDEncoder(BaseJSONEncoder):
"""http://python-eve.org/tutorials/custom_idfields.html ."""


def default(self, obj):
if isinstance(obj, int) or isinstance(obj, str):
return str(obj)
else:
return super(_IDEncoder, self).default(obj)


class _IDValidator(Validator):
""" Validator for integer _id """

def _validate_type_mystring(self, value):

try:
int(value)
except ValueError:
pass


app = Eve(json_encoder=_IDEncoder, validator=_IDValidator)
app.run()



Thank you however,
Cesare

Reply all
Reply to author
Forward
0 new messages