Example #1
0
    def format_impacts(self, impacts):

        navitia = Navitia(self.navitia_url, self.coverage, self.token)

        columns = impacts.keys()

        rows = []
        for sub_dict in impacts.fetchall():
            row = []
            for column in columns:
                val = sub_dict[column]
                if column == 'pt_object_name' :
                    val = navitia.find_tc_object_name(sub_dict['pt_object_uri'], sub_dict['pt_object_type'])
                elif column == 'periodicity':
                    val = 'yes' if val else 'no'
                elif column == 'status' and val == 'archived':
                    val = 'deleted'
                elif isinstance(val, datetime.date):
                    val = utils.utc_to_local(val, self.time_zone)

                row.append(utils.sanitize_csv_data(val))

            rows.append(row)

        return {'columns' : columns, 'rows' : rows}
Example #2
0
    def format_impacts(self, impacts):

        navitia = Navitia(self.navitia_url, self.coverage, self.token)

        columns = impacts.keys()

        rows = []
        for sub_dict in impacts.fetchall():
            row = []
            for column in columns:
                val = sub_dict[column]
                if column == 'pt_object_name':
                    val = navitia.find_tc_object_name(
                        sub_dict['pt_object_uri'], sub_dict['pt_object_type'])
                elif column == 'periodicity':
                    val = 'yes' if val else 'no'
                elif column == 'status' and val == 'archived':
                    val = 'deleted'
                elif isinstance(val, datetime.date):
                    val = utils.utc_to_local(val, self.time_zone)

                row.append(utils.sanitize_csv_data(val))

            rows.append(row)

        return {'columns': columns, 'rows': rows}
Example #3
0
def test_navitia_unauthorized():
    n = Navitia('http://api.navitia.io', 'jdr')
    with HTTMock(navitia_mock_navitia_unauthorized):
        try:
            n._navitia_caller('http://api.navitia.io')
        except exceptions.Unauthorized as e:
            eq_(e.message, 'test unauthorized')
            raise
Example #4
0
def test_navitia_retry():
    n = Navitia('http://api.navitia.io', 'jdr')

    def navitia_timeout_response(*args, **kwargs):
        global navitia_timeout_count
        navitia_timeout_count += 1
        raise requests.exceptions.Timeout
    requests.get = navitia_timeout_response
    n._navitia_caller('http://api.navitia.io')
    eq_(navitia_timeout_count, 3)
Example #5
0
def test_navitia_retry():
    n = Navitia('http://api.navitia.io', 'jdr')

    def navitia_timeout_response(*args, **kwargs):
        global navitia_timeout_count
        navitia_timeout_count += 1
        raise requests.exceptions.Timeout

    requests.get = navitia_timeout_response
    n._navitia_caller('http://api.navitia.io')
    eq_(navitia_timeout_count, 3)
Example #6
0
 def output(self, key, obj):
     if not obj:
         return None
     if obj.type == 'line_section':
         return None
     navitia = Navitia(current_app.config['NAVITIA_URL'],
                       get_coverage(request), get_token(request))
     response = navitia.get_pt_object(obj.uri, obj.type)
     if response and 'name' in response:
         return response['name']
     return 'Unable to find object'
Example #7
0
 def output(self, key, obj):
     if not obj:
         return None
     if obj.type == 'line_section':
         return None
     navitia = Navitia(
         current_app.config['NAVITIA_URL'],
         get_coverage(request),
         get_token(request))
     response = navitia.get_pt_object(obj.uri, obj.type)
     if response and 'name' in response:
         return response['name']
     return 'Unable to find object'
Example #8
0
def test_get_pt_object():
    n = Navitia('http://api.navitia.io', 'jdr')
    mock = NavitiaMock(200, {'networks': [{'id': 'network:foo', 'name': 'reseau foo'}]},
                       assert_url='http://api.navitia.io/v1/coverage/jdr/networks/network:foo?depth=0')
    with HTTMock(mock):
        eq_(n.get_pt_object('network:foo', 'network'), {'id': 'network:foo', 'name': 'reseau foo'})

    mock = NavitiaMock(200, {'networks': []})
    with HTTMock(mock):
        eq_(n.get_pt_object('network:foo', 'network'), None)

    mock = NavitiaMock(404, {})
    with HTTMock(mock):
        eq_(n.get_pt_object('network:foo', 'network'), None)
Example #9
0
    def output(self, key, obj):
        if not obj:
            return None
        if isinstance(obj, dict) and 'uri' in obj and 'type' in obj:
            obj_uri = obj['uri']
            obj_type = obj['type']
        else:
            obj_uri = obj.uri
            obj_type = obj.type

        navitia = Navitia(
            current_app.config['NAVITIA_URL'],
            get_coverage(request),
            get_token(request))

        return navitia.find_tc_object_name(obj_uri, obj_type)
Example #10
0
    def output(self, key, obj):
        to_return = []

        # for history
        if hasattr(obj, 'history_localization') and isinstance(obj.localizations, list):
            return self._clean_localizations(obj.history_localization)

        navitia = Navitia(current_app.config['NAVITIA_URL'],
                          get_coverage(request),
                          get_token(request))

        if isinstance(obj, dict) and 'localizations' in obj:
            for localization in obj['localizations']:

                response = navitia.get_pt_object(
                    localization['uri'],
                    localization['type'])

                if response and 'name' in response:
                    response["type"] = localization['type']
                    to_return.append(response)
                else:
                    to_return.append(
                        {
                            "id": localization['uri'],
                            "name": "Unable to find object",
                            "type": localization['type']
                        }
                    )
        elif obj.localizations:
            for localization in obj.localizations:
                response = navitia.get_pt_object(
                    localization.uri,
                    localization.type)

                if response and 'name' in response:
                    response["type"] = localization.type
                    to_return.append(response)
                else:
                    to_return.append(
                        {
                            "id": localization.uri,
                            "name": "Unable to find object",
                            "type": localization.type
                        }
                    )
        return self._clean_localizations(to_return)
Example #11
0
    def output(self, key, obj):
        to_return = []
        navitia = Navitia(current_app.config['NAVITIA_URL'],
                          get_coverage(request), get_token(request))
        for localization in obj.localizations:
            response = navitia.get_pt_object(localization.uri,
                                             localization.type)

            if response and 'name' in response:
                response["type"] = localization.type
                to_return.append(response)
            else:
                to_return.append({
                    "id": localization.uri,
                    "name": "Unable to find object",
                    "type": localization.type
                })
        return to_return
Example #12
0
 def wrapper(*args, **kwargs):
     try:
         coverage = get_coverage(request)
         token = get_token(request)
     except exceptions.HeaderAbsent as e:
         return marshal({'error': {
             'message': utils.parse_error(e)
         }}, fields.error_fields), 400
     nav = Navitia(current_app.config['NAVITIA_URL'], coverage, token)
     return func(*args, navitia=nav, **kwargs)
Example #13
0
    def output(self, key, obj):

        # for history
        if hasattr(obj, 'name'):
            return obj.name

        if not obj:
            return None
        if isinstance(obj, dict) and 'uri' in obj and 'type' in obj:
            obj_uri = obj['uri']
            obj_type = obj['type']
        else:
            obj_uri = obj.uri
            obj_type = obj.type

        navitia = Navitia(current_app.config['NAVITIA_URL'],
                          get_coverage(request), get_token(request))

        return navitia.find_tc_object_name(obj_uri, obj_type)
Example #14
0
    def output(self, key, obj):
        to_return = []
        navitia = Navitia(current_app.config['NAVITIA_URL'],
                          get_coverage(request),
                          get_token(request))

        if isinstance(obj, dict) and 'localizations' in obj:
            for localization in obj['localizations']:

                response = navitia.get_pt_object(
                    localization['uri'],
                    localization['type'])

                if response and 'name' in response:
                    response["type"] = localization['type']
                    to_return.append(response)
                else:
                    to_return.append(
                        {
                            "id": localization['uri'],
                            "name": "Unable to find object",
                            "type": localization['type']
                        }
                    )
        elif obj.localizations:
            for localization in obj.localizations:
                response = navitia.get_pt_object(
                    localization.uri,
                    localization.type)

                if response and 'name' in response:
                    response["type"] = localization.type
                    to_return.append(response)
                else:
                    to_return.append(
                        {
                            "id": localization.uri,
                            "name": "Unable to find object",
                            "type": localization.type
                        }
                    )
        return to_return
Example #15
0
def test_get_pt_object():
    n = Navitia('http://api.navitia.io', 'jdr')
    mock = NavitiaMock(
        200, {'networks': [{
            'id': 'network:foo',
            'name': 'reseau foo'
        }]},
        assert_url=
        'http://api.navitia.io/v1/coverage/jdr/networks/network:foo?depth=0')
    with HTTMock(mock):
        eq_(n.get_pt_object('network:foo', 'network'), {
            'id': 'network:foo',
            'name': 'reseau foo'
        })

    mock = NavitiaMock(200, {'networks': []})
    with HTTMock(mock):
        eq_(n.get_pt_object('network:foo', 'network'), None)

    mock = NavitiaMock(404, {})
    with HTTMock(mock):
        eq_(n.get_pt_object('network:foo', 'network'), None)
Example #16
0
def test_query_formater():
    n = Navitia('http://api.navitia.io', 'jdr')
    eq_(
        n.query_formater('uri', 'network'),
        'http://api.navitia.io/v1/coverage/jdr/networks/uri?depth=0&disable_disruption=true'
    )
Example #17
0
def test_query_formater_all():
    n = Navitia('http://api.navitia.io', 'jdr')
    eq_(n.query_formater('uri', 'network', 'networks'),
        'http://api.navitia.io/v1/coverage/jdr/networks/uri/networks?depth=0')
Example #18
0
def test_query_formater_line_section():
    n = Navitia('http://api.navitia.io', 'jdr')
    eq_(n.query_formater('uri', 'line_section'), None)
Example #19
0
def test_query_formater_all_objects_invalid():
    n = Navitia('http://api.navitia.io', 'jdr')
    eq_(n.query_formater('uri', 'network', 'stop_areas'), 'http://api.navitia.io/v1/coverage/jdr/networks/uri/networks?depth=0')
Example #20
0
def test_query_formater_all_objects_invalid():
    n = Navitia('http://api.navitia.io', 'jdr')
    eq_(
        n.query_formater('uri', 'network', 'stop_areas'),
        'http://api.navitia.io/v1/coverage/jdr/networks/uri/networks?depth=0&disable_disruption=true'
    )
Example #21
0
def test_query_formater_line_section():
    n = Navitia('http://api.navitia.io', 'jdr')
    eq_(n.query_formater('uri', 'line_section'), None)
Example #22
0
def test_query_formater_all():
    n = Navitia('http://api.navitia.io', 'jdr')
    eq_(n.query_formater('uri', 'network', 'networks'), 'http://api.navitia.io/v1/coverage/jdr/networks/uri/networks?depth=0')
Example #23
0
def test_navitia_unknown_object_type():
    n = Navitia('http://api.navitia.io', 'jdr')
    with HTTMock(navitia_mock_unknown_object_type):
        n.get_pt_object('network:foo', 'aaaaaaaa')
Example #24
0
def test_navitia_request_error():
    n = Navitia('http://api.navitia.io', 'jdr')
    with HTTMock(navitia_mock_navitia_error):
        n.get_pt_object('network:foo','network')
Example #25
0
def test_navitia_request_error():
    n = Navitia('http://api.navitia.io', 'jdr')
    with HTTMock(navitia_mock_navitia_error):
        n.get_pt_object('network:foo3', 'network')
Example #26
0
def test_query_formater():
    n = Navitia('http://api.navitia.io', 'jdr')
    eq_(n.query_formater('uri', 'network'), 'http://api.navitia.io/v1/coverage/jdr/networks/uri?depth=0&disable_disruption=true')
Example #27
0
def test_navitia_unknown_object_type():
    n = Navitia('http://api.navitia.io', 'jdr')
    with HTTMock(navitia_mock_unknown_object_type):
        n.get_pt_object('network:foo4', 'aaaaaaaa')