Python eve sub-resource / custom query

1.3k views Asked by At

I am having two resources,

Users resource:

users = {
  'schema': {
    'username': {
        'type': 'string',
        'required': True
    },
    'interests': {
        'type': 'list'
    }
  }
}

Example,

{"username": "Yogi", "interests":["Sports", "News"]}

Events resource:

events = {
  'schema': {
    'name': {
        'type': 'string',
        'required': True,
    },
    'categories': {
        'type': 'list',
        'required': True,
        'minlength':1
    }
  }
}

Example,

{"name": "IPLT20", "categories": ["Sports"]}

Is there a way in Python Eve to query all events matching the interest of the user.

One way is to construct query like below,

/events?where={"$or":[{"categories":"Sports"},{"categories":"News"}]}

for this we'll have do a GET of user and using that information we'll have to construct the above query.

Is it possible in Python Eve to have a (one step) query something like,

/users/<userid>/events

or

/events?for=<userid>
2

There are 2 answers

0
Maksym Rudenko On

events should be something like:

events = {
    'url': 'users/<regex("[a-zA-Z0-9]{24}":user_id>/events',
    'schema': {
        'user_id': {
            'type': 'objectid',
            'data_relation': {
                'resource': 'queues',
                'field': '_id',
                'embeddable': True
            },
        },
        'name': {
            'type': 'string',
            'required': True,
        },
        'categories': {
            'type': 'list',
            'required': True,
            'minlength': 1
        }
    }
}

But this will make you work with events only through users/<regex("[a-zA-Z0-9]{24}":user_id>/events link. If you want to keep /events endpoint as well use this approach

0
Mandar Vaze On

Documentation suggests that although it is possible, where styled query is preferred/recommended