Пример #1
0
    def post(self, request, format=None):
        if not request.user.is_authenticated:
            return Response(
                data={'data': 'Authentication Failed, Please login'},
                status=status.HTTP_401_UNAUTHORIZED)

        if request_contain_keys(
                request.POST,
            ['current_password', 'new_password', 'confirm_password']):
            if request.POST['new_password'] != request.POST['confirm_password']:
                return Response(
                    status=status.HTTP_400_BAD_REQUEST,
                    data={'data': 'Password authentication failed'})

            if update_user_password(request.user.id,
                                    request.POST['new_password']) == 1:
                return Response(
                    status=status.HTTP_200_OK,
                    data={'data': 'Password changed successfully!!'})
            else:
                return Response(
                    status=status.HTTP_404_NOT_FOUND,
                    data={'data': 'Something went wrong, code CNSU4'})

        return Response(status=status.HTTP_400_BAD_REQUEST,
                        data={'data': 'Invalid Parameters!!'})
Пример #2
0
    def get(self, request, format='json'):
        if request_contain_keys(request.GET, ['verification_code']):
            token = request.GET['verification_code']

            success, payload = decode_token(token)

            if success:
                subscription_id = payload['subscription_id']
                email = payload['email']

                user_set = get_user_by_email(email)

                if len(user_set) > 0:
                    if is_topic_quota_exhausted(user_set[0]):
                        return Response(
                            status=status.HTTP_403_FORBIDDEN,
                            data=
                            'You have reached your ACTIVE subscriptions quota!!'
                        )

                    if confirm_subscription(email, subscription_id):
                        return Response(
                            status=status.HTTP_200_OK,
                            data='Subscription verified successfully')

        return Response(status=status.HTTP_400_BAD_REQUEST,
                        data='Link expired!!')
Пример #3
0
    def post(self, request, format=None):
        if request_contain_keys(
                request.POST,
            ['verification_code', 'new_password', 'confirm_password']):
            if request.POST['new_password'] != request.POST['confirm_password']:
                return Response(
                    status=status.HTTP_400_BAD_REQUEST,
                    data={'data': 'Password authentication failed'})

            success, payload = decode_token(request.POST['verification_code'])

            if success:
                email = payload['email']
                user_id = payload['user_id']

                if update_user_password(user_id,
                                        request.POST['new_password']) == 1:
                    return Response(
                        status=status.HTTP_200_OK,
                        data={'data': 'Password reset successful!!'})
                else:
                    return Response(
                        status=status.HTTP_404_NOT_FOUND,
                        data={'data': 'Something went wrong, code CNSU3'})
            else:
                return Response(status=status.HTTP_403_FORBIDDEN,
                                data={'data': 'Token authentication failed'})

        return Response(status=status.HTTP_400_BAD_REQUEST,
                        data={'data': 'Invalid Parameters!!'})
Пример #4
0
    def post(self, request, format=None):
        if not request.user.is_authenticated:
            return Response(data={'data': 'Authentication Failed'},
                            status=status.HTTP_401_UNAUTHORIZED)

        if request_contain_keys(request.POST, ['plan_request_id']):
            payment_id = request.POST.get('payment_id', '')
            payment_mode = request.POST.get('payment_mode', 'OFFLINE')

            plan_request = get_plan_request_by_id(
                request.POST['plan_request_id'])
            update_plan = False
            if len(plan_request) > 0:
                if update_plan_request_status(plan_request[0].id,
                                              'ACCEPTED') == 1:
                    if plan_request[0].user.plan_subscribed is not None:
                        if is_user_plan_expired(plan_request[0].user):
                            update_plan = True
                        else:
                            record_plan_history(
                                plan_request[0].user, plan_request[0].new_plan,
                                get_plan_starts_from(plan_request[0].user),
                                payment_id, payment_mode)
                            add_upcoming_plan(plan_request[0].user,
                                              plan_request[0].new_plan)
                            return Response(
                                data={'data': 'Plan added to upcoming list'},
                                status=status.HTTP_200_OK)
                    else:
                        update_plan = True
                else:
                    return Response(
                        data={'data': 'Something went wrong, code: CNSU3'},
                        status=status.HTTP_500_INTERNAL_SERVER_ERROR)
                if update_plan:
                    if update_user_plan(plan_request[0].user,
                                        plan_request[0].new_plan) == 1:
                        record_plan_history(plan_request[0].user,
                                            plan_request[0].new_plan,
                                            get_utc_now(), payment_id,
                                            payment_mode)
                        send_plan_change_confirmation(plan_request[0])
                        return Response(
                            data={'data': 'Plan changed successfully'},
                            status=status.HTTP_200_OK)
                    else:
                        return Response(
                            data={'data': 'Something went wrong, code: CNSU2'},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)

                return Response(data={'data': 'Unable to change plan'},
                                status=status.HTTP_400_BAD_REQUEST)
            else:
                return Response(data={'data': 'Plan is invalid'},
                                status=status.HTTP_400_BAD_REQUEST)

        return Response(data={'data': 'Invalid parameters'},
                        status=status.HTTP_400_BAD_REQUEST)
Пример #5
0
    def get(self, request, format=None):
        if request_contain_keys(request.GET, ['verification_code']):
            token = request.GET['verification_code']

            success, payload = decode_token(token)

            if success:
                subscription_id = payload['subscription_id']
                email = payload['email']

                if unsubscribe(email, subscription_id):
                    return Response(status=status.HTTP_200_OK,
                                    data='Unsubscribed successfully')

        return Response(status=status.HTTP_400_BAD_REQUEST,
                        data='Something went wrong!!')
Пример #6
0
    def post(self, request, format=None):
        if not request.user.is_authenticated:
            return Response(
                data={'data': 'Authentication Failed, Please login'},
                status=status.HTTP_401_UNAUTHORIZED)

        if request_contain_keys(request.POST, ['subscription_ids[]']):
            for id in request.POST.getlist('subscription_ids[]'):
                remove_subscription(request.user, id)

            return Response(
                data={'data': 'Subscriptions removed successfully'},
                status=status.HTTP_200_OK)

        return Response(data={'data': 'Invalid Parameters'},
                        status=status.HTTP_400_BAD_REQUEST)
Пример #7
0
    def get(self, request, format='json'):
        if request_contain_keys(request.GET, ['verification_code']):
            token = request.GET['verification_code']

            success, payload = decode_token(token)

            print(payload)

            if success:
                user_id = payload['user_id']
                email = payload['email']

                if confirm_email_verification(email, user_id):
                    return Response(status=status.HTTP_200_OK,
                                    data='Email verified successfully')

        return Response(status=status.HTTP_400_BAD_REQUEST,
                        data='Link expired!!')
Пример #8
0
    def post(self, request, format=None):
        if not request.user.is_authenticated:
            return Response(
                data={'data': 'Authentication Failed, Please login'},
                status=status.HTTP_401_UNAUTHORIZED)

        if request_contain_keys(request.POST, ['plan_id']):
            print(request.POST)
            plan_set = get_plan_by_id(request.POST['plan_id'][0])
            if len(plan_set) > 0:
                add_plan_request(request.user, plan_set[0])
                return Response(
                    data={'data': 'Plan request added successfully'},
                    status=status.HTTP_200_OK)
            else:
                return Response(data={'data': 'Invalid Plan'},
                                status=status.HTTP_400_BAD_REQUEST)

        return Response(data={'data': 'Invalid Parameters'},
                        status=status.HTTP_400_BAD_REQUEST)
Пример #9
0
    def get(self, request, format=None):
        if not request.user.is_authenticated:
            return Response(
                data={'data': 'Authentication Failed, Please login'},
                status=status.HTTP_401_UNAUTHORIZED)

        if request_contain_keys(request.GET, ['query']):

            response = is_action_allowed(request.user)

            if response is True:
                if request.user.quick_analysis_counter < request.user.plan_subscribed.quick_analysis_quota:
                    tweet = TwitterHelper(request.GET['query'])
                    data = tweet.fetch_analysis()

                    if data['success']:
                        if update_quick_analysis_counter(request.user) == 1:
                            return Response(data={'data': data},
                                            status=status.HTTP_200_OK)
                        else:
                            return Response(
                                data={
                                    'data': 'Something went wrong, code:CNSU1'
                                },
                                status=status.HTTP_500_INTERNAL_SERVER_ERROR)
                    else:
                        return Response(data={'data': data['message']},
                                        status=data['status'])
                else:
                    return Response(data={
                        'data':
                        'You have exhausted your quick analysis quota'
                    },
                                    status=status.HTTP_400_BAD_REQUEST)
            else:
                return Response(data={'data': response},
                                status=status.HTTP_400_BAD_REQUEST)

        return Response(data={'data': 'Invalid or missing Parameters'},
                        status=status.HTTP_400_BAD_REQUEST)
Пример #10
0
    def post(self, request, format=None):
        if request_contain_keys(request.POST, ['user_email']):
            user_set = get_user_by_email(request.POST['user_email'])

            if len(user_set) > 0:
                token = generate_token(email=user_set[0].email,
                                       expire=1,
                                       user_id=user_set[0].id)
                password_reset_url = '{}/app/reset_password?verification_code={}'.format(
                    get_host_origin(request), token)
                send_password_reset_link(user_set[0], password_reset_url)
                return Response(
                    status=status.HTTP_200_OK,
                    data={
                        'data':
                        'Please check your mail for password reset link'
                    })
            else:
                return Response(status=status.HTTP_400_BAD_REQUEST,
                                data={'data': 'Email is not registered!!'})
        return Response(status=status.HTTP_400_BAD_REQUEST,
                        data={'data': 'Invalid Parameters!!'})
Пример #11
0
    def post(self, request, format=None):
        if request_contain_keys(
                request.POST,
            ['user_email', 'user_password', 'user_cnf_password']):
            if request.POST['user_password'] != request.POST[
                    'user_cnf_password']:
                return Response(
                    data={'data': 'Password authentication failed'},
                    status=status.HTTP_400_BAD_REQUEST)

            form_data = {
                'email': request.POST['user_email'],
                'password': request.POST['user_password'],
                'timezone_offset': request.POST.get('timezone_offset')
            }

            form = CustomUserRegisterForm(form_data)

            if form.is_valid():
                user = form.save()
                if user is not None:
                    token = generate_token(email=user.email, user_id=user.id)
                    email_verification_url = '{}/api/account/confirm_email?verification_code={}&format=json'.format(
                        get_host_origin(request), token)
                    send_email_verification_link(user, email_verification_url)
                    return Response(data={'data': user.toJSON()},
                                    status=status.HTTP_200_OK)
                else:
                    return Response(data={'data': 'User not found'},
                                    status=status.HTTP_401_UNAUTHORIZED)
            else:
                return Response(data={'data': 'User already exist'},
                                status=status.HTTP_400_BAD_REQUEST)

        return Response(data={'data': 'Invalid User credentials'},
                        status=status.HTTP_400_BAD_REQUEST)
Пример #12
0
    def post(self, request, format=None):
        if request_contain_keys(request.POST, ['user_email', 'user_password']):
            form_data = {
                'email': request.POST['user_email'],
                'password': request.POST['user_password']
            }

            form = CustomUserLoginForm(form_data)

            # is_valid return true if user not in db
            if not form.is_valid():
                user = CustomUserAuthentication().authenticate(
                    request,
                    email=form_data['email'],
                    password=form_data['password'])
                if user is not None:
                    if not user.email_verified:
                        return Response(
                            data={'data': 'Email is not verified yet'},
                            status=status.HTTP_400_BAD_REQUEST)
                    if user.status == 'SUSPENDED':
                        return Response(
                            data={'data': 'Your account is suspended'},
                            status=status.HTTP_400_BAD_REQUEST)

                    login(request,
                          user,
                          backend='app_perf.auths.CustomUserAuthentication')
                    return Response(data={'data': user.toJSON()},
                                    status=status.HTTP_200_OK)
            else:
                return Response(data={'data': 'User not found'},
                                status=status.HTTP_401_UNAUTHORIZED)

        return Response(data={'data': 'Invalid User credentials'},
                        status=status.HTTP_400_BAD_REQUEST)
