Exemplo n.º 1
0
    def get_instances(self,
                      name=None,
                      lon=None,
                      lat=None,
                      object_id=None,
                      api='ALL'):
        available_instances = []
        if name:
            if name in self.instances:
                available_instances = [self.instances[name]]
        elif lon and lat:
            available_instances = [
                self.instances[k] for k in self._all_keys_of_coord(lon, lat)
                if k in self.instances
            ]
        elif object_id:
            available_instances = [
                self.instances[k]
                for k in self._find_coverage_by_object_id(object_id)
                if k in self.instances
            ]
        else:
            available_instances = list(self.instances.values())

        valid_instances = self._filter_authorized_instances(
            available_instances, api)
        if available_instances and not valid_instances:
            # user doesn't have access to any of the instances
            authentication.abort_request(user=authentication.get_user())
        else:
            return valid_instances
Exemplo n.º 2
0
    def get(self, region=None, lon=None, lat=None):
        args = self.parsers["get"].parse_args()
        self._register_interpreted_parameters(args)
        if len(args['q']) == 0:
            abort(400, message="Search word absent")

        if args['disable_geojson']:
            g.disable_geojson = True

        user = authentication.get_user(token=authentication.get_token(), abort_if_no_token=False)

        if args['shape'] is None and user and user.shape:
            args['shape'] = json.loads(user.shape)

        if user and user.default_coord:
            if args['from'] is None:
                args['from'] = CoordFormat()(user.default_coord)
        else:
            if args['from'] == '':
                raise InvalidArguments("if 'from' is provided it cannot be null")

        # If a region or coords are asked, we do the search according
        # to the region, else, we do a word wide search

        if any([region, lon, lat]):
            self.region = i_manager.get_region(region, lon, lat)
            timezone.set_request_timezone(self.region)
            response = i_manager.dispatch(args, "places", instance_name=self.region)
        else:
            available_instances = get_all_available_instances(user)
            autocomplete = global_autocomplete.get('bragi')
            if not autocomplete:
                raise TechnicalError('world wide autocompletion service not available')
            response = autocomplete.get(args, instances=available_instances)
        return response, 200
Exemplo n.º 3
0
    def get(self, region=None, lon=None, lat=None):
        args = self.parsers["get"].parse_args()
        self._register_interpreted_parameters(args)
        if len(args['q']) == 0:
            abort(400, message="Search word absent")

        if args['disable_geojson']:
            g.disable_geojson = True

        user = authentication.get_user(token=authentication.get_token(),
                                       abort_if_no_token=False)

        args['shape'] = json.loads(user.shape) if user and user.shape else None

        # If a region or coords are asked, we do the search according
        # to the region, else, we do a word wide search

        if any([region, lon, lat]):
            self.region = i_manager.get_region(region, lon, lat)
            timezone.set_request_timezone(self.region)
            response = i_manager.dispatch(args,
                                          "places",
                                          instance_name=self.region)
        else:
            authentication.check_access_to_global_places(user)
            autocomplete = global_autocomplete.get('bragi')
            if autocomplete:
                response = autocomplete.get(args, instance=None)
            else:
                raise TechnicalError(
                    'world wide autocompletion service not available')
        return response, 200
Exemplo n.º 4
0
    def get(self, id, region=None, lon=None, lat=None):
        args = self.parsers["get"].parse_args()
        args.update({
            "uri": transform_id(id),
            "_current_datetime": datetime.utcnow()
        })
        request_id = "places_{}".format(flask.request.id)
        args["request_id"] = request_id

        if any([region, lon, lat]):
            self.region = i_manager.get_region(region, lon, lat)
            timezone.set_request_timezone(self.region)
            response = i_manager.dispatch(args,
                                          "place_uri",
                                          instance_name=self.region)
        else:
            user = authentication.get_user(token=authentication.get_token(),
                                           abort_if_no_token=False)
            available_instances = get_all_available_instances(user)
            autocomplete = global_autocomplete.get('bragi')
            if not autocomplete:
                raise TechnicalError(
                    'world wide autocompletion service not available')
            response = autocomplete.get_by_uri(args["uri"],
                                               request_id=request_id,
                                               instances=available_instances)

        return response, 200
Exemplo n.º 5
0
    def get_regions(self,
                    region_str=None,
                    lon=None,
                    lat=None,
                    object_id=None,
                    api='ALL',
                    only_one=False):
        available_regions = []
        if region_str and self.region_exists(region_str):
            available_regions = [region_str]
        elif lon and lat:
            available_regions = self._all_keys_of_coord(lon, lat)
        elif object_id:
            available_regions = self._all_keys_of_id(object_id)
        else:
            available_regions = self.instances.keys()

        valid_regions = self._filter_authorized_instances(
            available_regions, api)
        if valid_regions:
            return choose_best_instance(
                valid_regions) if only_one else valid_regions
        elif available_regions:
            authentication.abort_request(user=authentication.get_user())
        raise RegionNotFound(region=region_str,
                             lon=lon,
                             lat=lat,
                             object_id=object_id)
Exemplo n.º 6
0
    def fill_request(self, stat_request, start_time, call_result):
        """
        fill stat requests message (protobuf)
        """
        stat_request.request_date = int(start_time)
        # Note: for stat we don't want to abort if no token has been
        # given (it's up to the authentication process)
        token = get_token()
        user = get_user(token=token, abort_if_no_token=False)

        if user is not None:
            stat_request.user_id = user.id
            stat_request.user_name = user.login
            if user.end_point_id:
                stat_request.end_point_id = user.end_point_id
                stat_request.end_point_name = user.end_point.name
        if token is not None:
            stat_request.token = token
        stat_request.application_id = -1
        app_name = get_app_name(token)
        if app_name:
            stat_request.application_name = app_name
        else:
            stat_request.application_name = ''
        stat_request.api = request.endpoint
        stat_request.host = request.host_url
        if request.remote_addr and not request.headers.getlist("X-Forwarded-For"):
            stat_request.client = request.remote_addr
        elif request.headers.getlist("X-Forwarded-For"):
            stat_request.client = request.headers.getlist("X-Forwarded-For")[0]
        stat_request.path = request.path

        stat_request.response_size = sys.getsizeof(call_result[0])

        self.fill_info_response(stat_request.info_response, call_result)
Exemplo n.º 7
0
 def _filter_authorized_instances(self, instances, api):
     if not instances:
         return None
     user = authentication.get_user(token=authentication.get_token())
     valid_instances = [i for i in instances
                        if authentication.has_access(i.name, abort=False, user=user, api=api)]
     if not valid_instances:
         authentication.abort_request(user)
     return valid_instances
Exemplo n.º 8
0
    def get(self, region=None, lon=None, lat=None):
        args = self.parsers["get"].parse_args()
        self._register_interpreted_parameters(args)
        size_q = len(args['q'])
        if size_q == 0:
            abort(400, message="Search word absent")

        if size_q > 1024:
            abort(
                413,
                message="Number of characters allowed for the search is 1024")

        if args['disable_geojson']:
            g.disable_geojson = True

        user = authentication.get_user(token=authentication.get_token(),
                                       abort_if_no_token=False)

        if args['shape'] is None and user and user.shape:
            args['shape'] = json.loads(user.shape)

        if not args.get("shape_scope[]") and user:
            args.update({"shape_scope[]": user.shape_scope})

        if user and user.default_coord:
            if args['from'] is None:
                args['from'] = CoordFormat()(user.default_coord)
        else:
            if args['from'] == '':
                raise InvalidArguments(
                    "if 'from' is provided it cannot be null")

        # If a region or coords are asked, we do the search according
        # to the region, else, we do a word wide search
        args["request_id"] = args.get('request_id', flask.request.id)
        if any([region, lon, lat]):
            self.region = i_manager.get_region(region, lon, lat)

            # when autocompletion is done on a specific coverage we want to filter on its shape
            if not args['shape']:
                instance = i_manager.instances.get(self.region)
                args['shape'] = build_instance_shape(instance)
            timezone.set_request_timezone(self.region)
            response = i_manager.dispatch(args,
                                          "places",
                                          instance_name=self.region)
        else:
            available_instances = get_all_available_instances(user)
            autocomplete = global_autocomplete.get('bragi')
            if not autocomplete:
                raise TechnicalError(
                    'world wide autocompletion service not available')
            response = autocomplete.get(args, instances=available_instances)
        return response, 200
