Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Eve - REST API additional_lookup not working

I am facing the exact same issue that is described here. I have user_creds endpoint for my API. When I visit localhost:5000/user_creds/, I can see all the documents in that collection. But when I do something like localhost:5000/user_creds/[email protected], I always get a 404 Not Found response.

user_creds domain in settings.py looks like this:

'user_creds': {
        'schema': { 
            'email': {
                'type': 'string',
                'regex': r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)",
                'required': True,
                'unique': True,
            },
            'password': {
                'type': 'string',
                'required': True
            },
        }

        'resource_methods': ['GET', 'POST'],

        'item_methods': ['GET', 'PATCH', 'PUT'],

        'additional_lookup': {
            'url': 'regex("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")',
            'field': 'email'
        }
    }

I am following the example given here but cant figure out where I am going wrong. Also, if I visit this URL: http://127.0.0.1:5000/user_creds?email==%[email protected]%22, I get all the documents in the collection instead of getting just a single document which matches the email regex. If I visit this http://127.0.0.1:5000/user_creds?where=email==%[email protected]%22, I am getting the desired response.

like image 682
Stupid Man Avatar asked Dec 10 '25 11:12

Stupid Man


1 Answers

@Vorticity has the right fix. Just remove the leading "^" in your additional_lookup regex as follows:

'additional_lookup': {
    'url': 'regex("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")',
    'field': 'email'
}

You should be able to retrieve your item with or without url encoding eg:

localhost:5000/user_creds/[email protected]
localhost:5000/user_creds/someemail%40gmail.com

If you have any interest in making your items retrievable at the item level by email only (not object id), you can use item_lookup_field together with item_url:

'user_creds': {
    ...
    'item_url': 'regex("[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$")',
    'item_lookup_field': 'email'
}
like image 62
alexander_the_meh Avatar answered Dec 13 '25 10:12

alexander_the_meh