Example #1
0
    def put(self, taxi_id):
        hj = request.json
        t, last_update_at = self.get_descriptions(taxi_id)
        new_status = hj['data'][0]['status']
        if new_status != t[0]['vehicle_description_status'] or\
                t[0]['taxi_last_update_at'] is None or\
                t[0]['taxi_last_update_at'] <= (datetime.now() - timedelta(hours=4)):
            cur = current_app.extensions['sqlalchemy'].db.session.\
                    connection().connection.cursor()
            cur.execute("UPDATE vehicle_description SET status=%s WHERE id=%s",
                           (new_status, t[0]['vehicle_description_id'])
            )
            cur.execute("UPDATE taxi set last_update_at = %s WHERE id = %s",
                    (datetime.now(), t[0]['taxi_id'])
            )
            current_app.extensions['sqlalchemy'].db.session.commit()
            taxis_models.RawTaxi.flush(taxi_id)
            t[0]['vehicle_description_status'] = new_status
            taxi_id_operator = "{}:{}".format(taxi_id, current_user.email)
            if t[0]['vehicle_description_status'] == 'free':
                redis_store.srem(current_app.config['REDIS_NOT_AVAILABLE'],
                    taxi_id_operator)
            else:
                redis_store.sadd(current_app.config['REDIS_NOT_AVAILABLE'],
                    taxi_id_operator)

        taxi_m = marshal({'data':[
            taxis_models.RawTaxi.generate_dict(t, operator=current_user.email)]
            }, taxi_model)
        taxi_m['data'][0]['operator'] = current_user.email
        taxi_m['data'][0]['last_update'] = last_update_at
        return taxi_m
Example #2
0
 def post_json(self):
     json = request.get_json()
     if "data" not in json:
         abort(400, message="No data field in request")
     if len(json['data']) > 250:
         abort(413, message="You can only pass 250 objects")
     edited_ads_id = []
     new_ads = []
     for ads in json['data']:
         if ads['vehicle_id'] and\
           not taxis_models.Vehicle.query.get(ads['vehicle_id']):
             abort(400, message="Unable to find a vehicle with the id: {}"\
                     .format(ads['vehicle_id']))
         try:
             ads_db = create_obj_from_json(taxis_models.ADS, ads)
         except KeyError as e:
             abort(400, message="Missing key: "+str(e))
         except AssertionError as e:
             abort(400, message='Bad owner_type value, can be: {}'.format(
                 taxis_models.owner_type_enum
                 ))
         zupc = administrative_models.ZUPC.query.filter_by(insee=ads_db.insee).first()
         if zupc is None:
             abort(400, message="Unable to find a ZUPC for insee: {}".format(
                 ads_db.insee))
         ads_db.zupc_id = zupc.parent_id
         db.session.add(ads_db)
         if ads_db.id:
             edited_ads_id.append(ads.id)
         new_ads.append(ads)
     db.session.commit()
     return marshal({"data": new_ads}, ads_post), 201
Example #3
0
 def post_json(self):
     json = request.get_json()
     if "data" not in json:
         abort(400, message="You need data a data object")
     if len(json['data']) > 250:
         abort(413, message="You've reach the limits of 250 objects")
     edited_drivers_id = []
     new_drivers = []
     for driver in json['data']:
         departement = None
         if 'numero' in driver['departement']:
             departement = administrative_models.Departement.\
                 filter_by_or_404(numero=driver['departement']['numero'])
         elif 'nom' in driver['departement']:
             departement = administrative_models.Departement.\
                 filter_by_or_404(nom=driver['departement']['nom'])
         try:
             driver_obj = create_obj_from_json(taxis_models.Driver, driver)
             driver_obj.departement_id = departement.id
         except KeyError as e:
             abort(400, message="Key error")
         db.session.add(driver_obj)
         if driver_obj.id:
             edited_drivers_id.append(driver_obj.id)
         new_drivers.append(driver_obj)
     db.session.commit()
     return marshal({'data': new_drivers}, driver_fields), 201
 def test_marshal_nested_property(self):
     class TestObject(object):
         @property
         def fee(self):
             return {'blah': 'cool'}
     model = OrderedDict([
         ('foo', fields.Raw),
         ('fee', fields.Nested(
             OrderedDict([
                 ('fye', fields.String),
                 ('blah', fields.String)
             ]), allow_null=True))
     ])
     obj = TestObject()
     obj.foo = 'bar'
     obj.bat = 'baz'
     output = marshal([obj], model)
     expected = [OrderedDict([
         ('foo', 'bar'),
         ('fee', OrderedDict([
             ('fye', None),
             ('blah', 'cool')
         ]))
     ])]
     self.assertEquals(output, expected)
