def test_driver_location(self): """test for driver location""" # true serializer = DriverProfileSerializer(data={'account': {'username': '******', 'password': '******'}, 'profile_photo': img_file(), 'phone_number': 1234, 'vehicle_type': 'B', 'is_available': True, 'live_location_longitude': 30, 'live_location_latitude': 30}) self.assertTrue(serializer.is_valid()) # wrong longitude serializer = DriverProfileSerializer(data={'account': {'username': '******', 'password': '******'}, 'profile_photo': img_file(), 'phone_number': 1234, 'vehicle_type': 'B', 'is_available': True, 'live_location_longitude': 1000, 'live_location_latitude': 30}) self.assertFalse(serializer.is_valid()) # wrong latitude serializer = DriverProfileSerializer(data={'account': {'username': '******', 'password': '******'}, 'profile_photo': img_file(), 'phone_number': 1234, 'vehicle_type': 'B', 'is_available': True, 'live_location_longitude': 30, 'live_location_latitude': -200}) self.assertFalse(serializer.is_valid())
def partial_update(self, request, username=None): """Partially Updates the driver profile. Arguments: request: the request data sent by the user, it is used to check the user's permissions and get the data username: the username of the driver profile that will be updated Returns: HTTP 404 Response if driver profile is not found, HTTP 400 Response if the data is not valid with the errors, HTTP 403 Response if the user is not authorized to update that profile, if not returns HTTP 200 Response with the update JSON data. """ driver_profile = get_object_or_404(DriverProfileModel, account__username=username) self.check_object_permissions(request, driver_profile) serializer = DriverProfileSerializer(driver_profile, data=request.data, partial=True) if serializer.is_valid(): serializer.save() update_session_auth_hash(request, driver_profile.account) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def create(self, request): """Creates A new driver profile and Logs it In. Checks if user is authenticated if true, return HTTP 401 Response, then it Validates the post data if not valid, return HTTP 400 Response, then creates the driver and logs him in, and returns 201 Response. Arguments: request: the request data sent by the user, it is used to get the post data from it to get validated and created, and to log the driver in. Returns: HTTP 400 Response if data is not valid, HTTP 401 Response if user is already logged in, HTTP 201 Response with the JSON data of the created profile. """ if not request.user.is_authenticated: serializer = DriverProfileSerializer(data=request.data) if serializer.is_valid(): driver_profile = serializer.save() login(request, driver_profile.account) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) return Response(status=status.HTTP_401_UNAUTHORIZED)