Exemplo n.º 9
0
    def get(self, region=None, lon=None, lat=None, uri=None, id=None):
        collection = self.collection

        args = self.parsers["get"].parse_args()

        if "odt_level" in args and args[
                "odt_level"] != "all" and "lines" not in collection:
            abort(404,
                  message=
                  "bad request: odt_level filter can only be applied to lines")

        if region is None and lat is None and lon is None:
            if "external_code" in args and args["external_code"]:
                type_ = collections_to_resource_type[collection]
                for instance in i_manager.get_regions():
                    res = i_manager.instances[instance].has_external_code(
                        type_, args["external_code"])
                    if res:
                        region = instance
                        id = res
                        break
                if not region:
                    abort(404,
                          message="Unable to find an object for the uri %s" %
                          args["external_code"])
            else:
                abort(503, message="Not implemented yet")
        else:
            user = authentication.get_user(token=authentication.get_token())
            authentication.has_access(region, 'ALL', abort=True, user=user)
        self.region = i_manager.get_region(region, lon, lat)

        #we store the region in the 'g' object, which is local to a request
        set_request_timezone(self.region)

        if not self.region:
            return {"error": "No region"}, 404
        if collection and id:
            args["filter"] = collections_to_resource_type[collection] + ".uri="
            args["filter"] += '"' + id + '"'
        elif uri:
            if uri[-1] == "/":
                uri = uri[:-1]
            uris = uri.split("/")
            if collection is None:
                collection = uris[-1] if len(uris) % 2 != 0 else uris[-2]
            args["filter"] = self.get_filter(uris, args)
        #else:
        #    abort(503, message="Not implemented")
        response = i_manager.dispatch(args,
                                      collection,
                                      instance_name=self.region)
        return response
Exemplo n.º 10
0
def add_info_newrelic(response, *args, **kwargs):
    try:
        record_custom_parameter('navitia-request-id', request.id)
        token = get_token()
        user = get_user(token=token, abort_if_no_token=False)
        app_name = get_app_name(token)
        if user:
            record_custom_parameter('user_id', str(user.id))
        record_custom_parameter('token_name', app_name)
        coverages = get_used_coverages()
        if coverages:
            record_custom_parameter('coverage', coverages[0])
    except:
        logger = logging.getLogger(__name__)
        logger.exception('error while reporting to newrelic:')
    return response
Exemplo n.º 11
0
    def _filter_authorized_instances(self, instances, api):
        if not instances:
            return None
        # During the period database is not accessible, all the instances are valid for the user.
        if not can_connect_to_database():
            return instances

        user = authentication.get_user(token=authentication.get_token())
        valid_instances = [
            i for i in instances if authentication.has_access(
                i.name, abort=False, user=user, api=api)
        ]
        if not valid_instances:
            context = 'User has no access to any instance'
            authentication.abort_request(user, context)
        return valid_instances