Example #5
0
 def get(self, taxi_id):
     t, last_update_at = self.get_descriptions(taxi_id)
     taxi_m = marshal({'data':[
         taxis_models.RawTaxi.generate_dict(t,
         operator=current_user.email)]}, taxi_model)
     taxi_m['data'][0]['operator'] = current_user.email
     taxi_m['data'][0]['last_update'] = last_update_at
     return taxi_m
Example #6
0
def export_taxis(filename='/tmp/taxis.tar.gz'):
    model_ads = make_model('taxis', 'ADS', True)
    model_driver = make_model('taxis', 'Driver')
    model_vehicle = make_model('taxis', 'Vehicle', filter_id=True)
    tar = tarfile.TarFile.open(filename, 'w:gz')
    users = set()

    for taxi in Taxi.query.all():
        if taxi.vehicle_id is None or taxi.ads_id is None or taxi.driver_id is None:
            continue
        vehicle = Vehicle.query.get(taxi.vehicle_id)
        for vehicle_description in VehicleDescription.query.filter_by(vehicle_id=vehicle.id).all():
            users.add(vehicle_description.added_by)
            login_user(user_datastore.get_user(vehicle_description.added_by))
            json_vehicle = json.dumps(marshal({"data":[vehicle]}, model_vehicle))
            tarinfo = tarfile.TarInfo('{}/vehicle_{}_{}.json'.format(taxi.id,
                vehicle.id,vehicle_description.added_by))
            tarinfo.size = len(json_vehicle)
            tar.addfile(tarinfo, StringIO.StringIO(json_vehicle))

        ads = ADS.query.get(taxi.ads_id)
        json_ads = json.dumps(marshal({"data":[ads]}, model_ads))
        tarinfo = tarfile.TarInfo('{}/ads.json'.format(taxi.id))
        tarinfo.size = len(json_ads)
        tar.addfile(tarinfo, StringIO.StringIO(json_ads))

        driver = Driver.query.get(taxi.driver_id)
        json_driver = json.dumps(marshal({"data":[driver]}, model_driver))
        tarinfo = tarfile.TarInfo('{}/driver.json'.format(taxi.id))
        tarinfo.size = len(json_driver)
        tar.addfile(tarinfo, StringIO.StringIO(json_driver))

    users_dict = dict()
    for user_id in users:
        user = user_datastore.get_user(user_id)
        users_dict[user_id] = user.email
    users_json = json.dumps(users_dict)
    tarinfo = tarfile.TarInfo('users.json')
    tarinfo.size = len(users_json)
    tar.addfile(tarinfo, StringIO.StringIO(users_json))

    tar.close()
 def test_marshal_list(self):
     model = OrderedDict([
         ('foo', fields.Raw),
         ('fee', fields.List(fields.String))
     ])
     marshal_fields = OrderedDict([('foo', 'bar'),
                                   ('bat', 'baz'),
                                   ('fee', ['fye', 'fum'])])
     output = marshal(marshal_fields, model)
     expected = OrderedDict([('foo', 'bar'), ('fee', (['fye', 'fum']))])
     self.assertEquals(output, expected)
Example #8
0
 def get(self, hail_id):
     hail = HailModel.get_or_404(hail_id)
     self.filter_access(hail)
     return_ = marshal({"data": [hail]},hail_model)
     return_['data'][0]['taxi']['crowfly_distance'] = vincenty(
             (return_['data'][0]['taxi']['position']['lat'],
             return_['data'][0]['taxi']['position']['lon']),
             (return_['data'][0]['customer_lat'],
              return_['data'][0]['customer_lon'])
             ).kilometers
     return return_
