コード例 #1
0
ファイル: itinerary.py プロジェクト: mikhailshilkov/Youth
def create_trip(from_place, from_location, to_place, to_location, route, date, start_time):
    if route == None:
        return Trip('Route not found', None, None, None, None, None, None)
        
    steps_to = []
    duration = 0
    expenses = 0
    has_subway_info = False
    step_start_time = start_time
    
    # assign icons
    route.directions[0].start_icon = from_place.place_type
    route.directions[0].start_name = from_place.name_local
    for i in range(len(route.directions)):
        step = route.directions[i]
        previous = route.directions[i-1] if i > 0 else None
        if step.is_subway():
            step.start_icon = step.end_icon = 'underground'
            step.start_name = step.end_name = 'Underground station'
            if previous != None and previous.is_walk():
                previous.end_icon = 'underground'
                previous.end_name = 'Subway station'
        elif step.is_train():
            step.start_icon = step.end_icon = 'train'
            if previous != None and previous.is_walk():
                previous.end_icon = 'train'
            near_trains = get_nearest_trains(step, date, step_start_time)
            if near_trains != None:
                [prev_train, next_train] = near_trains 
                delta_prev = utils.time_get_delta_minutes(prev_train.departure, step_start_time)
                delta_next = utils.time_get_delta_minutes(step_start_time, next_train.departure)
                if delta_prev < delta_next * 2:
                    extra_wait_time = -delta_prev
                    train = prev_train
                else: 
                    extra_wait_time = delta_next
                    train = next_train
                start_time = utils.time_add_mins(start_time, extra_wait_time)
                steps_to.append({'instruction': _('You should leave at %s to fit train timetable') % utils.time_to_string(start_time),
                                 'start_time': ' ',
                                 'hint' : ''})
                step.duration = train.get_duration()
        elif step.is_land_transport():
            step.start_icon = step.end_icon = 'bus'
            step.start_name = step.end_name = 'Bus stop'
            if previous != None and previous.is_walk():
                previous.end_icon = 'bus'
                previous.end_name = 'Bus stop'
        elif step.is_walk():
            if previous != None:
                step.start_icon = previous.end_icon
                step.start_name = previous.end_name
        step_start_time = utils.time_add_mins(step_start_time, step.duration)
    route.directions[-1].end_icon = to_place.place_type
    route.directions[-1].end_name = to_place.name_local
    
    step_start_time = start_time
    for i in range(len(route.directions)):
        step = route.directions[i]
        previous = route.directions[i-1] if i > 0 else None
        hint = step.get_default_addinfo()
        details = []
        if step.is_train() and not step.has_map:
            details.append({
                'show_label': _('Learn about train tickets'),
                'hide_label': _('Hide info'),
                'action': 'info',
                'data' : "'" + _("Train tickets info HTML") + "'"
                })
                        
        if step.has_map:
            details.append({
                    'show_label': _('Show map'),
                    'hide_label': _('Hide map'),
                    'action': 'map',
                    'data' : step.get_route_json()
                    })
        if step.is_subway() and not has_subway_info:
            has_subway_info = True
            details.append({
                    'show_label': _('Learn about tokens'),
                    'hide_label': _('Hide info'),
                    'action': 'info',
                    'data' : "'" + _("Tokens info HTML") + "'"
                    })
        if step.hint != None:
            details.append({
                    'show_label': 'Show info',
                    'hide_label': 'Hide info',
                    'action': 'info',
                    'data' : '<i>' + step.hint + '</i>'
                    })
        steps_to.append({'instruction': step.direction,
                 'start_time': utils.time_to_string(step_start_time),
                 'hint' : hint,
                 'details' : details})
        step_start_time = utils.time_add_mins(step_start_time, step.duration)
        duration += step.duration
        expenses += step.transport.price if step.transport != None and step.transport.price != None else 0
    steps_to.append({'instruction': to_place.name_local,
                 'start_time': utils.time_to_string(step_start_time),
                 'hint' : ''})        
    total_duration = utils.time_get_delta_minutes(start_time, step_start_time)
    return Trip(_('Trip from') + ' ' + from_place.name_local + ' ' + _('to') + ' ' + to_place.name_local, from_location.to_url_param(), to_location.to_url_param(), expenses, total_duration, route.get_cost(), steps_to)
コード例 #2
0
ファイル: itinerary.py プロジェクト: bordanton/Youth
def create_trip(from_place, from_location, to_place, to_location, route, date, start_time):
    steps_to = []
    duration = 0
    expenses = 0
    has_subway_info = False
    step_start_time = start_time

    # assign icons
    for i in range(len(route.directions)):
        step = route.directions[i]
        previous = route.directions[i - 1] if i > 0 else None
        if step.is_subway():
            step.start_icon = step.end_icon = "underground"
            step.start_name = step.end_name = "Underground station"
            if previous != None and previous.is_walk():
                previous.end_icon = "underground"
                previous.end_name = "Underground station"
        elif step.is_land_transport():
            step.start_icon = step.end_icon = "bus"
            step.start_name = step.end_name = "Bus stop"
            if previous != None and previous.is_walk():
                previous.end_icon = "bus"
                previous.end_name = "Bus stop"
        elif step.is_walk():
            if previous != None:
                step.start_icon = previous.end_icon
                step.start_name = previous.end_name
    route.directions[0].start_icon = from_place.place_type
    route.directions[0].start_name = from_place.name
    route.directions[-1].end_icon = to_place.place_type
    route.directions[-1].end_name = to_place.name

    for step in route.directions:
        if step.is_train():
            next_train = get_next_peterhot_train(date, step_start_time)
            step_start_time = next_train.departure
            hint = "Train at " + utils.time_to_string(next_train.departure)
            step.duration = next_train.get_duration()
        else:
            hint = step.addinfo

        details = []
        if step.has_map:
            details.append(
                {
                    "show_label": "Show the map",
                    "hide_label": "Hide the map",
                    "action": "map",
                    "data": step.get_route_json(),
                }
            )
        if step.is_subway() and not has_subway_info:
            has_subway_info = True
            details.append(
                {
                    "show_label": "Learn about tokens",
                    "hide_label": "Hide info",
                    "action": "info",
                    "data": "<i>To buy tokens you need to find ticket office which are normally located very close to the entrance [Bad English - correct] [TO DO: Check whether ticket machines have English interface] [TO DO: Add photo of ticket office]</i>",
                }
            )
        if step.hint != None:
            details.append(
                {
                    "show_label": "Show info",
                    "hide_label": "Hide info",
                    "action": "info",
                    "data": "<i>" + step.hint + "</i>",
                }
            )
        steps_to.append(
            {
                "instruction": translit.translify(step.direction),
                "start_time": utils.time_to_string(step_start_time),
                "hint": hint,
                "details": details,
            }
        )
        step_start_time = utils.time_add_mins(step_start_time, step.duration)
        duration += step.duration
        expenses += step.transport.price if step.transport != None and step.transport.price != None else 0
    steps_to.append({"instruction": to_place.name, "start_time": utils.time_to_string(step_start_time), "hint": ""})
    total_duration = utils.time_get_delta_minutes(start_time, step_start_time)
    return Trip(
        "Trip from " + from_place.name + " to " + to_place.name,
        from_location.to_url_param(),
        to_location.to_url_param(),
        expenses,
        total_duration,
        steps_to,
    )