def recommend(time, category): start, end = map(lambda x: datetime.datetime.strptime(x, '%H:%M').time(), time.split('-')) category = _get_item_by_name(Category, category) # Get the day of the week. Now it is fixed to the timezone Europe/Madrid, but if the city was passed # it could be calculated for it, loading its timezone from the database. day = datetime.datetime.now(pytz.timezone('Europe/Madrid')) day = WEEK_DAYS[day.weekday()] # User's interval bounds expressed in hours user_start_hour = start.hour + start.minute / 60 user_end_hour = end.hour + end.minute / 60 # Activities opening intervals bounds expressed in hours activity_start_hour = OpeningInterval.start.hour + OpeningInterval.start.minute / 60 activity_end_hour = OpeningInterval.end.hour + OpeningInterval.end.minute / 60 q = Activity.select().join(OpeningInterval).where( (Activity.category == category) & (OpeningInterval.day == day) & # There's enough time to make the visit before it closes (( # Activity start <= user interval start (OpeningInterval.start <= start) & (user_start_hour + Activity.hours_spent <= activity_end_hour) & (user_start_hour + Activity.hours_spent <= user_end_hour)) | ( # Activity start >= user interval start (OpeningInterval.start >= start) & (activity_start_hour + Activity.hours_spent <= user_end_hour))) ).order_by(Activity.hours_spent.desc()) if q.exists(): return q[0].geojson return abort(404)
def query(**kwargs): q = Activity.select() if 'category' in kwargs: category = _get_item_by_name(Category, kwargs.get('category', None)) q = q.where(Activity.category == category) if 'location' in kwargs: location = _get_item_by_name(Location, kwargs.get('location', None)) q = q.where(Activity.location == location) if 'district' in kwargs: district = _get_item_by_name(District, kwargs.get('district', None)) q = q.where(Activity.district == district) return { 'type': 'FeatureCollection', 'features': list(map(lambda act: act.geojson, q)) }