Пример #13
0
    def post(self, request, format=None):
        if not request.user.is_authenticated:
            return Response(data={'data': 'Authentication Failed'},
                            status=status.HTTP_401_UNAUTHORIZED)

        if request_contain_keys(request.POST, ['user_email']):
            assign_plan = False
            user_set = get_user_by_email(request.POST['user_email'])

            if len(user_set) > 0:
                if user_set[0].plan_subscribed is not None:
                    if is_user_plan_expired(user_set[0]):
                        assign_plan = True
                    else:
                        return Response(data={
                            'data':
                            'User already have plan assigned and activated'
                        },
                                        status=status.HTTP_400_BAD_REQUEST)
                else:
                    assign_plan = True

                if assign_plan:
                    if update_user_plan(user_set[0],
                                        get_plan_by_id(5)[0]) == 1:
                        record_plan_history(user_set[0],
                                            get_plan_by_id(5)[0],
                                            get_utc_now())
                        send_plan_assigned_confirmation(
                            get_user_by_id(user_set[0].id)[0])
                        return Response(data={
                            'data':
                            'Plan assigned and confirmation sent successfully'
                        },
                                        status=status.HTTP_200_OK)
                    else:
                        return Response(
                            data={'data': 'Something went wrong, code: CNSU5'},
                            status=status.HTTP_500_INTERNAL_SERVER_ERROR)
            else:
                return Response(data={'data': 'User does not exist'},
                                status=status.HTTP_400_BAD_REQUEST)

        # if request_contain_keys(request.POST, ['test_user_password', 'test_user_cnf_password', 'test_user_email']):
        #     if request.POST['test_user_password'].strip() != request.POST['test_user_cnf_password'].strip():
        #         return Response(status=status.HTTP_400_BAD_REQUEST, data={'data': 'Password Authentication failed!!'})
        #     result = get_user_details(request.POST['test_user_email'], request.POST['test_user_password'])

        #     if len(result) == 0:
        #         user = register_or_verify_subscriber(request.POST['test_user_email'],
        #                                              request.POST['test_user_password'],
        #                                              '4')
        #         if not user.email_verified:
        #             token = generate_token(email=user.email, user_id=user.id)
        #             email_verification_url = '{}/api/confirm_email?verification_code={}'.format(
        #                 get_host_origin(request),
        #                 token)
        #             send_email_verification_link(user, email_verification_url)

        #         return Response(
        #             status=status.HTTP_200_OK,
        #             data={'data': 'User registered, please check email to verify'}
        #         )
        #     else:
        #         return Response(status=status.HTTP_400_BAD_REQUEST, data={'data': 'User already exist!!'})
        return Response(status=status.HTTP_400_BAD_REQUEST,
                        data={'data': 'Invalid parameters!!'})
Пример #14
0
    def post(self, request, format=None):
        if not request.user.is_authenticated:
            return Response(
                data={'data': 'Authentication Failed, Please login'},
                status=status.HTTP_401_UNAUTHORIZED)

        if request_contain_keys(
                request.POST,
            ['subscribe_start_date', 'subscribe_end_date', 'search_topic']):

            current_datetime = get_local_datetime(request.user.timezone_offset)

            start_date = datetime.strptime(
                request.POST['subscribe_start_date'], '%Y-%m-%d')
            end_date = datetime.strptime(request.POST['subscribe_end_date'],
                                         '%Y-%m-%d')

            if current_datetime.date() > start_date.date():
                return Response(
                    status=status.HTTP_400_BAD_REQUEST,
                    data={
                        'data':
                        'Subscription start date cannot be less than current date'
                    })
            elif start_date > end_date:
                return Response(
                    status=status.HTTP_400_BAD_REQUEST,
                    data={
                        'data':
                        'Subscription start date cannot be greater than end date'
                    })

            if (
                    end_date - start_date
            ).days > request.user.plan_subscribed.subscription_period_max_days:
                return Response(
                    status=status.HTTP_400_BAD_REQUEST,
                    data={
                        'data':
                        'Subscription period exceeded select plan {} days limit!!'
                        .format(request.user.plan_subscribed.
                                subscription_period_max_days)
                    })

            response = is_action_allowed(request.user)

            if response is True:
                subscription, status_msg = add_subscription(
                    request.user, request.POST['search_topic'], start_date,
                    end_date)
            else:
                return Response(data={'data': response},
                                status=status.HTTP_400_BAD_REQUEST)

            if isinstance(subscription, SubscriptionModel):
                if subscription.subscription_status == 'IDLE':
                    token = generate_token(email=subscription.user.email,
                                           subscription_id=subscription.id)
                    confirmation_url = '{}/api/account/confirm_subscription?verification_code={}&format=json'.format(
                        get_host_origin(request), token)
                    send_subscription_verification_link(
                        subscription, confirmation_url)
                if status_msg == 'CREATED':
                    return Response(
                        status=status.HTTP_200_OK,
                        data={
                            'data':
                            'Subscription created, please check email to verify the subscription'
                        })
                elif status_msg == 'UPDATED':
                    return Response(status=status.HTTP_200_OK,
                                    data={'data': 'Subscription updated'})
            else:
                if status_msg == 'NOT_VERIFIED':
                    return Response(status=status.HTTP_400_BAD_REQUEST,
                                    data={'data': 'Email is not verified!!'})
                if status_msg == 'QUOTA_EXHAUSTED':
                    return Response(
                        status=status.HTTP_400_BAD_REQUEST,
                        data={
                            'data':
                            'You have reached your ACTIVE subscriptions quota!!'
                        })

        return Response(status=status.HTTP_400_BAD_REQUEST,
                        data={'data': 'Something went wrong!!'})