Beispiel #1
0
def forgot_pass(request):
    try:
        if request.method == 'POST':
            form = forgotPasswordForm(request.POST)
            if form.is_valid():
                mobile_number = request.POST.get('Mobile_Number')
                response = BaseHandler.mobile_number_exists(mobile_number)
                if response is False:
                    messages.add_message(request, messages.ERROR,
                                         "Mobile number does not exists")
                else:
                    logging.info('Ready to process reset instructions')
                    response = PassResetHandler.process_reset_code(
                        mobile_number)
                    if response is True:
                        context = {
                            'form': passResetCodeVerify(),
                            'person': mobile_number,
                        }

                        return render(
                            request,
                            'alpha/accounts/activate_password_reset.html',
                            context)
                    else:
                        messages.add_message(
                            request, messages.ERROR,
                            "Request failed, please try again!")
        else:
            form = forgotPasswordForm()
        return render(request, 'alpha/accounts/forgot_password.html',
                      {'form': form})
    except Exception as e:
        logging.error(e)
        raise e
Beispiel #2
0
def reg_ister(request):
    try:
        form = SignupForm(request.POST or None, request.FILES or None)
        if request.method == 'POST':
            if form.is_valid():
                mobile_number = request.POST.get('mnumber')
                response = BaseHandler.mobile_number_exists(mobile_number)
                if response is True:
                    messages.add_message(request, messages.ERROR,
                                         "Mobile number already exists")
                else:
                    logging.info('Ready to process registration')
                    user = form.save(commit=False)
                    RegistrationHandler.register_user(user, mobile_number)
                    context = {
                        'form': smsCodeVerifyForm(),
                        'person': mobile_number,
                    }

                    return render(request,
                                  'alpha/accounts/activate_account.html',
                                  context)
        else:
            form = SignupForm()
        return render(request, 'alpha/accounts/signup.html', {'form': form})
    except Exception as e:
        logging.error(e)
        raise e
Beispiel #3
0
    def post(self, request, *args, **kwargs):
        response = {}

        try:
            serializer = passwordReset().get_serializer(
                data=request.data,
                context={"request": request}
            )
            serializer.is_valid(raise_exception=True)
            mobile_number = serializer.validated_data['mobile_number']
            new_password = serializer.validated_data['new_password']
            confirmed_password = serializer.validated_data['confirm_password']
            logging.info(str(mobile_number))
            res_ = BaseHandler.mobile_number_exists(mobile_number)
            if res_ is True:
                logging.info('Ready to reset password')
                min_length = 8
                if new_password != confirmed_password:
                    status = 400
                    response['status'] = False
                    response['code'] = 1
                    response['message'] = "Two passwords do not match"
                elif len(new_password) < min_length:
                    status = 400
                    response['status'] = False
                    response['code'] = 1
                    response['message'] = "This password must contain at least 8 characters."
                else:
                    reply = PassResetHandler.set_new_password(mobile_number, new_password)
                    if reply is True:
                        status = 201
                        response['status'] = True
                        response['code'] = 0
                        response['message'] = [
                            {
                                "body": "Password reset was successful",
                                "user": mobile_number
                            }
                        ]
                    else:
                        status = 500
                        response['status'] = False
                        response['code'] = 1
                        response['message'] = "Something went wrong. Please try again !!"
            else:
                status = 400
                response['status'] = False
                response['code'] = 1
                response['message'] = "Mobile number not registered"
        except Exception as e:
            logging.exception(e)
            response['status'] = False
            response['code'] = 1
            response['message'] = str(e)
            status = 400

        return JsonResponse(response, status=status)
