Example #1
0
    async def create_trip(self, content):
        """
        Create trip instance
        Send newly created trip data to `driver` group
        Add rider to unique trip group with trip_id as group name
        """
        trip = await self._create_trip(content)
        trip_data = trips_serializers.ReadOnlyTripSerializer(trip).data

        # Send rider requests to all drivers.
        await self.channel_layer.group_send(
            'driver',
            {
                'type': 'echo.message',
                'action': 'UPDATE_AVAILABLE_TRIPS',
                'payload': trip_data
            },
        )
        logger.info('New rider trip request sent to all drivers.')

        # Add rider to unique trip group
        user = self.scope['user']
        if await self._user_group(user) == 'RIDER':
            await self.channel_layer.group_add(trip_data.get('id'),
                                               self.channel_name)

        # Send trip detail to rider
        await self.echo_message({
            'action': 'SET_CURRENT_TRIP',
            'payload': trip_data
        })
Example #2
0
 def _get_available_trips(self):
     """
     Get all newly requested trip which is not taken by any other driver.
     """
     trips = trips_models.Trip.objects.filter(
         status=trips_models.Trip.REQUESTED, driver=None)
     return trips_serializers.ReadOnlyTripSerializer(trips, many=True).data
Example #3
0
 def _get_recent_trips(self, user):
     """
     Get all previously completed trip detail of current user.
     """
     trips = trips_models.Trip.objects.filter(
         Q(status=trips_models.Trip.COMPLETED)
         & (Q(driver=user) | Q(rider=user))).distinct()
     return trips_serializers.ReadOnlyTripSerializer(trips, many=True).data
Example #4
0
    def _get_trip(self, payload, user):
        """
        Get trip instance by id & user
        """
        trips = trips_models.Trip.objects.filter(id=payload.get('id'), driver=user)

        if trips:
            trip_data = trips_serializers.ReadOnlyTripSerializer(trips.get()).data
            return trip_data
Example #5
0
    def _get_current_trip(self, user):
        """
        Get trip instance if user has any ongoing trip
        """
        trips = (trips_models.Trip.objects.exclude(
            status=trips_models.Trip.COMPLETED).filter(
                Q(rider=user) | Q(driver=user)).distinct())

        if trips:
            trip_data = trips_serializers.ReadOnlyTripSerializer(
                trips.get()).data
            return trip_data