Example #9
0
 def get(self, taxi_id):
     taxi = taxis_models.Taxi.cache.get(taxi_id)
     if not taxi:
         abort(404, message="Unable to find this taxi")
     description = taxi.vehicle.description
     if not description:
         abort(403, message="You're not authorized to view this taxi")
     taxi_m = marshal({'data':[taxi]}, taxi_model)
     taxi_m['data'][0]['operator'] = current_user.email
     op, timestamp = taxi.get_operator(favorite_operator=current_user.email)
     taxi_m['data'][0]['last_update'] = timestamp if op == current_user else None
     return taxi_m
 def post(self, event_id):
     from helpers.tasks import export_event_task
     task = export_event_task.delay(
         event_id, marshal(self.api.payload, EXPORT_SETTING))
     if current_app.config.get('CELERY_ALWAYS_EAGER'):
         TASK_RESULTS[task.id] = {
             'result': task.get(),
             'state': task.state
         }
     return jsonify(
         task_url=url_for('api.extras_celery_task', task_id=task.id)
     )
def import_event_task_base(task_handle, file_path, source_type='json', creator_id=None):
    new_event = None
    if source_type == 'json':
        new_event = import_event_json(task_handle, file_path)
    elif source_type == 'pentabarf':
        new_event = PentabarfImporter.import_data(file_path=file_path, task_handle=task_handle, creator_id=creator_id)
    elif source_type == 'ical':
        new_event = ICalImporter.import_data(file_path=file_path, task_handle=task_handle, creator_id=creator_id)
    if new_event:
        record_activity('import_event', event_id=new_event.id)
        return marshal(new_event, EVENT)
    else:
        return None
 def test_marshal_list_of_nesteds(self):
     model = OrderedDict([
         ('foo', fields.Raw),
         ('fee', fields.List(fields.Nested({
             'fye': fields.String
         })))
     ])
     marshal_fields = OrderedDict([('foo', 'bar'),
                                   ('bat', 'baz'),
                                   ('fee', {'fye': 'fum'})])
     output = marshal(marshal_fields, model)
     expected = OrderedDict([('foo', 'bar'),
                             ('fee', [OrderedDict([('fye', 'fum')])])])
     self.assertEquals(output, expected)
 def test_marshal_nested_with_null(self):
     model = OrderedDict([
         ('foo', fields.Raw),
         ('fee', fields.Nested(
             OrderedDict([
                 ('fye', fields.String),
                 ('blah', fields.String)
             ]), allow_null=True))
     ])
     marshal_fields = OrderedDict([('foo', 'bar'),
                                   ('bat', 'baz'),
                                   ('fee', None)])
     output = marshal(marshal_fields, model)
     expected = OrderedDict([('foo', 'bar'), ('fee', None)])
     self.assertEquals(output, expected)
 def test_marshal_nested_dict(self):
     model = OrderedDict([
         ('foo', fields.Raw),
         ('bar', OrderedDict([
             ('a', fields.Raw),
             ('b', fields.Raw),
         ])),
     ])
     marshal_fields = OrderedDict([('foo', 'foo-val'),
                                   ('bar', 'bar-val'),
                                   ('bat', 'bat-val'),
                                   ('a', 1), ('b', 2), ('c', 3)])
     output = marshal(marshal_fields, model)
     expected = OrderedDict([('foo', 'foo-val'),
                             ('bar', OrderedDict([('a', 1), ('b', 2)]))])
     self.assertEquals(output, expected)
Example #15
0
def zupc_search():
    parser = reqparse.RequestParser()
    parser.add_argument('lon', type=float, required=True, location='args')
    parser.add_argument('lat', type=float, required=True, location='args')
    try:
        args = parser.parse_args()
    except BadRequest as e:
        return json.dumps(e.data), 400

    id_list = index_zupc.intersection(args['lon'], args['lat'])
    to_return = []
    if id_list:
        ZUPC = administrative_models.ZUPC
        zupc_list = ZUPC.query.filter(ZUPC.id.in_(id_list)).all()
