Skip to content

Instantly share code, notes, and snippets.

@pyaf
Created March 24, 2017 12:13
Show Gist options
  • Save pyaf/1ba80df554cc4032d55b77938d261f2d to your computer and use it in GitHub Desktop.
Save pyaf/1ba80df554cc4032d55b77938d261f2d to your computer and use it in GitHub Desktop.
class LoginView(FormView):
template_name = 'login.html'
form_class = LoginForm
success_url = '/dashboard'
def dispatch(self, request, *args, **kwargs):
if self.request.user.is_authenticated:
messages.add_message(self.request, messages.INFO,
"User already logged in")
return redirect('/dashboard')
else:
return super().dispatch(request, *args, **kwargs)
def form_valid(self, form):
user = form.login(self.request)
print(user.two_factor_auth)
if user.two_factor_auth is False:
login(self.request, user)
return redirect('/dashboard')
else:
try:
response = send_verfication_code(user)
pass
except Exception as e:
messages.add_message(self.request, messages.ERROR,
'verification code not sent. \n'
'Please retry logging in.')
return redirect('/login')
data = json.loads(response.text)
if data['success'] == False:
messages.add_message(self.request, messages.ERROR,
data['message'])
return redirect('/login')
print(response.status_code, response.reason)
print(response.text)
if data['success'] == True:
self.request.method = "GET"
print(self.request.method)
kwargs = {'user':user}
return PhoneVerificationView(self.request, **kwargs)
else:
messages.add_message(self.request, messages.ERROR,
data['message'])
return redirect('/login')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment