Пример #1
0
class ListMeetingModelSerializer(serializers.ModelSerializer):
    owner = UserModelSerializer()
    invited = UserModelSerializer()
    participans_invited = UserModelSerializer(many=True)
    participans_validated = UserModelSerializer(many=True)

    class Meta:
        model = Meeting
        fields = ('__all__')
Пример #2
0
 def login(self, request):
     """User sign in."""
     serializer = UserLoginSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     user, token = serializer.save()
     data = {'user': UserModelSerializer(user).data, 'access_token': token}
     return Response(data, status=status.HTTP_201_CREATED)
Пример #3
0
class InvitationModelSerializer(serializers.ModelSerializer):
    transmission = TransmissionModelSerializer()
    user = UserModelSerializer()

    class Meta:
        model = Invitation
        fields = ('__all__')
Пример #4
0
    def follow(self, request, *args, **kwargs):
        """Follow action."""
        user_from = request.user
        user_to = self.get_object()
        follows = False

        # Check if user_from already follows user_to

        if user_from.follow.filter(id=user_to.id).exists():
            user_from.follow.remove(user_to)
            follows = False
            user_to.profile.followers -= 1
            user_to.profile.save()

        else:
            user_from.follow.add(user_to)
            follows = True
            user_to.profile.followers += 1
            user_to.profile.save()

        if follows == True:
            message = 'Now you follow {}'.format(user_to.username)
        else:
            message = 'You unfollow {}'.format(user_to.username)

        data = {
            'user': UserModelSerializer(user_to).data,
            'message': message
        }

        return Response(data, status=status.HTTP_200_OK)
Пример #5
0
 def signup(self, request):
     """User sign up."""
     serializer = UserSignUpSerializer(data=request.data)
     serializer.is_valid(raise_exception=True)
     user = serializer.save()
     data = UserModelSerializer(user).data
     return Response(data, status=status.HTTP_201_CREATED)
Пример #6
0
    def post(self, request, *args, **kwargs):
        """Handle HTTP POST request."""

        serializer = UserLoginSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user, token = serializer.save()
        data = {'user': UserModelSerializer(user).data, 'access_token': token}
        return Response(data, status=status.HTTP_201_CREATED)
Пример #7
0
    def post(self, request, *args, **kwargs):
        """Handle HTTP POST request."""

        serializer = UserSignupSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = serializer.save()
        data = UserModelSerializer(user).data,

        return Response(data, status=status.HTTP_201_CREATED)
Пример #8
0
 def profile(self, request, username):
     """Update profile's information."""
     user = self.get_object()
     partial = request.method == 'PATCH'
     serializer = ProfileModelSerializer(user.profile,
                                         data=request.data,
                                         partial=partial)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     data = UserModelSerializer(user).data
     return Response(data, status=status.HTTP_200_OK)
Пример #9
0
    def get_user(self, obj):
        """Return UserModelSerializer representation."""
        from apps.users.serializers import UserModelSerializer

        return UserModelSerializer(
            obj.user,
            fields=(
                'username',
                'profile',
            ),
            # context={'action': 'like'}
        ).data
Пример #10
0
 def get_user(self, obj):
     """Show user that makes the publication."""
     # Serializer
     from apps.users.serializers import UserModelSerializer
     try:
         user = User.objects.get(username=obj.project.creator)
     except User.DoesNotExist:
         return None
     return UserModelSerializer(user, fields=(
         'username',
         'profile',
     )).data
Пример #11
0
 def profile(self, request, *args, **kwargs):
     """Update profile data."""
     user = self.get_object()
     profile = user.profile
     partial = request.method == 'PATCH'
     serializer = ProfileModelSerializer(profile,
                                         data=request.data,
                                         partial=partial)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     data = UserModelSerializer(user).data
     return Response(data)
Пример #12
0
class ViewingModelSerializer(serializers.ModelSerializer):
    user = UserModelSerializer()
    comments = serializers.SerializerMethodField('get_comments')

    def get_comments(self, viwing):
        qs = Comment.objects.filter(viwing=viwing)
        serializer = CommentModelSerializer(instance=qs, many=True)
        data = serializer.data
        return data

    class Meta:
        model = ViewingSelected
        fields = ('__all__')
Пример #13
0
class RideModelSerializer(serializers.ModelSerializer):
    """Ride model serializer."""

    offered_by = UserModelSerializer(read_only=True)
    offered_in = serializers.StringRelatedField()

    passengers = UserModelSerializer(read_only=True, many=True)

    class Meta:
        """Meta class."""

        model = Ride
        fields = '__all__'
        read_only_fields = ('offered_by', 'offered_in', 'rating')

    def update(self, instance, data):
        """Allow updates only before departure date."""
        now = timezone.now()
        if instance.departure_date <= now:
            raise serializers.ValidationError(
                'Ongoing rides cannot be modified.')
        return super(RideModelSerializer, self).update(instance, data)
Пример #14
0
class CameraReviewModelSerializer(serializers.ModelSerializer):
    """Camera review model serializer."""

    product = CameraModelSerializer(read_only=True)
    author = UserModelSerializer(read_only=True)

    class Meta:
        """Meta class."""
        model = CameraReview
        fields = (
            'title',
            'content',
            'product',
            'author',
        )
Пример #15
0
    def get_followed(self, obj):
        """Return followers information."""
        view = self.context.get('view', None)
        fields = (
            'email',
            'username',
            'phone_number',
            'first_name',
            'last_name',
            'profile',
        )

        if view is not None:
            # Users
            if view.view_name == 'users' and view.action in view.fields_to_return:
                fields = view.fields_to_return[
                    view.action]['followeds']['followed']

        return UserModelSerializer(obj.followed,
                                   fields=fields,
                                   context=self.context).data
Пример #16
0
    def get_user(self, obj):
        """Return user model representation."""
        # Serializer
        from apps.users.serializers import UserModelSerializer

        return UserModelSerializer(obj.user, fields=('username', 'profile')).data
Пример #17
0
class RetrieveModalSerializer(serializers.ModelSerializer):
    owner = UserModelSerializer()    
    invited = UserModelSerializer()    
    class Meta:
        model = Invitation
        fields = '__all__'