class TripSerializer(serializers.ModelSerializer): city = CitySerializer(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')
class NotificationSerializer(serializers.ModelSerializer): initiator_user = UserSerializer(many=False, read_only=True) trip = TripSerializer(many=False, read_only=True) class Meta(object): model = Notification fields = ('id', 'initiator_user', 'notification_type', 'text', 'created_at', 'is_read', 'trip')
def get_user_profile(request): """ Returns user object using user email address :param request: :param email: :return: 200 successful """ serializer = UserSerializer(request.user) return Response(serializer.data)
def get_users_by_email(request, email): """ Returns user object using user email address :param request: :param email: :return: 200 successful """ users = User.objects.filter(is_staff=False, is_superuser=False, username__startswith=email)[:5] serializer = UserSerializer(users, many=True) return Response(serializer.data)
def get_user_by_id(request, user_id): """ Returns user object using user id :param request: :param user_id: :return: 400 if incorrect user ID is sent :return: 200 successful """ try: user = User.objects.get(pk=user_id) except User.DoesNotExist: error_message = "Invalid user ID" return Response(error_message, status=status.HTTP_400_BAD_REQUEST) serializer = UserSerializer(user) return Response(serializer.data)
def trip_friends_all(request): """ Returns a list of friends user had been to trip :param request: :return: 200 successful """ try: all_trips = User.objects.filter(trip__users=request.user).distinct() except User.DoesNotExist: error_message = "Trip does not exist" return Response(error_message, status=status.HTTP_404_NOT_FOUND) except Exception as e: return Response(str(e), status=status.HTTP_400_BAD_REQUEST) serializer = UserSerializer(all_trips, many=True) return Response(serializer.data)
class FeedbackCondensedSerializer(serializers.ModelSerializer): user = UserSerializer(read_only=True) class Meta: model = Feedback fields = ('id', 'type', 'created', 'user')