예제 #1
0
    async def update_trip(self, message):
        # Get the trip data from the messsage sent by the driver
        data = message.get('data')
        # Call helper function to update the Trip object in the DB
        trip = await self._update_trip(data)
        # Get the trip.id from that object
        trip_id = f'{trip.id}'
        # Serialize the updated Trip data
        trip_data = NestedTripSerializer(trip).data

        ###
        # Check if Trip status is 'Started'
        if trip.status == 'STARTED':
            # Send message to drivers group saying trip accepted
            await self.channel_layer.group_send(
                group='drivers',
                message={
                    'type': 'echo.message',
                    'data': f'Trip {trip_id} has been accepted.'
                })
        ###

        # Server/consumer sends update via the Trip group
        await self.channel_layer.group_send(group=trip_id,
                                            message={
                                                'type': 'echo.message',
                                                'data': trip_data
                                            })

        await self.channel_layer.group_add(group=trip_id,
                                           channel=self.channel_name)

        await self.send_json({'type': 'echo.message', 'data': trip_data})
예제 #2
0
    async def update_trip(self, message):
        data = message.get('data')
        trip = await self._update_trip(data)
        trip_id = f'{trip.id}'
        trip_data = NestedTripSerializer(trip).data

        # Send update to rider.
        await self.channel_layer.group_send(
            group=trip_id,
            message={
                'type': 'echo.message',
                'data': trip_data,
            }
        )

        # Add driver to the trip group.
        await self.channel_layer.group_add(
            group=trip_id,
            channel=self.channel_name
        )

        await self.send_json({
            'type': 'echo.message',
            'data': trip_data
        }) 
예제 #3
0
	async def cancel_trip(self, message):
		data = message.get('data')
		trip = await self._cancel_trip(data)
		trip_id = f'{trip.id}'
		trip_data = NestedTripSerializer(trip).data

		# Send update to driver.
		await self.channel_layer.group_send(
			group=trip_id,
			message={
				'type': 'echo.message',
				'data': trip_data,
			}
		)

		# Remove driver from the trip group.
		await self.channel_layer.group_discard(
			group='drivers',
			channel=self.channel_name
		)

		await self.send_json({
			'type': 'echo.message',
			'data': trip_data
		})
예제 #4
0
    async def update_trip(self, message):
        data = message.get("data")
        trip = await self._update_trip(data)
        trip_id = f"{trip.id}"
        trip_data = NestedTripSerializer(trip).data

        # Send update to rider.
        await self.channel_layer.group_send(
            group=trip_id, message={"type": "echo.message", "data": trip_data,}
        )

        # Add driver to the trip group.
        await self.channel_layer.group_add(group=trip_id, channel=self.channel_name)

        await self.send_json({"type": "echo.message", "data": trip_data})
예제 #5
0
	async def create_trip(self, message):
		data = message.get('data')
		trip = await self._create_trip(data)
		trip_data = NestedTripSerializer(trip).data

		# Send rider requests to all drivers.
		await self.channel_layer.group_send(group='drivers', message={
			'type': 'echo.message',
			'data': trip_data
		})

		await self.send_json({
			'type': 'echo.message',
			'data': trip_data,
		})
예제 #6
0
    async def create_trip(self, message):
        data = message.get('data')
        trip = await self._create_trip(data)
        trip_data = NestedTripSerializer(trip).data

        # send rider requests to all drivers
        await self.channel_layer.group_send(group='drivers',
                                            message={
                                                'type': 'echo.message',
                                                'data': trip_data
                                            })

        # add rider to trip group
        await self.channel_layer.group_add(group=f'{trip.id}',
                                           channel=self.channel_name)

        await self.send_json({'type': 'echo.message', 'data': trip_data})
예제 #7
0
    async def create_trip(self, message):
        data = message.get("data")
        trip = await self._create_trip(data)
        trip_data = NestedTripSerializer(trip).data

        # Send rider requests to all drivers.
        await self.channel_layer.group_send(
            group="drivers", message={"type": "echo.message", "data": trip_data}
        )

        # Add rider to trip group.
        await self.channel_layer.group_add(  # new
            group=f"{trip.id}", channel=self.channel_name
        )

        await self.send_json(
            {"type": "echo.message", "data": trip_data,}
        )
예제 #8
0
    async def create_trip(self, message):
        '''creates a new trip and passes the details back to the client'''
        data = message.get('data')
        trip = await self._create_trip(data)
        trip_data = NestedTripSerializer(trip).data

        # Send rider requests to all drivers.
        await self.channel_layer.group_send(group='drivers',
                                            message={
                                                'type': 'echo.message',
                                                'data': trip_data
                                            })

        # Add rider to trip group.
        await self.channel_layer.group_add(  # new
            group=f'{trip.id}', channel=self.channel_name)

        await self.send_json({
            'type': 'echo.message',
            'data': trip_data,
        })
예제 #9
0
    async def create_trip(self, message):
        data = message.get('data')
        trip = await self._create_trip(data)
        trip_data = NestedTripSerializer(trip).data

        # Send rider requests to all drivers.
        await self.channel_layer.group_send(group='drivers', message={
            'type': 'echo.message',
            'data': trip_data
        })

        # Add rider to trip group.
        await self.channel_layer.group_add(
            group=f'{trip.id}',
            channel=self.channel_name
        )

        await self.send_json({
            'type': 'echo.message',
            'data': trip_data,
        })

        async def disconnect(self, code):
            user = self.scope['user']
            user_group = await self._get_user_group(user)
            if user_group == 'driver':
                await self.channel_layer.group_discard(
                    group='drivers',
                    channel=self.channel_name
                )

            for trip_id in await self._get_trip_ids(user):
                await self.channel_layer.group_discard(
                    group=trip_id,
                    channel=self.channel_name
                )

            await super().disconnect(code)
예제 #10
0
    async def update_trip(self, message):
        '''
        On accepting rider's request update the trip:
           - let the rider know request has been accepted
           - Add the driver to the trip group
        '''
        data = message.get('data')
        trip = await self._update_trip(data)
        trip_id = f'{trip.id}'
        trip_data = NestedTripSerializer(trip).data

        # Send update to rider.
        await self.channel_layer.group_send(group=trip_id,
                                            message={
                                                'type': 'echo.message',
                                                'data': trip_data,
                                            })

        # Add driver to the trip group.
        await self.channel_layer.group_add(group=trip_id,
                                           channel=self.channel_name)

        await self.send_json({'type': 'echo.message', 'data': trip_data})
예제 #11
0
 def _get_trip_data(self, trip):
     return NestedTripSerializer(trip).data