コード例 #1
0
    def get(self, pet_id):
        if pet_id:
            pet = Pet.objects.filter(external_id=pet_id, live=True).first()
            if pet:
                response = {"result": "ok", "pet": pet_obj(pet)}
                return jsonify(response), 200
            else:
                return jsonify({}), 404
        else:
            # pet URL template
            pet_href = "/pets/?page=%s"

            pets = Pet.objects.filter(live=True)
            if "species" in request.args:
                pets = pets.filter(species=request.args.get('species'))
                pet_href += "&species=" + request.args.get('species')
            if "breed" in request.args:
                pets = pets.filter(breed=request.args.get('breed'))
                pet_href += "&breed=" + request.args.get('breed')

            page = int(request.args.get('page', 1))
            pets = pets.paginate(page=page, per_page=self.PETS_PER_PAGE)

            response = {
                "result": "ok",
                "total": pets.total,
                "links": [{
                    "href": pet_href % page,
                    "rel": "self"
                }],
                "pets": pets_obj(pets)
            }
            if pets.has_prev:
                response["links"].append({
                    "href": pet_href % (pets.prev_num),
                    "rel": "previous"
                })
            if pets.has_next:
                response["links"].append({
                    "href": pet_href % (pets.next_num),
                    "rel": "next"
                })
            return jsonify(response), 200
コード例 #2
0
ファイル: api.py プロジェクト: xod442/pets-api-dev
 def get(self, store_id):
     if store_id:
         store = Store.objects.filter(external_id=store_id,
                                      live=True).first()
         if store:
             if "pets" in request.url:
                 pets = Pet.objects.filter(store=store, live=True)
                 page = int(request.args.get('page', 1))
                 pets = pets.paginate(page=page,
                                      per_page=self.PETS_PER_PAGE)
                 response = {
                     "result":
                     "ok",
                     "links": [{
                         "href":
                         "/stores/%s/pets/?page=%s" % (store_id, page),
                         "rel":
                         "self"
                     }],
                     "store":
                     store_obj(store),
                     "pets":
                     pets_obj(pets, nostore=True)
                 }
                 if pets.has_prev:
                     response["links"].append({
                         "href":
                         "/stores/%s/pets/?page=%s" %
                         (store_id, pets.prev_num),
                         "rel":
                         "previous"
                     })
                 if pets.has_next:
                     response["links"].append({
                         "href":
                         "/stores/%s/pets/?page=%s" %
                         (store_id, pets.next_num),
                         "rel":
                         "next"
                     })
             else:
                 response = {"result": "ok", "store": store_obj(store)}
             return jsonify(response), 200
         else:
             return jsonify({}), 404
     else:
         stores = Store.objects.filter(live=True)
         page = int(request.args.get('page', 1))
         stores = stores.paginate(page=page, per_page=self.STORES_PER_PAGE)
         response = {
             "result": "ok",
             "links": [{
                 "href": "/stores/?page=%s" % page,
                 "rel": "self"
             }],
             "stores": stores_obj(stores)
         }
         if stores.has_prev:
             response["links"].append({
                 "href":
                 "/stores/?page=%s" % (stores.prev_num),
                 "rel":
                 "previous"
             })
         if stores.has_next:
             response["links"].append({
                 "href":
                 "/stores/?page=%s" % (stores.next_num),
                 "rel":
                 "next"
             })
         return jsonify(response), 200