def get_all_businesses(): ''' Get all Businesses ''' name = request.args.get('name') category = request.args.get('category') city = request.args.get('city') country = request.args.get('country') searchAll = request.args.get('searchAll') page = request.args.get('page') per_page = request.args.get('limit') businesses = Business.query.order_by(desc(Business.created_at)).order_by( desc(Business.created_at)) # Filter by search query name_q = category_q = country_q = city_q = None if name or category or country or city: # Filter by if name is not None and name.strip() != '': name_q = func.lower(Business.name).like('%' + func.lower(name) + '%') # Filter by category if category is not None and category.strip() != '': category_q = func.lower( Business.category).like('%' + func.lower(category) + '%') # Filter by city if city is not None and city.strip() != '': city_q = func.lower(Business.city).like('%' + func.lower(city) + '%') # Filter by country if country is not None and country.strip() != '': country_q = func.lower( Business.country).like('%' + func.lower(country) + '%') businesses = businesses.filter( or_(name_q, country_q, city_q, category_q)) if searchAll is not None or searchAll == 'true': # Search by all if name is not None and name.strip() != '': businesses = businesses.filter( or_( func.lower(Business.name).like('%' + func.lower(name) + '%'), func.lower(Business.city).like('%' + func.lower(name) + '%'), func.lower(Business.category).like('%' + func.lower(name) + '%'), func.lower(Business.country).like('%' + func.lower(name) + '%'))) errors = [] # Errors list if (per_page is not None and per_page.isdigit() is False and per_page.strip() != ''): errors.append({'limit': 'Invalid limit page limit number'}) if page is not None and page.isdigit() is False and page.strip() != '': errors.append({'page': 'Invalid page number'}) if len(errors) is not 0: response = jsonify(status='error', message="Please provide valid details", errors=errors) response.status_code = 400 return response page = int(page) if page is not None and page.strip() != '' else 1 per_page = int( per_page) if per_page is not None and per_page.strip() != '' else 20 # Overall filter results businesses = businesses.paginate(per_page=per_page, page=page) if len(Business.serializer(businesses.items)) is not 0: response = jsonify({ 'status': 'ok', 'message': 'There are {} businesses found'.format(str(len(businesses.items))), 'next_page': businesses.next_num, 'previous_page': businesses.prev_num, 'current_page': businesses.page, 'pages': businesses.pages, 'total_businesses': businesses.total, 'businesses': Business.serializer(businesses.items) }) response.status_code = 200 return response response = jsonify(status='error', message="No business found!") response.status_code = 200 return response
def get_user_businesses(): ''' User's Businesses list ''' user_id = token_id(request.headers.get('Authorization')) query = request.args.get('name') category = request.args.get('category') city = request.args.get('city') country = request.args.get('country') page = request.args.get('page') per_page = request.args.get('limit') businesses = Business.query.order_by(desc( Business.created_at)).filter_by(user_id=user_id) if businesses.count() is not 0: # Filter by search query if query is not None and query.strip() != '': businesses = businesses.filter( func.lower(Business.name).like('%' + func.lower(query) + '%')) # Filter by category if category is not None and category.strip() != '': businesses = businesses.filter( func.lower(Business.category) == func.lower(category)) # Filter by city if city is not None and city.strip() != '': businesses = businesses.filter( func.lower(Business.city) == func.lower(city)) # Filter by country if country is not None and country.strip() != '': businesses = businesses.filter( func.lower(Business.country) == func.lower(country)) errors = [] # Errors list if (per_page is not None and per_page.isdigit() is False and per_page.strip() != ''): errors.append({'limit': 'Invalid limit page limit number'}) if page is not None and page.isdigit() is False and page.strip() != '': errors.append({'page': 'Invalid page number'}) if len(errors) is not 0: response = jsonify(status='error', message="Please provide valid details", errors=errors) response.status_code = 400 return response page = int(page) if page is not None and page.strip() != '' else 1 per_page = int(per_page) if ((per_page is not None) and (per_page.strip() != '')) else 20 # Overall filter results businesses = businesses.paginate(per_page=per_page, page=page) if len(Business.serializer(businesses.items)) is not 0: response = jsonify({ 'status': 'ok', 'message': 'There are {} businesses found'.format( str(len(businesses.items))), 'next_page': businesses.next_num, 'previous_page': businesses.prev_num, 'current_page': businesses.page, 'pages': businesses.pages, 'total_businesses': businesses.total, 'businesses': Business.serializer(businesses.items) }) response.status_code = 200 return response response = jsonify(status='error', message="No business found!") response.status_code = 200 return response response = jsonify(status='error', message="You don't have any registered business") response.status_code = 200 return response