def process_step(self, form): if self.steps.current == '1': # Process contact info step sms_enabled = form.cleaned_data['sms'] self.request.user.phone = form.cleaned_data['phone'] # If phone number has not been provided the user cannot enroll in SMS messaging if form.cleaned_data['phone'] in [None, '']: sms_enabled = 'False' if sms_enabled == 'True': self.request.user.carrier = form.cleaned_data['carrier'] else: self.request.user.carrier = '' self.request.user.save() # Add or remove the SMS verification form based on whether or not the user opted in or out end = len(self.form_list) exists = False for i in range(end): if self.form_list[str(i)] == SMSVerificationForm: exists = True if sms_enabled == 'False': del self.form_list[str(i)] if sms_enabled == 'True': if not exists: self.form_list[str(end)] = SMSVerificationForm # Generate and send the user the verification code code = randint(100000, 999999) message = { "user": self.request.user, "message": str(code) + "\nUse this code for LNL verification" } email = generate_sms_email(message) email.send() existing_code = PhoneVerificationCode.objects.filter( user=self.request.user).first() if existing_code: existing_code.delete() PhoneVerificationCode.objects.create(user=self.request.user, code=code) # Ensure carrier field is set to "Opt out" until the user has verified their phone number self.request.user.carrier = '' self.request.user.save() return self.get_form_step_data(form)
def request_token(request): missing = True sent = False context = {'NO_FOOT': True, 'NO_NAV': True, 'NO_API': True, 'styles': "#main {\n\tbackground-color: white !important;\n\theight: 100vh;\n}\n.text-white {" "\n\tcolor: black !important;\n}\n.help-block {\n\tdisplay: none;\n}"} if request.user.phone and request.user.carrier: missing = False context['phone'] = "(" + request.user.phone[:3] + ") " + request.user.phone[3:6] + "-" + request.user.phone[6:] if request.method == 'POST': form = SMSOptInForm(request.POST, instance=request.user, request=request, exists=not missing) if form.is_valid(): if missing: form.save() missing = False form = SMSOptInForm(request=request, exists=not missing) context['phone'] = "(" + request.user.phone[:3] + ") " + request.user.phone[3:6] + "-" + \ request.user.phone[6:] else: code = randint(100000, 999999) message = { "user": request.user, "message": str(code) + "\nUse this code for LNL verification" } email = generate_sms_email(message) try: token_request = TokenRequest.objects.get(user=request.user) token_request.code = code token_request.attempts = settings.TFV_ATTEMPTS token_request.timestamp = timezone.now() token_request.save() except TokenRequest.DoesNotExist: TokenRequest.objects.create(user=request.user, code=code, attempts=settings.TFV_ATTEMPTS, timestamp=timezone.now()) email.send() sent = True else: form = SMSOptInForm(instance=request.user, request=request, exists=not missing) context['form'] = form context['missing'] = missing context['sent'] = sent return render(request, "phone_verification.html", context)