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)

[((473, 492), 'application.views.get_navbar', 'get_navbar', (['request'], {}), '(request)\n', (483, 492), False, 'from application.views import get_navbar, Page\n'), ((524, 542), 'application.forms.AuthenticateForm', 'AuthenticateForm', ([], {}), '()\n', (540, 542), False, 'from application.forms import AuthenticateForm\n'), ((558, 599), 'django.shortcuts.render', 'render', (['request', 'Page.login', 'self.context'], {}), '(request, Page.login, self.context)\n', (564, 599), False, 'from django.shortcuts import render, redirect\n'), ((664, 683), 'application.views.get_navbar', 'get_navbar', (['request'], {}), '(request)\n', (674, 683), False, 'from application.views import get_navbar, Page\n'), ((727, 749), 'application.forms.AuthenticateForm', 'AuthenticateForm', (['data'], {}), '(data)\n', (743, 749), False, 'from application.forms import AuthenticateForm\n'), ((1333, 1355), 'application.forms.AuthenticateForm', 'AuthenticateForm', (['data'], {}), '(data)\n', (1349, 1355), False, 'from application.forms import AuthenticateForm\n'), ((1371, 1412), 'django.shortcuts.render', 'render', (['request', 'Page.login', 'self.context'], {}), '(request, Page.login, self.context)\n', (1377, 1412), False, 'from django.shortcuts import render, redirect\n'), ((797, 863), 'django.contrib.auth.authenticate', 'authenticate', ([], {'username': "data['username']", 'password': "data['password']"}), "(username=data['username'], password=data['password'])\n", (809, 863), False, 'from django.contrib.auth import login, authenticate\n'), ((1108, 1196), 'django.contrib.messages.error', 'messages.error', (['request', '"""Invalid username and password pair."""'], {'extra_tags': '"""danger"""'}), "(request, 'Invalid username and password pair.', extra_tags=\n    'danger')\n", (1122, 1196), False, 'from django.contrib import messages\n'), ((1218, 1306), 'django.contrib.messages.error', 'messages.error', (['request', '"""Invalid username and password pair."""'], {'extra_tags': '"""danger"""'}), "(request, 'Invalid username and password pair.', extra_tags=\n    'danger')\n", (1232, 1306), False, 'from django.contrib import messages\n'), ((948, 968), 'django.contrib.auth.login', 'login', (['request', 'user'], {}), '(request, user)\n', (953, 968), False, 'from django.contrib.auth import login, authenticate\n'), ((985, 1046), 'django.contrib.messages.success', 'messages.success', (['request', '"""You have successfully logged in!"""'], {}), "(request, 'You have successfully logged in!')\n", (1001, 1046), False, 'from django.contrib import messages\n'), ((1079, 1094), 'django.urls.reverse', 'reverse', (['"""main"""'], {}), "('main')\n", (1086, 1094), False, 'from django.urls import reverse\n')]