Ejemplo n.º 1
0
    def post(self, request):

        # check if the user has token ...
        if request.user and request.auth:

            # delete the token
            try:
                request.user.auth_token.delete()
            except:
                pass

            # get the current user
            current_user = AppUser.objects.filter(id=request.user.id).first()

            # ip login today
            current_user.ip_sign_in_today = get_ip(request)

            # last login date
            current_user.last_login_date = timezone.now()

            # save the object
            current_user.save()

            # generate a new token for the current user
            token = Token.objects.create(user=current_user)

            # check if the user is not none
            if not current_user:
                # return repsonse
                return Response({}, status=status.HTTP_401_UNAUTHORIZED)

            # serialize data
            serializer = UserShowSerializer(current_user)

            # return data and token
            return Response({'data': serializer.data, 'token': token.key})

        # else if user has not any token ...
        else:

            # get the requested data and serialize it
            serializer = UnregisteredUserSerializer(data=request.data)

            # check if the serializer is valid
            if serializer.is_valid():

                # first time to login

                # save the serializer
                new_user = serializer.save()

                # save the first ip
                new_user.first_ip = get_ip(request)

                # last login date
                new_user.last_login_date = timezone.now()

                # register date
                new_user.register_date = timezone.now()

                # save new user model after editing
                new_user.save()

                # generate a new token
                token = Token.objects.create(user=new_user)

                # show serializer
                show_serialzier = UserShowSerializer(new_user)

                # return data and token
                return Response(
                    {
                        'data': show_serialzier.data,
                        'token': token.key
                    },
                    status=status.HTTP_201_CREATED)

            # if serializer is not valid
            else:
                return Response({'error': serializer.errors},
                                status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 2
0
    def post(self, request):

        # if there is no token from google or facebook
        if not request.data.get('user_token'):
            # return bad request
            return Response({'error': 'there is no token'},
                            status=status.HTTP_400_BAD_REQUEST)

        # check if the user has token
        if request.user and request.auth:
            # here user has logged in before
            # so we will delete the token and generate a new one

            # get the current user
            current_user = AppUser.objects.filter(id=request.user.id).first()
            # check if the current user is none
            if not current_user:
                # return unauthroized user
                return Response({}, status=status.HTTP_401_UNAUTHORIZED)

            # check if the requested user_token == current.user_token
            if request.data.get('user_token') != current_user.user_token:
                # return unauthorized response
                return Response({}, status=status.HTTP_401_UNAUTHORIZED)

            # ip login today
            current_user.ip_sign_in_today = get_ip(request)

            # last date for login
            current_user.last_login_date = timezone.now()

            # save the object
            current_user.save()

            # save ip of the user in the userIp model
            new_user_ip_object, _ = UserIP.objects.get_or_create(
                user=current_user, ip=current_user.ip_sign_in_today)

            # save login date of the user in logindates model
            new_login_date_object, _ = LoginDates.objects.get_or_create(
                user=current_user, date=current_user.last_login_date)

            try:
                # delete the token of the current user
                current_user.auth_token.delete()
            except:
                pass

            # generate a new token for the current user
            token = Token.objects.create(user=current_user)

            # serialize current user data
            serializer = UserShowSerializer(current_user)

            # return response 200 OK
            return Response({
                'data': serializer.data,
                'token': token.key
            },
                            status=status.HTTP_200_OK)

        # else, user has not any token
        else:

            # serialize the requested data
            serializer = UserSerializer(data=request.data)

            # check if the serializer is valid
            if serializer.is_valid():

                # this is the first time
                # save the data into our model
                new_user = serializer.save()

                # save the first ip
                new_user.first_ip = get_ip(request)

                # edit the last login date
                new_user.last_login_date = timezone.now()

                # edit registered date
                new_user.register_date = timezone.now()

                # is_registered to True
                new_user.is_registerd = True

                # save qrcode of the user
                img = qrcode.make(new_user.name_id)
                blob = BytesIO()
                img.save(blob, 'JPEG')

                #				new_user.qr_code = img
                new_user.qr_code.save('{}_qrcode.jpg'.format(new_user.name_id),
                                      File(blob),
                                      save=False)

                # save after edit first ip
                new_user.save()

                # save ip of the user in the userIp model
                new_user_ip_object, _ = UserIP.objects.get_or_create(
                    user=new_user, ip=new_user.first_ip)

                # save login date of the user in logindates model
                new_login_date_object, _ = LoginDates.objects.get_or_create(
                    user=new_user, date=new_user.last_login_date)

                # generate a new token
                token = Token.objects.create(user=new_user)

                # show data serializer
                show_serializer = UserShowSerializer(new_user)

                # return Resonse Created
                return Response(
                    {
                        'data': show_serializer.data,
                        'token': token.key
                    },
                    status=status.HTTP_201_CREATED)