def handle_query(): query = request.args.get('query') city = request.args.get('city') if not query or len(query) > 100: return ApiResponse(message="Query is invalid", has_error=True).send() if not city: city = get_default_city_name() query_response = _process_query(query, city) return ApiResponse(data=query_response).send()
def load_image(short_url): image_full_path = ImageUrlCacheManager.get_image_full_path(short_url) if not image_full_path: # We need to get the path from the db and cache it # Get the id of the image from short_url id = ImageModel.get_row_id_for_short_url(short_url) image = ImageModel.get_image_by_id(id) if image: ImageUrlCacheManager.cache_image_short_url(image) image_full_path = os.path.join(image.storage_full_dir, image.image_filename) else: return ApiResponse(message="Image not found", has_error=True).send() if os.path.isfile(image_full_path): return send_file(image_full_path) return ApiResponse(message="Image not found", has_error=True).send()
def get_images(page=0): images, next_page_num, total_page_nums = ImageModel.get_all_images(page) # Cache the image short_url as key and full image path as value # for faster image retrieves and less db calls for image in images: image.image_url = _generate_image_url(image.image_short_url) ImageUrlCacheManager.cache_image_short_url(image) image.create_posted_on_property() images = ImageSchema(many=True).dump(images).data return ApiResponse(data=images, total_page_nums=total_page_nums).send()
def handle_image_uploading(): if not request.files or not request.files['file']: return ApiResponse(message="File is missing", has_error=True).send() if not request.form['title'] or len(request.form['title']) > constants.IMAGE_TITLE_MAX_LENGTH: return ApiResponse(message="Title is invalid", has_error=True).send() if request.form['description'] and len(request.form['description']) > constants.IMAGE_DESCRIPTION_MAX_LENGTH: return ApiResponse(message="Description is invalid", has_error=True).send() # read the file and prepare a filename file = request.files['file'] filename = request.files['file'].filename if not is_allowed_file_extension(filename): return ApiResponse(message="File type is not supported", has_error= True).send() # generate today's folder such as folder '12-2-2012' storage_full_dir = generate_todays_date_folder() filename = secure_filename(file.filename) secret_filename = generate_secret_filename_for(filename) image_complete_path = os.path.join(storage_full_dir, secret_filename) file.save(image_complete_path) image_record = ImageModel(title=request.form['title'], description=request.form['description'], image_filename=secret_filename, storage_full_dir=storage_full_dir) db.session.add(image_record) db.session.commit() # we do refresh to update image_record with ID from the db db.session.refresh(image_record) short_url = image_record.self_assign_short_url() db.session.add(image_record) db.session.commit() image_full_url = _generate_image_url(short_url) return ApiResponse(message='Image uploaded: URL => {}'.format(image_full_url)).send()
def test_ApiResponse_properties(self): data = {"hello": "hello"} status_code = 6000 message = "test" has_error = True expected_response = dict(data=data, status_code=status_code, message=message, has_error=has_error) resp = ApiResponse(data=data, message=message, has_error=has_error, status_code=status_code) self.assertEqual(data, resp.data) self.assertEqual(message, resp.message) self.assertEqual(has_error, resp.has_error) actual_resp, actual_status_error = resp.send() self.assertEqual(status_code, actual_status_error) self.assertDictEqual(expected_response, json.loads(actual_resp))
def exception_handler(e): return ApiResponse(message="We can't find what you are looking for", status_code=404).send()
def get_cities(): return ApiResponse(data=constants.get_cities_names()).send()