from django.contrib import messages
from django.contrib.auth import login, authenticate
from django.shortcuts import render, redirect
from django.urls import reverse
from django.views import View

from application.forms import AuthenticateForm
from application.views import get_navbar, Page


class LoginView(View):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.context = {}

    def get(self, request):
        self.context['navbar'] = get_navbar(request)
        self.context['form'] = AuthenticateForm()
        return render(request,  Page.login, self.context)

    def post(self, request):
        self.context['navbar'] = get_navbar(request)
        data = request.POST
        form = AuthenticateForm(data)
        if form.is_valid():
            user = authenticate(
                username=data['username'],
                password=data['password'],
            )
            if user:
                login(request, user)
                messages.success(request, 'You have successfully logged in!')
                return redirect(reverse('main'))
            messages.error(request, 'Invalid username and password pair.', extra_tags='danger')
        else:
            messages.error(request, 'Invalid username and password pair.', extra_tags='danger')
        self.context['form'] = AuthenticateForm(data)
        return render(request, Page.login, self.context)

[((17, 33, 17, 52), 'application.views.get_navbar', 'get_navbar', ({(17, 44, 17, 51): 'request'}, {}), '(request)', False, 'from application.views import get_navbar, Page\n'), ((18, 31, 18, 49), 'application.forms.AuthenticateForm', 'AuthenticateForm', ({}, {}), '()', False, 'from application.forms import AuthenticateForm\n'), ((19, 15, 19, 57), 'django.shortcuts.render', 'render', ({(19, 22, 19, 29): 'request', (19, 32, 19, 42): 'Page.login', (19, 44, 19, 56): 'self.context'}, {}), '(request, Page.login, self.context)', False, 'from django.shortcuts import render, redirect\n'), ((22, 33, 22, 52), 'application.views.get_navbar', 'get_navbar', ({(22, 44, 22, 51): 'request'}, {}), '(request)', False, 'from application.views import get_navbar, Page\n'), ((24, 15, 24, 37), 'application.forms.AuthenticateForm', 'AuthenticateForm', ({(24, 32, 24, 36): 'data'}, {}), '(data)', False, 'from application.forms import AuthenticateForm\n'), ((37, 31, 37, 53), 'application.forms.AuthenticateForm', 'AuthenticateForm', ({(37, 48, 37, 52): 'data'}, {}), '(data)', False, 'from application.forms import AuthenticateForm\n'), ((38, 15, 38, 56), 'django.shortcuts.render', 'render', ({(38, 22, 38, 29): 'request', (38, 31, 38, 41): 'Page.login', (38, 43, 38, 55): 'self.context'}, {}), '(request, Page.login, self.context)', False, 'from django.shortcuts import render, redirect\n'), ((26, 19, 29, 13), 'django.contrib.auth.authenticate', 'authenticate', (), '', False, 'from django.contrib.auth import login, authenticate\n'), ((34, 12, 34, 95), 'django.contrib.messages.error', 'messages.error', (), '', False, 'from django.contrib import messages\n'), ((36, 12, 36, 95), 'django.contrib.messages.error', 'messages.error', (), '', False, 'from django.contrib import messages\n'), ((31, 16, 31, 36), 'django.contrib.auth.login', 'login', ({(31, 22, 31, 29): 'request', (31, 31, 31, 35): 'user'}, {}), '(request, user)', False, 'from django.contrib.auth import login, authenticate\n'), ((32, 16, 32, 77), 'django.contrib.messages.success', 'messages.success', ({(32, 33, 32, 40): 'request', (32, 42, 32, 76): '"""You have successfully logged in!"""'}, {}), "(request, 'You have successfully logged in!')", False, 'from django.contrib import messages\n'), ((33, 32, 33, 47), 'django.urls.reverse', 'reverse', ({(33, 40, 33, 46): '"""main"""'}, {}), "('main')", False, 'from django.urls import reverse\n')]