Exemplo n.º 12
0
    def get(self, id, region=None, lon=None, lat=None):
        args = self.parsers["get"].parse_args()
        args.update({
            "uri": transform_id(id),
            "_current_datetime": datetime.utcnow()
        })
        request_id = "places_{}".format(flask.request.id)
        args["request_id"] = request_id

        if any([region, lon, lat]):
            self.region = i_manager.get_region(region, lon, lat)
            timezone.set_request_timezone(self.region)
            response = i_manager.dispatch(args,
                                          "place_uri",
                                          instance_name=self.region)
        else:
            user = authentication.get_user(token=authentication.get_token(),
                                           abort_if_no_token=False)
            available_instances = get_all_available_instances(
                user, exclude_backend='kraken')

            # If no instance available most probably due to database error
            if (not user) and (not available_instances):
                raise TechnicalError(
                    'world wide autocompletion service not available temporarily'
                )

            # If parameter '_autocomplete' is absent use 'bragi' as default value
            if args["_autocomplete"] is None:
                args["_autocomplete"] = app.config.get(
                    'DEFAULT_AUTOCOMPLETE_BACKEND', 'bragi')
            autocomplete = global_autocomplete.get(args["_autocomplete"])
            if not autocomplete:
                raise TechnicalError(
                    'world wide autocompletion service not available')
            response = autocomplete.get_by_uri(args["uri"],
                                               request_id=request_id,
                                               instances=available_instances)

        return response, 200
Exemplo n.º 13
0
    def get(self, region=None, lon=None, lat=None, uri=None, id=None):
        collection = self.collection

        args = self.parsers["get"].parse_args()

        # handle headsign
        if args.get("headsign"):
            f = u"vehicle_journey.has_headsign({})".format(
                protect(args["headsign"]))
            if args.get("filter"):
                args["filter"] += " and " + f
            else:
                args["filter"] = f

        # for retrocompatibility purpose
        for forbid_id in args['__temporary_forbidden_id[]']:
            args['forbidden_uris[]'].append(forbid_id)

        if "odt_level" in args and args[
                "odt_level"] != "all" and "lines" not in collection:
            abort(404,
                  message=
                  "bad request: odt_level filter can only be applied to lines")

        if region is None and lat is None and lon is None:
            if "external_code" in args and args["external_code"]:
                type_ = collections_to_resource_type[collection]
                for instance in i_manager.get_regions():
                    res = i_manager.instances[instance].has_external_code(
                        type_, args["external_code"])
                    if res:
                        region = instance
                        id = res
                        break
                if not region:
                    abort(404,
                          message="Unable to find an object for the uri %s" %
                          args["external_code"])
            else:
                abort(503, message="Not implemented yet")
        else:
            user = authentication.get_user(token=authentication.get_token())
            authentication.has_access(region, 'ALL', abort=True, user=user)
        self.region = i_manager.get_region(region, lon, lat)

        #we store the region in the 'g' object, which is local to a request
        set_request_timezone(self.region)

        # change dt to utc
        if args['since']:
            args['_original_since'] = args['since']
            args['since'] = date_to_timestamp(
                self.convert_to_utc(args['since']))
        if args['until']:
            args['_original_until'] = args['until']
            args['until'] = date_to_timestamp(
                self.convert_to_utc(args['until']))

        if not self.region:
            return {"error": "No region"}, 404
        if uri:
            if uri[-1] == "/":
                uri = uri[:-1]
            uris = uri.split("/")
            if collection is None:
                collection = uris[-1] if len(uris) % 2 != 0 else uris[-2]
            args["filter"] = self.get_filter(uris, args)
        if collection and id:
            f = u'{o}.uri={v}'.format(
                o=collections_to_resource_type[collection], v=protect(id))
            if args.get("filter"):
                args["filter"] += " and " + f
            else:
                args["filter"] = f

        response = i_manager.dispatch(args,
                                      collection,
                                      instance_name=self.region)
        return response
Exemplo n.º 14
0
 def get(self):
     user = authentication.get_user(token=authentication.get_token())
     if not user or user and user.login == "unknown_user":
         abort(404, message="User not found", status=404)
     return user, 200