gogasca
gogasca

Reputation: 10058

Flask search query not using filters

I have a working Flask API and now I want to implement search queries. My understanding is that the filter is applied on the client and the Flask API takes care of it.

Flask==0.10.1
Flask-HTTPAuth==2.7.0
Flask-Limiter==0.9.1
Flask-Login==0.3.2
Flask-Mail==0.9.1
Flask-Principal==0.4.0
Flask-Restless==0.17.0

I have followed documentation and performed my search query, but still just retrieving same results:

http://flask-restless.readthedocs.org/en/latest/searchformat.html

No filter:

curl -u aaa:bbb -H "Content-Type: application/json" http://0.0.0.0:8080/api/1.0/job/

{
  "jobs": [
    {
      "description": "ESXi job completed", 
      "reference": "07FC78BCC0", 
      "status": 1
    }, 
    {
      "description": "Server discovery failed. Please verify HTTPS/SSH parameters", 
      "reference": "A6EE28F4C0", 
      "status": -1
    }]
}

Search query based on: http://flask-restless.readthedocs.org/en/latest/searchformat.html

curl -u aaa:bbb -G -H "Content-Type: application/json" -d '{
> "filters": [{"name": "description", "op": "like", "val": "%ESXi%"}]}' http://0.0.0.0:8080/api/1.0/job/

Or

curl -u aaa:bbb -G -H "Content-Type: application/json" -d '{"filters": [{"name": "status", "op": "eq", "val":0}]}' http://0.0.0.0:8080/api/1.0/job/

And same results are shown.

This is my Flask endpoint:

def get_jobs():
    """

    :return:
    """
    try:
        log.info(request.remote_addr + ' ' + request.__repr__())
        jobs = Model.Job.query.order_by(desc(Model.Job.job_start)).limit(settings.items_per_page).all()

        # =========================================================
        # Get JOBS
        # =========================================================

        values = ['description', 'status', 'reference']
        response = [{value: getattr(d, value) for value in values} for d in jobs]
        return jsonify(jobs=response)


    except Exception, excpt:
        log.exception(excpt.__repr__())
        response = json.dumps('Internal Server Error. Please try again later')
        resp = Response(response, status=500, mimetype='application/json')
        return resp

My Model

class Job(db.Model, AutoSerialize, Serializer):
    """

    """
    __tablename__ = 'job'
    __public__ = ('status','description','reference','job_start','job_end')
    id = Column(Integer, primary_key=True, server_default=text("nextval('job_id_seq'::regclass)"))
    description = Column(String(200))
    reference = Column(String(50))
    job_start = Column(DateTime)
    job_end = Column(DateTime)
    fk_server = Column(ForeignKey(u'server.id'))
    owner_id = Column(ForeignKey(u'auth_user.id'))
    is_cluster = Column(Boolean)
    host_information = Column(String(1024))
    status = Column(Integer, nullable=False)
    owner = relationship(u'AuthUser')
    server = relationship(u'Server')

    def serialize(self):
        """

        :return:
        """

        d = Serializer.serialize(self)
        return d

Do I need to change anything?

Upvotes: 0

Views: 1627

Answers (1)

pjcunningham
pjcunningham

Reputation: 8046

Maybe having __public__ as a Job attribute is interfering with the way the filtering works. There's a warning in the Flask-Restless documentation about this.

Upvotes: 1

Related Questions