def post(self): args = self.parser.parse_args() if (not Util._isValidStatus(args["status"])): return app.config["INVALID_STATUS"], 405 pet = Pet(**args).save() pet = Util._formatPetToJson(pet) return pet, 200 pass
def get(self, id): id = Util._isValidId(id) if not id: return app.config["INVALID_ID_ERROR"], 400 try: pet = Pet.objects.get(id=id) pet = Util._formatPetToJson(pet) return pet, 200 except Pet.DoesNotExist: return app.config["NOT_FOUND_ERROR"], 404 pass
def post(self, id): args = self.parser.parse_args() #validate Id id = Util._isValidId(id) if not id: return app.config["INVALID_ID_ERROR"], 400 #checking if pet with id exists try: pet = Pet.objects.get(id=id) except Pet.DoesNotExist: return app.config["NOT_FOUND_ERROR"], 404 #checking if the file is image if (not (self._isValidImage(args["file"].content_type))): return "Invalid Image File please sent image in one of the following formats (gif, jpg, jpeg, png, webp, svg)", 400 #save the file on local system fileName = self._saveFile() #update the document self._updateDocument(fileName, pet) #send the response response = { "code": 200, "type": args["file"].content_type, "message": "file was uploaded to " + fileName } return response, 200 pass
def put(self): args = self.parser.parse_args() if (not Util._isValidStatus(args["status"])): return app.config["INVALID_STATUS"], 405 try: id = int(args['id']) except ValueError: return app.config["INVALID_ID_ERROR"], 400 try: Pet.objects.get(id=id) pet = Pet(**args).save() pet = Util._formatPetToJson(pet) return pet, 200 except Pet.DoesNotExist: return app.config["NOT_FOUND_ERROR"], 404 pass
def get(self): # preparing query status = request.args.getlist("status") allPets = [] for stat in status: if (not Util._isValidStatus(stat)): return app.config["INVALID_STATUS"], 400 pets = Pet.objects(status=stat) allPets.extend(pets) # prepare result for response petsList = [] for pet in allPets: pet = Util._formatPetToJson(pet) petsList.append(pet) return petsList, 200 pass
def post(self, id): args = self.parser.parse_args() id = Util._isValidId(id) if not id: return app.config["INVALID_ID_ERROR"], 400 if (not Util._isValidStatus(args["status"])): return app.config["INVALID_STATUS"], 405 try: pet = Pet.objects.get(id=id) pet.name = args["name"] pet.status = args["status"] pet.save() pet = Util._formatPetToJson(pet) return pet, 200 except Pet.DoesNotExist: return app.config["NOT_FOUND_ERROR"], 404 pass
def delete(self, id): api_key = request.headers.get('api_key') if (not (api_key == app.config["API_KEY"])): return app.config["INVALID_API_KEY"], 400 id = Util._isValidId(id) if not id: return app.config["INVALID_ID_ERROR"], 400 try: pet = Pet.objects.get(id=id) res = pet.delete() return app.config["DELETE_SUCCESS"], 200 except Pet.DoesNotExist: return app.config["NOT_FOUND_ERROR"], 404 pass