#Level is one, because we don't want to have parent in the response
        to_return = marshal(zupc_list, ZUPC.marshall_obj(filter_id=True, level=1))
    return json.dumps({"data": to_return})
Example #16
0
 def post_json(self):
     json = request.get_json()
     if "data" not in json:
         abort(400, message="No data field in request")
     if len(json['data']) > 250:
         abort(413, message="You can only pass 250 objects")
     edited_ads_id = []
     new_ads = []
     db = current_app.extensions['sqlalchemy'].db
     for ads in json['data']:
         if not ads.get('vehicle_id', None) or ads['vehicle_id'] == 0:
             ads['vehicle_id'] = None
         if ads['vehicle_id'] and\
           not taxis_models.Vehicle.query.get(ads['vehicle_id']):
             abort(400, message="Unable to find a vehicle with the id: {}"\
                     .format(ads['vehicle_id']))
         try:
             ads_db = create_obj_from_json(taxis_models.ADS, ads)
         except KeyError as e:
             abort(400, message="Missing key: "+str(e))
         except AssertionError as e:
             abort(400, message='Bad owner_type value, can be: {}'.format(
                 taxis_models.owner_type_enum
                 ))
         zupc = administrative_models.ZUPC.query.filter_by(insee=ads_db.insee).first()
         if zupc is None:
             abort(400, message="Unable to find a ZUPC for insee: {}".format(
                 ads_db.insee))
         ads_db.zupc_id = zupc.parent_id
         db.session.add(ads_db)
         if ads_db.id:
             edited_ads_id.append(ads.id)
         new_ads.append(ads_db)
     db.session.commit()
     for ads in new_ads:
         cur = db.session.connection().connection.cursor()
         cur.execute("""
             UPDATE taxi set ads_id = %s WHERE ads_id IN (
                 SELECT id FROM "ADS"  WHERE numero = %s
                 AND insee = %s
             )""",
             (ads.id, ads.numero, ads.insee)
         )
     db.session.commit()
     return marshal({"data": new_ads}, ads_post), 201
 def test_allow_null_presents_data(self):
     model = OrderedDict([
         ('foo', fields.Raw),
         ('fee', fields.Nested(
             OrderedDict([
                 ('fye', fields.String),
                 ('blah', fields.String)
             ]), allow_null=True))
     ])
     marshal_fields = OrderedDict([('foo', 'bar'),
                                   ('bat', 'baz'),
                                   ('fee', {'blah': 'cool'})])
     output = marshal(marshal_fields, model)
     expected = OrderedDict([
         ('foo', 'bar'),
         ('fee', OrderedDict([('fye', None), ('blah', 'cool')]))
     ])
     self.assertEquals(output, expected)
Example #18
0
def send_request_operator(hail_id, endpoint, operator_header_name,
        operator_api_key, operator_email):
    operator_api_key = operator_api_key.encode('utf-8')
    operator_header_name = operator_header_name.encode('utf-8')
    hail = Hail.cache.get(hail_id)
    if not hail:
        current_app.logger.error('Unable to find hail: {}'.format(hail_id))
        return False

    r = None
    try:
        headers = {'Content-Type': 'application/json',
                   'Accept': 'application/json'}
        if operator_header_name is not None and operator_header_name != '':
            headers[operator_header_name] = operator_api_key
        data = json.dumps(marshal({"data": [hail]}, hail_model))
        hail_log = HailLog('POST to operator', hail, data)
        r = requests.post(endpoint,
                data=data,
                headers=headers
        )
    except requests.exceptions.MissingSchema:
        pass
    hail_log.store(r, redis_store)
    if not r or r.status_code < 200 or r.status_code >= 300:
        hail.status = 'failure'
        current_app.extensions['sqlalchemy'].db.session.commit()
        current_app.logger.error("Unable to reach hail's endpoint {} of operator {}"\
            .format(endpoint, operator_email))
        return False
    r_json = None
    try:
        r_json = r.json()
    except ValueError:
        pass

    if r_json and 'data' in r_json and len(r_json['data']) == 1\
            and 'taxi_phone_number' in r_json['data'][0]:
        hail.taxi_phone_number = r_json['data'][0]['taxi_phone_number']

    hail.status = 'received_by_operator'
    current_app.extensions['sqlalchemy'].db.session.commit()
    return True
