Beispiel #1
0
def register(request):
    data = request.data
    device_id = data.get('device_id', None)
    if not device_id:
        return json_response(
            {
                'status': 'error',
                'msg': 'cannot register without device id'
            },
            status=400)

    driver_name = data.get('driver_name', None)
    driver_number = data.get('driver_number', None)
    vehicle_number = data.get('vehicle_number', None)
    vehicle_type = data.get('vehicle_type', None)

    driver_number = None if not driver_number else driver_number.strip()

    created = False
    try:
        driver_app_user = DriverAppUser.objects.get(device_id=device_id)
    except DriverAppUser.DoesNotExist:
        driver_app_user = DriverAppUser(device_id=device_id)
        created = True

    if driver_number and driver_number != driver_app_user.driver_number:
        driver_app_user.number_verified = False
    driver = get_or_none(Driver, phone=driver_number)

    driver_app_user.driver_name = driver_name
    driver_app_user.driver_number = driver_number
    driver_app_user.vehicle_number = vehicle_number
    driver_app_user.vehicle_type = vehicle_type
    driver_app_user.driver = driver
    driver_app_user.save()

    vehicle = set_vehicle_driverapp_user(driver_app_user)
    data = {
        'imei': data.get('device_id', None),
        'device_id': data.get('device_id', None),
        'driver_name': driver_name,
        'driver_number': driver_number,
        'vehicle_number': vehicle_number,
        'vehicle': vehicle.id if isinstance(vehicle, Vehicle) else None,
        'driver': driver.id if isinstance(driver, Driver) else None,
        'vehicle_type': vehicle_type,
        'device_provider': 7
    }
    gps_device_serializer = GPSDeviceSerializer(data=data)
    if gps_device_serializer.is_valid():
        gps_device_serializer.save()
    print(gps_device_serializer.errors)
    data = merge(
        {
            'status': 'success',
            'msg': 'registered' if created else 'registration updated',
            'auth_token': driver_app_user.auth_token,
        }, driver_app_data(driver_app_user))

    return json_response(data)
Beispiel #2
0
    def test_update_setting_empty_custom_settings(self):
        main_dict = {
            "flag": True,
            "section": {
                "flag": True
            }
        }
        custom_dict = {}
        expected = {
            "flag": True,
            "section": {
                "flag": True
            }
        }

        merge(main_dict, custom_dict)
        self.assertEqual(main_dict, expected)
Beispiel #3
0
def get_pod_details(request):
    driver = request.driverapp_user

    data = merge({
        'status': 'success',
        'msg': 'pod details',
    }, driver_app_data(driver))

    return json_response(data)
Beispiel #4
0
def edit_driver_details(request):
    data = request.data
    driver_number = data.get('driver_number', None)
    driver_number = None if not driver_number else driver_number.strip()

    driverapp_user = request.driverapp_user
    driverapp_user.driver_name = data.get('driver_name', None)
    driverapp_user.driver_number = data.get('driver_number', None)
    driverapp_user.vehicle_number = data.get('vehicle_number', None)
    driverapp_user.vehicle_type = data.get('vehicle_type', None)
    driverapp_user.driver = get_or_none(Driver, phone=driver_number)

    if driver_number and driver_number != driverapp_user.driver_number:
        driverapp_user.number_verified = False

    driverapp_user.save()
    driver = get_or_none(Driver, phone=driverapp_user.driver_number)

    vehicle = set_vehicle_driverapp_user(driverapp_user)
    print(vehicle)
    data = {
        'imei': data.get('device_id', None),
        'device_id': data.get('device_id', None),
        'driver_name': data.get('driver_name', None),
        'driver_number': driver_number,
        'vehicle_number': data.get('driver_number', None),
        'vehicle': vehicle.id if isinstance(vehicle, Vehicle) else None,
        'driver': driver.id if isinstance(driver, Driver) else None,
        'vehicle_type': data.get('vehicle_type', None),
        'device_provider': 7
    }
    gps_device_serializer = GPSDeviceSerializer(data=data)
    if gps_device_serializer.is_valid():
        gps_device_serializer.save()
    print(gps_device_serializer.errors)
    data = merge(
        {
            'status': 'success',
            'msg': 'details edited',
            'auth_token': driverapp_user.auth_token,
        }, driver_app_data(driverapp_user))

    return json_response(data)
Beispiel #5
0
def edit_vehicle_status(request):
    data = request.data

    vehicle_status = data.get('vehicle_status', None)
    if vehicle_status not in dict(VEHICLE_STATUS_CHOICES):
        return json_response(
            {
                'status': 'error',
                'msg': 'not a valid vehicle_status'
            },
            status=400)

    driver = request.driverapp_user
    driver.vehicle_status = vehicle_status
    driver.save()
    if isinstance(driver, DriverAppUser):
        try:
            gps_device = GPSDevice.objects.get(device_id=driver.device_id,
                                               device_provider_id=7)
            gps_device_serializer = GPSDeviceSerializer(
                partial=True,
                instance=gps_device,
                data={'vehicle_status': vehicle_status})
            if gps_device_serializer.is_valid():
                gps_device_serializer.save()
        except GPSDevice.DoesNotExist:
            pass

    if driver.number_verified:
        Vehicle.objects.filter(driver_app_user=driver).update(
            status=driver.vehicle_status)

    data = merge({
        'status': 'success',
        'msg': 'vehicle_status updated',
    }, driver_app_data(driver))

    return json_response(data)
Beispiel #6
0
def merge_dict_schema(schema1, schema2):
    return dict_schema(**merge(schema1.get('properties', {}), schema2.get('properties', {})))