Exemple #1
0
class TripSerializer(serializers.ModelSerializer):
    city = CityCondensedSerializer(many=False, read_only=True)
    users = UserSerializer(many=True, read_only=True)

    class Meta:
        model = Trip
        fields = ('id', 'trip_name', 'city', 'users', 'start_date_tx')
Exemple #2
0
def get_city_by_name(request, city_prefix):
    """
    Returns a list of cities that starts with the given city prefix
    :param request:
    :param city_prefix:
    :return: 200 successful
    """
    cities = City.objects.filter(city_name__istartswith=city_prefix)[:5]
    serializer = CityCondensedSerializer(cities, many=True)
    return Response(serializer.data)
Exemple #3
0
def get_all_cities(request, no_of_cities=8):
    """
    Returns a list of cities with maximum number of logs (visits)
    :param request:
    :param no_of_cities: (default count: 8)
    :return: 200 successful
    """
    cities = City.objects.annotate(visit_count=Count('logs')).order_by('-visit_count')[:no_of_cities]
    serializer = CityCondensedSerializer(cities, many=True)
    return Response(serializer.data)
Exemple #4
0
class TripCondensedSerializer(serializers.ModelSerializer):
    city = CityCondensedSerializer(many=False, read_only=True)
    users_count = serializers.SerializerMethodField()

    class Meta:
        model = Trip
        fields = ('id', 'trip_name', 'city', 'users_count', 'start_date_tx')

    def get_users_count(self, obj):
        return obj.users.count()
Exemple #5
0
def get_visited_city(request, user_id=None):
    """
    all the unique cities user has in his list of trips.
    :param request:
    :param user_id:
    :return: 404 if user does not exist
    :return: 200 successful
    """
    # if user with user_id does not exist
    if not user_id:
        user_id = request.user.id

    if not User.objects.filter(id=user_id).exists():
        error_message = "User does not exists."
        return Response(error_message, status=status.HTTP_404_NOT_FOUND)

    cities = set()
    trips = Trip.objects.filter(users=user_id)
    for trip in trips:
        cities.add(trip.city)

    serializer = CityCondensedSerializer(cities, many=True)
    return Response(serializer.data, status=status.HTTP_200_OK)