Skip to content

Instantly share code, notes, and snippets.

@CryceTruly
Created October 13, 2022 18:48
Show Gist options
  • Save CryceTruly/26620d59b32d0792f5d6cfe896dc5ab2 to your computer and use it in GitHub Desktop.
Save CryceTruly/26620d59b32d0792f5d6cfe896dc5ab2 to your computer and use it in GitHub Desktop.
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