Exemplo n.º 1
0
def api_links(request):
    if request.method == 'GET':

        params = extract_params(request)
        _limit = _uuid = _q = None

        if params:
            if 'limit' in params.keys() and int(params['limit']):
                _limit = int(params['limit'])
            if 'uuid' in params.keys():
                _uuid = params['uuid']

        if _uuid or _q:
            return JSONResponse(LinkSerializer(
                Link.objects.filter(
                    uuid__exact=_uuid,
                )[0:_limit],
                many=True
            ).data)
        else:
            return JSONResponse(LinkSerializer(
                Link.objects.all()[0:_limit],
                many=True
            ).data)
    else:
        return JSONResponse(None, status=400)
Exemplo n.º 2
0
def message():
    """
       This function checks the command received.
       Based on the command, it dynamically creates the URL and
       makes a request to the NextBus service

       """
    data = Response('Bad Request!Missing Parameters',
                    status=400,
                    mimetype='text')
    command, agency_tag = extract_params(request)
    #    If the params do not contain command and agency_tag an error is returned
    if not command or not agency_tag:
        return data
    message_url = BASE_URL + command + '&a=' + agency_tag
    if command == 'messages':
        # If no route is specified, get all messages
        if request.args.get('r'):
            routes = request.args.getlist('r')
            for route in routes:
                message_url = message_url + "&r=" + route
        data, response_time = proxy_request(message_url)
    elif command == 'vehicleLocations':
        route = request.args.get('r')
        # If no route is specified, get vehicleLocations for all routes
        if route is not None:
            message_url = message_url + '&r=' + route
        time = request.args.get('t')
        if time is None:
            time = '0'
        message_url = message_url + '&t=' + time
        data, response_time = proxy_request(message_url)
    update_count_db(message_url, response_time)
    return data
Exemplo n.º 3
0
    def available_params(self):
        """
            Merge parameter values into a dictionary of available parameters

        :param param_values: A dictionary of Query param values.
        :return: A merged dictionary of parameter names and values. Values of non-existent parameters are removed.
        """

        p = extract_params(self.sql)
        if self.params:
            shared_dict_update(p, self.params)
        return p
Exemplo n.º 4
0
def prediction():
    """
        This function checks the command received.
        Based on the command, it dynamically creates the URL and
        makes a request to the NextBus service

        """
    data = Response('Bad Request!Missing parameters',
                    status=400,
                    mimetype='text')
    command, agency_tag = extract_params(request)
    # If request params do not contain a command or an agency, return an error
    if not command or not agency_tag:
        return data
    prediction_url = BASE_URL + command + '&a=' + agency_tag
    # Set useShortTitles flag based on the request params and update the request URL accordingly
    if request.args.get('useShortTitles'):
        use_short_titles = request.args.get('useShortTitles')
        prediction_url = prediction_url+'&useShortTitles=' + use_short_titles
    if command == 'predictions':
        # Predictions has 2 contracts. Form the required one based on the request params
        if request.args.get('stopId'):
            stop_id = request.args.get('stopId')
            prediction_url = prediction_url + '&stopId=' + stop_id
            if request.args.get('r'):
                route = request.args.get('r')
                prediction_url = prediction_url + '&r=' + route
            data, response_time = proxy_request(prediction_url)
        elif request.args.get('s'):
            stop_tag = request.args.get('s')
            prediction_url = prediction_url + '&s=' + stop_tag
            if request.args.get('r'):
                route = request.args.get('r')
                prediction_url = prediction_url + '&r=' + route
            data, response_time = proxy_request(prediction_url)
    elif command == 'predictionsForMultiStops':
        if not request.args.get('stops'):
            return data
        stops = request.args.getlist('stops')
        for stop in stops:
            prediction_url = prediction_url+"&stops="+stop
        data, response_time = proxy_request(prediction_url)
    elif command == 'schedule':
        route = request.args.get('r')
        if route is None:
            return data
        prediction_url = prediction_url+'&r='+route
        data, response_time = proxy_request(prediction_url)
    update_count_db(prediction_url, response_time)
    return data
Exemplo n.º 5
0
def api_resources(request):
    if request.method == 'GET':

        params = extract_params(request)
        _limit = _uuid = _q = None
        resources = None

        if params:
            if 'limit' in params.keys() and int(params['limit']):
                _limit = int(params['limit'])
            if 'uuid' in params.keys():
                _uuid = params['uuid']
            if 'q' in params.keys() and len(params['q']) > 0:
                _q = HTML_PARSER.unescape(
                    urllib.unquote(params['q'])
                ).replace('+', ' ')

        if _q and len(_q) > 0:
            return JSONResponse(ResourceSerializer(
                Resource.objects.filter(
                    name__contains=_q
                )[0:_limit],
                many=True
            ).data)
        elif _uuid or _q:
            return JSONResponse(ResourceSerializer(
                Resource.objects.filter(
                    uuid__exact=_uuid,
                )[0:_limit],
                many=True
            ).data)
        else:
            return JSONResponse(ResourceSerializer(
                Resource.objects.all()[0:_limit],
                many=True
            ).data)
    else:
        return JSONResponse(None, status=400)