Beispiel #4
0
 def register_activation_details(user, mobile_number, category):
     try:
         min_char = 6
         max_char = 6
         code = BaseHandler.get_verify_code(min_char, max_char)
         token = account_activation_token.make_token(user)
         act = activation_data()
         try:
             tk = activation_data.objects.filter(token=token)
         except(TypeError, ValueError, OverflowError, activation_data.DoesNotExist):
             tk = None
         if tk is not None:
             activation_data.objects.filter(token=token).delete()
             act.username = user.username
             act.code = code
             act.token = token
             act.mobile_number = mobile_number
             act.status = "unverified"
             act.date = timezone.now
             act.save()
         else:
             act.username = user.username
             act.code = code
             act.token = token
             act.mobile_number = mobile_number
             act.status = "unverified"
             act.date = timezone.now
             act.save()
         logging.info(" Your Phone is: {0}".format(str(mobile_number)))
         logging.info(" Your activation token is: {0}".format(str(token)))
         logging.info(" Your activation code is: {0}".format(str(code)))
         message = "Your Genus verification code is: {0}".format(code)
         logging.info(message)
         response = SendSMSHandler.africaStalkingSMS(mobile_number, message)
         africastalkingResponseHandler.record_africastalking_sms_response(response, category, mobile_number)
         BaseHandler.sent_sms_logs(mobile_number, message, category)
         # This is to be sent for email verification if email address applies
         # http://127.0.0.1:8000/a/activate/MTQ/506-07bac31c7df7ee8818bc/
         return True
     except Exception as e:
         logging.info(e)
         raise e
Beispiel #5
0
    def post(self, request, *args, **kwargs):
        response = {}
        try:
            serializer = RegistrationVeryPhone().get_serializer(
                data=request.data,
                context={"request": request}
            )
            serializer.is_valid(raise_exception=True)
            mobile_number = serializer.validated_data['mobile_number']
            code = serializer.validated_data['code']
            logging.info(mobile_number)
            logging.info(code)
            res_ = BaseHandler.mobile_number_exists(mobile_number)
            if res_ is True:
                response_ = RegistrationHandler.sms_code_activation(mobile_number, code)
                # return JsonResponse(
                #     RegistrationHandler.smsCodeAPIActivation(
                #         serializer.validated_data['mobile_number'],
                #         serializer.validated_data['code']
                #     ), safe=False
                # )
                if response_ == "error0":
                    status = 400
                    response['status'] = False
                    response['code'] = 1
                    response['message'] = "The code provided is invalid"
                elif response_ == "error1":
                    status = 400
                    response['status'] = False
                    response['code'] = 1
                    response['message'] = "Verification token invalid"
                elif response_ == "error2":
                    status = 400
                    response['status'] = False
                    response['code'] = 1
                    response['message'] = "Phone number and Code provided are invalid"
                else:
                    status = 201
                    response['status'] = True
                    response['code'] = 0
                    response['message'] = "Account phone verified successfully"
            else:
                status = 400
                response['status'] = False
                response['code'] = 1
                response['message'] = "Mobile number not registered"
        except Exception as e:
            logging.exception(e)
            response['status'] = False
            response['code'] = 1
            response['message'] = str(e)
            status = 400

        return JsonResponse(response, status=status)
Beispiel #6
0
 def register_profile(user, mobile_number):
     try:
         data = profile()
         data.username_id = user.id
         data.mobile_number = mobile_number
         data.operator = BaseHandler.telco_operator_finder(mobile_number)
         data.slug = mobile_number
         data.date = timezone.now
         data.save()
         RegistrationHandler.register_activation_details(user, mobile_number, sentSMSLogs.REG)
         record_user_activity(userActivities.USER_REGISTRATION, mobile_number,
                                              userActivities.COMPLETED)
         return True
     except Exception as e:
         logging.info(e)
         raise e
Beispiel #7
0
def pass_reset(request):
    try:
        if request.method == 'POST':
            form = SetPasswordForm(request.POST)
            if form.is_valid():
                mobile_number = request.POST.get('Mobile_Number')
                new_password = request.POST.get('New_Password')
                confirm_password = request.POST.get('Confirm_New_Password')
                response = BaseHandler.mobile_number_exists(mobile_number)
                if response is False:
                    messages.add_message(request, messages.ERROR,
                                         "Mobile number does not exists")
                else:
                    logging.info('Ready to reset password')
                    min_length = 8
                    if new_password != confirm_password:
                        messages.add_message(request, messages.ERROR,
                                             "Two passwords do not match")
                    elif len(new_password) < min_length:
                        messages.add_message(
                            request, messages.ERROR,
                            "This password must contain at least 8 characters."
                        )
                    else:
                        response = PassResetHandler.set_new_password(
                            mobile_number, new_password)
                        if response is True:
                            return redirect('core_login')
                        else:
                            messages.add_message(request, messages.ERROR,
                                                 str(response))
                context = {
                    'form': SetPasswordForm(),
                    'person': mobile_number,
                }
                return render(request, 'alpha/accounts/password_reset.html',
                              context)
        else:
            logging.info("This kind of request is not acceptable")
            return HttpResponse("This is not acceptable")
    except Exception as e:
        logging.error(e)
        raise e
