Пример #1
0
def create_trip_search_terms(title, description):
  return_value = list()
  if title:
    return_value.append(search_terms.create_search_terms(title))
  if description:
    desc = json.loads(description)
    return_value.append(search_terms.create_search_terms(" ".join(desc)))
  return " "+" ".join(return_value) + " "
Пример #2
0
def search(search_text=None, from_date=None, to_date=None,
           min_price=None, max_price=None, offset=None, limit=None):
  '''Searches based on multiple parameters.

  Args:
    search_text: a text that should be looked in all textual fields of trips. In example:
      title, description etc.
    from_date: a trip will be returned if there is a TripDate.start_date after the from_date.
    to_date: a trip will be returned if there is a TripDate.end_date before the to_date.
    offset: amount of trips to skip.
    limit: amount of trips to fetch.

  Returns:
    A pair of: (total of trips satisfying the filter, list of Trips).
  '''
  trips_filter = True
  if search_text:
    stemmed_search_text = search_terms.create_search_terms(search_text)
    trips_filter = and_(trips_filter, Trip.search_terms.like('% {} %'.format(stemmed_search_text)))

  if min_price or max_price:
    if min_price:
      trips_filter = and_(trips_filter, Trip.from_price >= min_price)
    if max_price:
      trips_filter = and_(trips_filter, Trip.from_price <= max_price)

  date_filter = TripDate.trip_id == Trip.trip_id
  if from_date or to_date:
    if from_date:
      date_filter = and_(date_filter, TripDate.start_date >= from_date)
    if to_date:
      date_filter = and_(date_filter, TripDate.end_date <= to_date)

  count_filtered_dates = select([func.count(TripDate.trip_id)]).where(date_filter).as_scalar()
  min_start_date = select([func.min(TripDate.start_date)]).where(date_filter).as_scalar()

  trips_query = (Trip.query.filter(and_(trips_filter, count_filtered_dates > 0))
                 .order_by(min_start_date, Trip.from_price))

  return (trips_query.count(), trips_query.offset(offset).limit(limit).all())