def delete(self, zipcode): try: zip_code_validator = ZipCodeValidator(zipcode) if not zip_code_validator.is_valid(): raise BadRequest() zipcode = zip_code_validator.cleaned_data['zipcode'] zipcode_obj = ZipCode.remove_by_zipcode(zipcode) app.logger.info('Remove zipcode {}'.format(zipcode_obj)) return {'status_code': Http.NOT_CONTENT}, Http.NOT_CONTENT except BadRequest as exception: app.logger.error('Invalid value to parameter zipcode {}, Exception: {} '.format(zipcode, exception)) return {'message': 'Invalid parameter', 'status_code': Http.BAD_REQUEST}, Http.BAD_REQUEST except DoesNotExist, exception: app.logger.error('Error to get a zipcode {} on database, Exception: {}'.format(zipcode, exception)) return {'message': 'Zipcode not found', 'status_code': Http.NOT_FOUND}, Http.NOT_FOUND
def put(self): zipcode = http_request.form.get('zip_code') try: zip_code_validator = ZipCodeValidator(zipcode) if not zip_code_validator.is_valid(): raise BadRequest() zipcode = zip_code_validator.cleaned_data['zipcode'] uri = '{}{}'.format(app.config['POSTMON_BASE_URL'], zipcode) response = requests.get(uri) if response.status_code == Http.NOT_FOUND: raise NotFound() response_to_dict = json.loads(response.content) if ZipCode.zipcode_exists(zipcode): zipcode_obj = ZipCode.update_zipcode(response_to_dict) app.logger.info('Update zipcode {} on database'.format(zipcode_obj)) return {'message': 'Zipcode updated', 'status_code': Http.OK}, Http.OK zipcode_obj = ZipCode.save_zipcode(response_to_dict) app.logger.info('Add zipcode {} on database'.format(zipcode_obj)) return {'message': 'Zipcode added', 'status_code': Http.CREATED}, Http.CREATED except BadRequest as exception: app.logger.error('Invalid value to parameter zipcode {}, Exception: {} '.format(zipcode, exception)) return {'message': 'Invalid parameter', 'status_code': Http.BAD_REQUEST}, Http.BAD_REQUEST except NotFound as exception: app.logger.error('Error to add a zipcode {}, this zipcode not found on postmon api, Exception: {}'.format(zipcode, exception)) return {'message': 'Zipcode not found', 'status_code': Http.NOT_FOUND}, Http.NOT_FOUND except Exception as exception: app.logger.error('Internal server error: {} '.format(exception)) return {'message': 'Internal server error, contact the administrator', 'status_code': Http.INTERNAL_SERVER_ERROR}, Http.INTERNAL_SERVER_ERROR