Ejemplo n.º 1
0
 def status(self, request):
     """ Get the login state (logged in user)
     ---
     response_serializer: UserSerializer
     """
     generate_csrf_token_for_frontend(request)
     if request.user.is_anonymous():
         serializer = UserSerializer()
     else:
         serializer = UserSerializer(request.user)
     return Response(serializer.data)
Ejemplo n.º 2
0
 def create(self, request, **kwargs):
     """ Log in
     ---
     request_serializer: AuthLoginSerializer
     response_serializer: UserSerializer
     """
     serializer = AuthLoginSerializer(data=request.data, context={'request': request})
     if serializer.is_valid():
         return Response(data=UserSerializer(request.user).data, status=status.HTTP_201_CREATED)
     else:
         return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 3
0
 def status(self, request):
     """ Get the login state (logged in user)
     ---
     response_serializer: UserSerializer
     """
     generate_csrf_token_for_frontend(request)
     if request.user.is_anonymous():
         return Response(data={"error": "not_authed"},
                         status=status.HTTP_401_UNAUTHORIZED)
     else:
         serializer = UserSerializer(request.user)
         return Response(serializer.data)
Ejemplo n.º 4
0
class ConversationSerializer(serializers.Serializer):
    topic = CharField(max_length=MAX_TOPIC_LENGTH, required=False)

    # Writing
    with_participants = PrimaryKeyRelatedField(
        many=True, write_only=True, queryset=get_user_model().objects.all())
    message = CharField(max_length=MAX_MESSAGE_LENGTH, write_only=True)

    # Reading
    id = IntegerField(read_only=True)
    type = SerializerMethodField(read_only=True)
    participants = UserSerializer(many=True, read_only=True)
    messages = MessageSerializer(many=True, read_only=True)

    def get_type(self, obj):
        return ConversationType.name(obj.type)

    def create(self, validated_data):
        """
        Create new conversation with other user(s) and a message
        """
        message_data = {
            'content': validated_data.pop('message'),
            'author_id': self.context['request'].user.id,
        }

        participant_ids = [_.id for _ in validated_data.pop('with_participants')] + \
                          [self.context['request'].user.id, ]

        # Set default type
        if len(participant_ids) == 2:
            chat_type = ConversationType.ONE_ON_ONE
        else:
            chat_type = ConversationType.MULTICHAT

        chat = ConversationModel.objects.create(type=chat_type,
                                                **validated_data)
        chat.participants = participant_ids
        chat.save()

        # Todo: refactor to message serializer
        MessageModel.objects.create(in_conversation_id=chat.id, **message_data)

        return chat

    def validate_with_participants(self, value):
        if len(value) < 1:
            raise serializers.ValidationError("No chat participants given")
        if len(value) == 1 and self.context['request'].user.id in value:
            raise serializers.ValidationError(
                "Requesting user is only participant")
        return value
 def test_instantiation(self):
     serializer = UserSerializer(self.user)
     for k in ['id', 'display_name', 'email']:
         self.assertEqual(serializer.data[k], getattr(self.user, k))