Example #19
0
 def post(self, event_id):
     from helpers.tasks import export_event_task
     # queue task
     task = export_event_task.delay(
         event_id, marshal(self.api.payload, EXPORT_SETTING))
     # create Job
     try:
         create_export_job(task.id, event_id)
     except Exception:
         pass
     # in case of testing
     if current_app.config.get('CELERY_ALWAYS_EAGER'):
         send_export_mail(event_id, task.get())
         TASK_RESULTS[task.id] = {
             'result': task.get(),
             'state': task.state
         }
     return jsonify(
         task_url=url_for('api.extras_celery_task', task_id=task.id)
     )
Example #20
0
def send_request_operator(hail_id, operateur_id, env):
    hail = Hail.cache.get(hail_id)
    if not hail:
        current_app.logger.error('Unable to find hail: {}'.format(hail_id))
        return False
    operateur = User.query.get(operateur_id)
    if not operateur:
        current_app.logger.error('Unable to find operateur: {}'.format(operateur_id))
        return False

    r = None
    endpoint = operateur.hail_endpoint(env)
    try:
        headers = {'Content-Type': 'application/json',
                   'Accept': 'application/json'}
        if operateur.operator_header_name is not None and operateur.operator_header_name != '':
            headers[operateur.operator_header_name] = operateur.operator_api_key
        r = requests.post(endpoint, data=json.dumps(marshal({"data": [hail]}, hail_model)),
            headers=headers)
    except requests.exceptions.MissingSchema:
        pass
    if not r or r.status_code < 200 or r.status_code >= 300:
        hail.status = 'failure'
        db.session.commit()
        current_app.logger.error("Unable to reach hail's endpoint {} of operator {}"\
            .format(endpoint, operateur.email))
        return False
    r_json = None
    try:
        r_json = r.json()
    except ValueError:
        pass

    if r_json and 'data' in r_json and len(r_json['data']) == 1\
            and 'taxi_phone_number' in r_json['data'][0]:
        hail.taxi_phone_number = r_json['data'][0]['taxi_phone_number']

    hail.status = 'received_by_operator'
    db.session.commit()
    return True
Example #21
0
    def post_json(self):
        json = request.get_json()
        if "data" not in json:
            abort(400, message="You need data a data object")
        if len(json['data']) > 250:
            abort(413, message="You've reach the limits of 250 objects")
        edited_drivers_id = []
        new_drivers = []
        db = current_app.extensions['sqlalchemy'].db
        for driver in json['data']:
            departement = None
            if 'numero' in driver['departement']:
                departement = administrative_models.Departement.\
                    filter_by_or_404(numero=driver['departement']['numero'])
            elif 'nom' in driver['departement']:
                departement = administrative_models.Departement.\
                    filter_by_or_404(nom=driver['departement']['nom'])
            try:
                driver_obj = create_obj_from_json(taxis_models.Driver, driver)
                driver_obj.departement_id = departement.id
            except KeyError as e:
                abort(400, message="Key error")
            db.session.add(driver_obj)
            if driver_obj.id:
                edited_drivers_id.append(driver_obj.id)
            new_drivers.append(driver_obj)
        db.session.commit()
        for driver in new_drivers:
            cur = db.session.connection().connection.cursor()
            cur.execute("""
                UPDATE taxi set driver_id = %s WHERE driver_id IN (
                    SELECT id FROM driver WHERE professional_licence = %s
                    AND departement_id = %s
                )""",
                (driver.id, driver.professional_licence, driver.departement_id)
            )
        db.session.commit()

        return marshal({'data': new_drivers}, driver_fields), 201