Beispiel #8
0
def forgot_pass_verification(request):
    try:
        if request.method == 'POST':
            form = passResetCodeVerify(request.POST)
            if form.is_valid():
                mobile_number = request.POST.get('Mobile_Number')
                code = request.POST.get('Verification_Code')
                response = BaseHandler.mobile_number_exists(mobile_number)
                if response is False:
                    messages.add_message(request, messages.ERROR,
                                         "Mobile number does not exists")
                else:
                    response = RegistrationHandler.sms_code_activation(
                        mobile_number, code)
                    if response == "error0":
                        messages.add_message(request, messages.ERROR,
                                             "The code provided is invalid")
                    elif response == "error1":
                        messages.add_message(request, messages.ERROR,
                                             "Verification token invalid")
                    elif response == "error2":
                        messages.add_message(
                            request, messages.ERROR,
                            "Phone number and Code provided are invalid")
                    else:
                        logging.info("Ready to process password reset")
                        context = {
                            'form': SetPasswordForm(),
                            'person': mobile_number,
                        }

                        return render(request,
                                      'alpha/accounts/password_reset.html',
                                      context)
        else:
            form = passResetCodeVerify()
        return render(request, 'alpha/accounts/activate_password_reset.html',
                      {'form': form})
    except Exception as e:
        logging.error(e)
        raise e
Beispiel #9
0
    def post(self, request, *args, **kwargs):
        response = {}
        try:
            serializer = requestPasswordResetCode().get_serializer(
                data=request.data,
                context={"request": request}
            )
            serializer.is_valid(raise_exception=True)
            mobile_number = serializer.validated_data['mobile_number']
            logging.info(str(mobile_number))
            res_ = BaseHandler.mobile_number_exists(mobile_number)
            if res_ is True:
                response_ = PassResetHandler.process_reset_code(mobile_number)
                if response_ is True:
                    status = 201
                    response['status'] = True
                    response['code'] = 0
                    response['message'] = [
                        {
                            "body": "Password reset code sent via sms",
                            "user": mobile_number
                        }
                    ]
                else:
                    status = 500
                    response['status'] = True
                    response['code'] = 1
                    response['message'] = "Password reset code request failed. PLease try again !!"
            else:
                status = 400
                response['status'] = False
                response['code'] = 1
                response['message'] = "Mobile number not registered"
        except Exception as e:
            logging.exception(e)
            response['status'] = False
            response['code'] = 1
            response['message'] = str(e)
            status = 400

        return JsonResponse(response, status=status)
Beispiel #10
0
    def post(self, request, *args, **kwargs):
        response = {}
        try:
            serializer = RegisterUsersApi().get_serializer(
                data=request.data,
                context={"request": request}
            )
            serializer.is_valid(raise_exception=True)
            mobile_number = serializer.validated_data['mobile_number']
            password = serializer.validated_data['password']
            username = serializer.validated_data['username']
            logging.info(mobile_number)
            logging.info(username)
            logging.info(password)
            res_ = BaseHandler.mobile_number_exists(mobile_number)
            if res_ is True:
                status = 400
                response['status'] = False
                response['code'] = 1
                response['message'] = "Mobile number already exists"
            else:
                user = User()
                user.username = username
                user.set_password(password)
                RegistrationHandler.register_user_api(user, mobile_number, username)
                status = 201
                response['status'] = True
                response['code'] = 0
                response['message'] = "Registration was successful"
        except Exception as e:
            logging.exception(e)
            response['status'] = False
            response['code'] = 1
            response['message'] = str(e)
            status = 400

        return JsonResponse(response, status=status)