def list(select, request): list_return = { "data": "No results", "paging": { "next": None, "previous": None } } page = 1 number = 10 if "page" in request.args: page = int(request.args['page']) if "number" in request.args: number = int(request.args['number']) if page != 1: list_return["paging"]['prev'] = str(request.base_url) + '?page=' + str(page - 1) + '&number=' + str(number) list_return["paging"]['next'] = str(request.base_url) + '?page=' + str(page + 1) + '&number=' + str(number) dicts = [] for r in select.paginate(page, number): dicts.append(r.to_dict()) list_return["data"] = dicts return jsonify(list_return)
def list_of_states(): if request.method == 'GET': list = ListStyle.list(State.select(), request) return jsonify(list) if request.method == 'POST': # name_state = request.form['name'] try: new_state = State(name=request.form['name']) # saving the changes new_state.save() # returning the new information in hash form return "New State entered! -> %s\n" % (new_state.name) except: return make_response(jsonify({'code': 10000, 'msg': 'State already exist'}), 409)
def list(select, request): styled_return = { 'data': "No results", 'paging': { 'next': None, 'previous': None } } page = 1 num = 10 if request.args.get('page'): page = int(request.args.get('page')) if request.args.get('number'): num = int(request.args.get('number')) total = len([r for r in select]) styled_return['data'] = [r.to_dict() for r in select.paginate(page, num)] if page > 1: styled_return['paging']['previous'] = request.base_url + "?page=" + str(page - 1) + "&number=" + str(num) if page * num < total: styled_return['paging']['next'] = request.base_url + "?page=" + str(page + 1) + "&number=" + str(num) return jsonify(styled_return)
def get_cities(id): cities = [] query = City.select().join(State).where(State.id == id) for i in query: cities.append(i.to_hash()) return jsonify(cities)
def get_amenity_by_place(id): amenities = [] query = Amenity.select().where(Place.id == id) for i in query: amenities.append(i.to_hash()) return jsonify(amenities)
def get_amenities(): amenities = [] query = Amenity.select() for i in query: amenities.append(i.to_hash()) return jsonify(amenities)
def do_upload(): upload = request.files['upload'] if not upload.filename.lower().endswith(('.csv')): #TODO verfy RIGHT WAY raise JsonError(description="Invalid file.") try: _id = util.save_csv(upload) except Exception: raise JsonError(description="Failed to create user. try again.") return jsonify(id=_id)
def errorMessage(code, message): message = { 'status': code, 'message': message, 'httpcat': 'https://http.cat/' + str(code) } resp = jsonify(message) resp.status_code = code return resp
def list_of_states(): if request.method == 'GET': list = ListStyle.list(State.select(), request) return jsonify(list) if request.method == 'POST': # name_state = request.form['name'] try: new_state = State(name=request.form['name']) # saving the changes new_state.save() # returning the new information in hash form return "New State entered! -> %s\n" % (new_state.name) except: return make_response( jsonify({ 'code': 10000, 'msg': 'State already exist' }), 409)
def not_found(error=None): massage = { 'status' : 404, 'massage' : 'Not_Found' + request.url } resp = jsonify(massage) resp.status_code = 404 return resp
def handle_api_exception(error): """ Converts an API exception into an error response. """ _mp_track( type="exception", status_code=error.status_code, message=error.message, ) response = jsonify(dict(message=error.message)) response.status_code = error.status_code return response
def modify_state(state_id): try: # displaying state by id if request.method == 'GET': return jsonify(State.get(State.id == state_id).to_dict()) except: return "No State was found with id %d\n" % (int(id)) if request.method == 'DELETE': id = state_id try: # creating and oobject calling the State get_state = State.get(State.id == id) # deleting state based on the id get_state.delete_instance() return "State with id %d was deleted\n" % (int(id)) except: return "No State was found with id %d\n" % (int(id))
def modify_state(state_id): try: # displaying state by id if request.method == 'GET': return jsonify(State.get(State.id == state_id).to_dict()) except: return "No State was found with id %d\n" % (int(id)) if request.method == 'DELETE': id = state_id try: # creating and oobject calling the State get_state = State.get(State.id == id) # deleting state based on the id get_state.delete_instance() return "State with id %d was deleted\n" % (int(id)) except: return "No State was found with id %d\n" % (int(id))
def get_users(): users = [] query = User.select() for i in query: users.append(i.to_hash()) return jsonify(users), 200
def get_places(): places = [] query = Place.select() for i in query: places.append(i.to_hash()) return jsonify(places)
def serach_type(type): x = requests.get( "https://api.flickr.com/services/rest/?method=flickr.photos.search&safe_search=3&api_key=2e7d941570fb9c6591c1cfbe37d2d521&format=json&nojsoncallback=1&per_page=06&text={}food &page=1" .format(type)) return jsonify(x.json())
def get_books(id): books = [] query = PlaceBook.select().join(Place).where(Place.id == id) for book in query: books.append(book.to_hash()) return jsonify(books)
def not_found(error): data = {'code': '404', 'msg': 'not found'} error = jsonify(data) error.status_code = 404 return error '''return json_response(code="404", msg="not found")'''
def get_states(): states = [] query = State.select() for i in query: states.append(i.to_hash()) return jsonify(states), 200