Example #22
0
    def get(self):
        if not request_wants_json():
            abort(400, message="request needs JSON")
        parser = reqparse.RequestParser()
        parser.add_argument('lon', type=float, required=True, location='args')
        parser.add_argument('lat', type=float, required=True, location='args')
        try:
            args = parser.parse_args()
        except BadRequest as e:
            return json.dumps(e.data), 400, {"Content-Type": "application/json"}

        cur = current_app.extensions['sqlalchemy'].db.session.connection()\
                .connection.cursor(cursor_factory=RealDictCursor)
        cur.execute("""SELECT active, nom, insee FROM "ZUPC"
            WHERE ST_Intersects(shape, ST_POINT(%s, %s)::geography)""",
            (args['lon'], args['lat']))
        to_return = []
        ZUPC = administrative_models.ZUPC
        for zupc in cur.fetchall():
            if any(map(lambda z: zupc['insee'] == z['insee'], to_return)):
                continue
            to_return.append(marshal(zupc, ZUPC.marshall_obj(filter_id=True,
                level=1, api=api)))
        return {"data": to_return}, 200
 def test_marshal_tuple(self):
     model = OrderedDict({'foo': fields.Raw})
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = marshal((marshal_fields,), model)
     self.assertEquals(output, [{'foo': 'bar'}])
 def test_marshal_field(self):
     model = OrderedDict({'foo': fields.Raw()})
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = marshal(marshal_fields, model)
     self.assertEquals(output, {'foo': 'bar'})
 def get(self):
     return marshal({"foo": 3.0}, self.fields)
 def test_marshal_with_envelope(self):
     model = OrderedDict([('foo', fields.Raw)])
     marshal_dict = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = marshal(marshal_dict, model, envelope='hey')
     self.assertEquals(output, {'hey': {'foo': 'bar'}})
Example #27
0
 def post(self):
     file = get_file_from_request(['zip'])
     new_event = import_event_json(file)
     return marshal(new_event, EVENT)
Example #28
0
 def get(self, **kwargs):
     '''Get a given object'''
     obj = self.get_or_404(**kwargs)
     return marshal(obj, self.fields)
Example #29
0
 def post(self):
     file = get_file_from_request(['zip'])
     new_event = import_event_json(file)
     record_activity('import_event', event_id=new_event.id)
     return marshal(new_event, EVENT)
Example #30
0
 def post(self):
     '''Create a new object'''
     form = api.validate(self.form)
     return marshal(form.save(), self.fields), 201
Example #31
0
 def get(self, **kwargs):
     '''Get a given object'''
     obj = self.get_or_404(**kwargs)
     return marshal(obj, self.fields)
Example #32
0
 def put(self, **kwargs):
     '''Update a given object'''
     obj = self.get_or_404(**kwargs)
     form = api.validate(self.form, obj)
     return marshal(form.save(), self.fields)
 def get(self, event_id):
     """Fetch an event given its id.
     Alternate endpoint for fetching an event.
     """
     includes = parse_args(self.event_parser).get('include', '').split(',')
     return marshal(DAO.get(event_id), get_extended_event_model(includes))
 def test_marshal_tuple_with_envelope(self):
     model = OrderedDict({'foo': fields.Raw})
     marshal_fields = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = marshal((marshal_fields,), model, envelope='hey')
     self.assertEquals(output, {'hey': [{'foo': 'bar'}]})
 def test_marshal(self):
     model = OrderedDict([('foo', fields.Raw)])
     marshal_dict = OrderedDict([('foo', 'bar'), ('bat', 'baz')])
     output = marshal(marshal_dict, model)
     self.assertEquals(output, {'foo': 'bar'})
Example #36
0
 def post(self):
     '''Create a new object'''
     form = api.validate(self.form)
     return marshal(form.save(), self.fields), 201
 def get(self):
     """List all events"""
     parsed_args = parse_args(self.event_parser)
     includes = parsed_args.get('include', '').split(',')
     erase_from_dict(parsed_args, 'include')
     return marshal(DAO.list(**parsed_args), get_extended_event_model(includes))
Example #38
0
 def put(self, **kwargs):
     '''Update a given object'''
     obj = self.get_or_404(**kwargs)
     form = api.validate(self.form, obj)
     return marshal(form.save(), self.fields)
def import_event_task_base(task_handle, file):
    new_event = import_event_json(task_handle, file)
    record_activity('import_event', event_id=new_event.id)
    return marshal(new_event, EVENT)