Created
October 13, 2022 18:48
-
-
Save CryceTruly/26620d59b32d0792f5d6cfe896dc5ab2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LogoutView(View): | |
def get(self, request): | |
logout(request) | |
return redirect('welcome') | |
class WelcomeView(View): | |
def get(self, request): | |
if request.user.is_authenticated: | |
return redirect('api-documentation') | |
return render(request, 'home/welcome.html') | |
def post(self, request): | |
context = { | |
'data': request.POST, | |
'has_error': False | |
} | |
username = request.POST.get('username') | |
password = request.POST.get('password') | |
if username == '': | |
messages.add_message(request, messages.ERROR, | |
'Username is required') | |
context['has_error'] = True | |
if password == '': | |
messages.add_message(request, messages.ERROR, | |
'Password is required') | |
context['has_error'] = True | |
user = authenticate(request, username=username, password=password) | |
if not user and not context['has_error']: | |
messages.add_message(request, messages.ERROR, 'Invalid login') | |
context['has_error'] = True | |
if context['has_error']: | |
return render(request, 'home/welcome.html', status=401, context=context) | |
group_names = [] | |
for group in user.groups.all(): | |
group_names.append(group.name) | |
if "developers" in group_names or "partners" in group_names: | |
login(request, user) | |
return redirect('api-documentation') | |
messages.add_message(request, messages.ERROR, 'Invalid login') | |
return render(request, 'home/welcome.html', status=401, context=context) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment