def clients(request, format=None): if request.method == "GET": return Response(get_serialized_model_objects(Client, ClientSerializer)) elif request.method == "POST": serializer = ClientSerializer(data=request.data) serializer.is_valid(raise_exception=True) client = serializer.save(user=request.user) return Response({"client_id": client.id}, status=status.HTTP_201_CREATED)
def test_validate_iban(self, iban_account, error_message): """Tests IBAN account validator. Args: iban_account (str): Client IBAN account. error_message (str): Expected error message. """ try: validated_iban = ClientSerializer.validate_iban(iban_account) self.assertEqual(validated_iban, iban_account) except ValidationError as e: self.assertEqual(str(e.args[0]), error_message)
def test_validate_surname(self, surname, error_message): """Tests surname validator. Args: surname (str): Client surname. error_message (str): Expected error message. """ try: validated_surname = ClientSerializer.validate_surname(surname) self.assertEqual(validated_surname, surname) except ValidationError as e: self.assertEqual(e.args[0], error_message)
def test_get_valid_single_client(self): client = Client.objects.create( code="CL004", first_name="client4", last_name="client4", address="Batna, Algeria", date_of_birth="1990-01-01", mobile_phone1="0123456789", ) response = self.client.get( reverse(CLIENT_DETAIL, kwargs={"pk": client.pk}) ) serializer = ClientSerializer(client) self.assertEqual(response.data, serializer.data) self.assertEqual(response.status_code, status.HTTP_200_OK)
def test_retrieve_clients_list(self): # Test retrieving a list of clients Client.objects.create( code="CL001", first_name="client1", last_name="client1", address="Batna, Algeria", date_of_birth="1990-01-01", mobile_phone1="0123456789", ) Client.objects.create( code="CL002", first_name="client2", last_name="client2", address="Batna, Algeria", date_of_birth="1990-01-01", mobile_phone1="0123456789", ) response = self.client.get(CLIENTS_URL) clients = Client.objects.all().order_by("id") serializer = ClientSerializer(clients, many=True) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, serializer.data)
def calculate_route(client_id=None): mapquest_url = "https://www.mapquestapi.com/directions/v2/routematrix?key=%s" % settings.MAPQUEST_KEY request_body = {"options": {"allToAll": True}} locations = [] current_driver_location = DriverLocation.objects.filter( latest=True).order_by("-timestamp") current_driver_location = current_driver_location[0] locations.append({ "latLng": { "lat": current_driver_location.latitude, "lng": current_driver_location.longitude, }, "user": "******", "custom_type": "start" }) for location in Ride.objects.filter( active=True, deleted=False).order_by("request_received_at")[:8]: if location.serviced_by: locations.append({ "latLng": { "lat": location.drop_latitude, "lng": location.drop_longitude }, "user": ClientSerializer(location.client).data, "custom_type": "drop", "ride_id": location.pk, "request_time": location.request_received_at.strftime("%s"), "pickup_at": location.pickup_at.strftime("%s") }) else: locations.append({ "latLng": { "lat": location.pickup_latitude, "lng": location.pickup_longitude }, "user": ClientSerializer(location.client).data, "custom_type": "pick", "ride_id": location.pk, "request_time": location.request_received_at.strftime("%s"), "pickup_at": None }) location.request_processed_at = timezone.now() request_body['locations'] = locations if len(locations) > 0: response = requests.post(mapquest_url, data=json.dumps(request_body)) if response.status_code == 200: try: time_matrix = json.loads(response.content)['time'] except: return None, None path = [i for i in range(0, len(time_matrix))] cost_matrix = time_matrix[0] # path, cost_matrix = dijkstra(time_matrix) eta = 0 path_in_co_ordinates = [{ "latLng": locations[0]['latLng'], "user": "******", "eta": eta }] for index in range(1, len(path)): eta += cost_matrix[index] path_in_co_ordinates.append({ "latLng": locations[path[index]]['latLng'], "user": locations[path[index]]['user'], "type": locations[path[index]]['custom_type'], "eta": eta, "ride_id": locations[path[index]]['ride_id'], "request_time": locations[path[index]]['request_time'], "pickup_at": locations[path[index]]['pickup_at'] }) if client_id: eta_for_client = 0 for i in range(1, len(path_in_co_ordinates)): if path_in_co_ordinates[i]["user"]["id"] == client_id: eta_for_client = path_in_co_ordinates[i]["eta"] if current_driver_location.driver.push_notification_token: result = PUSH_SERVICE.notify_single_device( registration_id=current_driver_location.driver. push_notification_token, data_message={"path": path_in_co_ordinates}, message_body={"path": path_in_co_ordinates}) return path_in_co_ordinates, eta_for_client return path_in_co_ordinates else: print "error" else: return None
def list_all(self, request): if request.method == 'GET': clients = Client.objects.all() serializer = ClientSerializer(clients, many=True) return JsonResponse(serializer.data, safe=False)