Exemplo n.º 1
0
 def post(self, request):
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         user = serializer.save()
         return Response(serializer.data, status=status.HTTP_201_CREATED)
     else:
         return Response(serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 2
0
def my_jwt_response_handler(token, user=None, request=None):
    return {
        'token': token,
        'user': UserSerializer(user, context={
            'request': request
        }).data
    }
Exemplo n.º 3
0
    def connect(self):
        self.accept()
        channel_url = self.scope['url_route']['kwargs']['channel_url']
        self.room_name = Channel.objects.get(
            channel_url=channel_url).channel_name
        print('chat_%s' % self.room_name)
        self.room_group_name = 'chat_%s' % self.room_name.replace(" ", "_")

        channels = Channel.objects.all()
        self.groups.extend([
            'chat_%s' % channel.channel_name.replace(" ", "_")
            for channel in channels
        ])

        if not self.room_group_name in self.groups:
            self.groups.append(self.room_group_name)

        # Join room group
        async_to_sync(self.channel_layer.group_add)(self.room_group_name,
                                                    self.channel_name)
        if channel_url == 'generic':
            user = UserSerializer(self.scope['user'])
            for group in self.groups:
                if not 'generic' in group:
                    async_to_sync(self.channel_layer.group_send)(
                        group, {
                            'type': 'broadcast',
                            'event': 'join_channel',
                            'channel_name': 'generic',
                            'user': user.data
                        })
Exemplo n.º 4
0
 def send_update_participant_list_by_group_id(cls, group_id):
     from chat.serializers import UserSerializer
     qs = User.objects.filter(groupparticipant__group_id=group_id)
     json_data = UserSerializer(qs, many=True).data
     group_name = ChatGroup.get_group_name(group_id)
     cls._send_channel_layers_event(group_name, json_data, 'layers.method',
                                    'participant.list')
Exemplo n.º 5
0
def user_list(request, pk=None):
    """
    List all required messages, or create a new message.
    """
    if request.method == 'GET':
        if pk:
            users = User.objects.filter(id=pk)
        else:
            users = User.objects.all().exclude(id=request.user.id)
        serializer = UserSerializer(users,
                                    many=True,
                                    context={'request': request})
        return Response(serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        try:
            user = User.objects.create_user(username=data['username'],
                                            password=data['password'])
            UserProfile.objects.create(user=user)
            Token.objects.get_or_create(user=user)
            return Response(data, status=status.HTTP_200_OK)
        except Exception:
            return Response({'error': "Something went wrong"},
                            status=status.HTTP_200_OK)
Exemplo n.º 6
0
    def post(self, request, format=None):
        email = request.data.get("email")
        password = request.data.get("password")
        if email is None or password is None:
            return Response(
                {'message': 'Please provide both email and password'},
                status=HTTP_400_BAD_REQUEST)
        try:
            user = User.objects.get(email=email)
        except Exception as e:
            user = None
        if user:
            if user.check_password(password):
                try:
                    token, _ = Token.objects.get_or_create(user=user)
                except Exception as e:
                    pass

                user_data = UserSerializer(user).data
                return Response({
                    'token': token.key,
                    'user': user_data
                },
                                status=HTTP_200_OK)
            else:
                return Response({'message': 'Invalid Username or Password'},
                                status=HTTP_400_BAD_REQUEST)
        else:
            return Response(
                {
                    'message':
                    'no user associated with this email please enter check email.'
                },
                status=HTTP_400_BAD_REQUEST)
Exemplo n.º 7
0
def user_list(request, pk=None):
    """
    List all required messages, or create a new message.
    """
    if request.method == 'GET':
        if pk:
            users = User.objects.filter(id=pk)
        else:
            users = User.objects.all()
        serializer = UserSerializer(users,
                                    many=True,
                                    context={'request': request})
        return JsonResponse(serializer.data, safe=False)

    if request.method == 'POST':
        data = JSONParser().parse(request)
        try:
            user = User.objects.create_user(username=data['username'],
                                            email=data['email'],
                                            password=data['password'])
            UserProfile.objects.create(user=user)
            Userdetails.objects.create(user=user)
            return JsonResponse(data, status=201)
        except Exception:
            return JsonResponse({'error': "Something went wrong"}, status=400)
Exemplo n.º 8
0
def user_list(request, pk=None):
    """
    List all users, an unique user or create a new one.
    """
    if request.method == 'GET':
        if pk:  # If PrimaryKey (id) of the user is specified in the url
            users = User.objects.filter(
                id=pk)  # Select only that particular user
        else:
            users = User.objects.all()  # Else get all user list
        serializer = UserSerializer(users,
                                    many=True,
                                    context={'request': request})
        return JsonResponse(serializer.data,
                            safe=False)  # Return serialized data
    elif request.method == 'POST':
        data = JSONParser().parse(
            request
        )  # On POST, parse the request object to obtain the data in json
        try:
            # serializer = UserSerializer(data=data)  # Serialize the data
            # if serializer.is_valid():
            #     serializer.save()  # Save it if valid
            # return JsonResponse(serializer.data, status=201)  # Return back the data on success
            # return JsonResponse(serializer.errors, status=400)  # Return back the errors  if not valid
            user = User.objects.create_user(username=data['username'],
                                            password=data['password'])
            UserProfile.objects.create(user=user)
            return JsonResponse(data, status=201)
        except Exception:
            return JsonResponse({'error': "Something went wrong"}, status=400)
def user_list(request, pk=None):
    """
    List all required messages, or create a new message.
    """
    if request.method == 'GET':
        if pk:  # If PrimaryKey (id) of the user is specified in the url
            users = User.objects.filter(
                id=pk)  # Select only that particular user
        else:
            users = User.objects.all()  # Else get all user list
        serializer = UserSerializer(users,
                                    many=True,
                                    context={'request': request})
        return JsonResponse(serializer.data,
                            safe=False)  # Return serialized data
    elif request.method == 'POST':
        data = JSONParser().parse(
            request
        )  # On POST, parse the request object to obtain the data in json
        serializer = UserSerializer(data=data)  # Seraialize the data
        if serializer.is_valid():
            serializer.save()  # Save it if valid
            return JsonResponse(serializer.data,
                                status=201)  # Return back the data on success
        return JsonResponse(serializer.errors,
                            status=400)  # Return back the errors  if not valid
Exemplo n.º 10
0
def user_details(request):
    """
    Get Username details
    """
    if request.method == 'GET':
        users = User.objects.filter(id=request.user.id)
        serializer = UserSerializer(users,
                                    many=True,
                                    context={'request': request})
        return Response(serializer.data)
Exemplo n.º 11
0
def user_list(request, pk=None):
    """
    List all users, an unique user or create a new one.
    """
    if request.method == 'GET':
        if pk:  # If PrimaryKey (id) of the user is specified in the url
            users = User.objects.filter(
                id=pk)  # Select only that particular user
        else:
            users = User.objects.all()  # Else get all user list
            serializer = UserSerializer(users,
                                        many=True,
                                        context={'request': request})
        return JsonResponse(serializer.data,
                            safe=False)  # Return serialized data
Exemplo n.º 12
0
    def post(self, request):
        data = request.data
        form = UserRegisterForm(data)

        if form.is_valid():
            username = form.cleaned_data['username']
            password = make_password(form.cleaned_data['password2'])
            data_user = {
                'username': username,
                'password': password,
                'email': form.cleaned_data['email']
            }
            user = UserSerializer(data=data_user)
            if user.is_valid():
                messages.success(
                    request, f'el usuario {username} ha sido creado con exito')
                user.save()
            else:
                messages.error(request,
                               f'el usuario {username} no se pudo crear')

        context = {'form': form}

        return render(request, self.__template, context)
Exemplo n.º 13
0
def user_list(request, pk=None):
    if request.method == 'GET':
        if pk:
            users = User.objects.filter(id=pk)
        else:
            users = User.objects.all()
        serializer = UserSerializer(users,
                                    many=True,
                                    context={'request': request})
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = UserSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
Exemplo n.º 14
0
def user_list(request, pk=None):
    """
    List all required message, or create a new message.
    """
    if request.method == 'GET':
        if pk:
            users = User.objects.filter(
                id=pk)  #Select only that particular user
        else:
            users = Users.objects.all()  #Else get all user list
        serializer = UserSerializer(users,
                                    many=True,
                                    context={'request': request})
        return JsonResponse(serializer.data, safe=False)
    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = UserSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(Serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
    def get(self, request):
        sel_id = request.GET.get("sel")
        try:
            if sel_id[0] == "g":
                resiver = Room.objects.get(id=int(sel_id[1::]))
                message = resiver.message.all()
            else:
                resiver = User.objects.get(id=int(sel_id))
                message = Message.objects.filter(
                    content_type__model="user",
                    object_id=int(sel_id),
                    sender=request.user) | Message.objects.filter(
                        content_type__model="user",
                        sender=resiver,
                        object_id=request.user.id)

            serializer = MessageSerializer(message, many=True)
            return Response({"data": serializer.data})

        except TypeError:
            chats = Room.objects.filter(invited=request.user)

            privateChat2 = Message.objects.filter(
                content_type__model="user", object_id=request.user.id) | \
                           Message.objects.filter(sender=request.user,
                                                  content_type__model="user")

            UserSet = set(chat_item.resiver for chat_item in privateChat2)

            userSerializer = UserSerializer(UserSet, many=True)
            roomSerializer = RoomSerializers(chats, many=True)

            return Response({
                "GroupChat": roomSerializer.data,
                "PrivateChat": userSerializer.data,
            })
Exemplo n.º 16
0
 def get_participants(self):
     qs = User.objects.filter(groupparticipant__group=self.group)
     return UserSerializer(qs, many=True).data
Exemplo n.º 17
0
 def get_users_list(self):
     user_ids = list(
         GroupParticipant.objects.filter(group=self.group).values_list(
             'user_id', flat=True))
     qs = User.objects.all().exclude(id__in=user_ids)
     return UserSerializer(qs, many=True).data
Exemplo n.º 18
0
 def get(self, request, format=None):
     data = UserSerializer(request.user).data
     return Response(data)
Exemplo n.º 19
0
 def get(self, request):
     users = User.objects.all()
     serializers = UserSerializer(users, many=True)
     return Response(serializers.data)
Exemplo n.º 20
0
    def get(self, request, format=None):
        ''' Returns active signin user '''
        user = request.user

        user_serializer = UserSerializer(user)
        return Response(user_serializer.data)