Beispiel #1
0
class CarPositions(APICall):
    """
    Returns a list of cars and their current positions.

    Accepts optional JSON content::

        {
            in_polygon: [(x, y), (x, y), ...],
            car_id: 123
        }
    """
    rules = (
        optional('in_polygon', valid_polygon),
        optional('car_id', valid_car_id),
    )

    @staticmethod
    def filter(car):
        """
        Override to filter out which cars should be displayed by this view.

        Returns True if `car` should be included in the result.
        """
        return True

    @staticmethod
    def get_car_data(cars):
        """
        Override to specify data to send to the front-end application for
        given `car`.
        """
        return [{
            'name': unicode(car),
        } for car in cars]

    @process_request(maybe | parse_json_optional | (validate_request, rules))
    def get(self, request, data):
        bounds = data > maybe | X.get('in_polygon')
        return ((data or {}) > as_kwargs(get_car_position_data)
                | where(X['car'] | self.filter)
                | group_by(X['location'] | (grouping_precision, X, bounds))
                | X.iteritems()
                | foreach({
                    'location': X[1][0]['location'],
                    'cars': X[1] | foreach(X['car']) | self.get_car_data,
                })
                | tuple)

    # POST with JSON content is easier to do in jQuery than GET
    post = get
 def test_invalid(self):
     data = {'some_field': []}
     self.assertEqual(validate(
         optional('some_field', valid_int),
     )(data), (False, '"some_field" should be an integer, not "[]"'))
 def test_valid(self):
     data = {'some_field': 3}
     self.assertEqual(validate(
         optional('some_field', valid_int),
     )(data), OK)
 def test_missing(self):
     data = {}
     self.assertEqual(validate(
         optional('some_field', valid_int),
     )(data), OK)
Beispiel #5
0
class StoreLog(APICall):
    """
    An API method to store one or more log-entries from a car unit.
    """

    rules = (
        required('unit_id', valid_int),
        required('entries'),
        X['entries'] | validate_each(
            required('timestamp', valid_timestamp),
            required('location', valid_location),
            optional('event', valid_string),
            optional('user_id', valid_user_id),
            optional('odometer', valid_float),
            optional('velocity', valid_float),
            optional('consumption', valid_float),
            optional('fuel_remaining', valid_float),
            optional('altitude', valid_float),
            optional('engine_temp', valid_float),
            optional('engine_rpm', valid_float),
            optional('throttle', valid_float),
            optional('gps_accuracy', valid_float),
        ),
    )

    @process_request(pipe
                     | parse_json
                     | authenticate
                     | (validate_request, rules))
    def post(self, request, data):
        store(data['unit_id'], data['entries'])
        return {'status': 'ok'}