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, 'application.views.get_navbar', 'get_navbar', (['request'], {}), False, 'from application.views import get_navbar, Page\n'), (18, 'application.forms.AuthenticateForm', 'AuthenticateForm', ([], {}), False, 'from application.forms import AuthenticateForm\n'), (19, 'django.shortcuts.render', 'render', (['request', 'Page.login', 'self.context'], {}), False, 'from django.shortcuts import render, redirect\n'), (22, 'application.views.get_navbar', 'get_navbar', (['request'], {}), False, 'from application.views import get_navbar, Page\n'), (24, 'application.forms.AuthenticateForm', 'AuthenticateForm', (['data'], {}), False, 'from application.forms import AuthenticateForm\n'), (37, 'application.forms.AuthenticateForm', 'AuthenticateForm', (['data'], {}), False, 'from application.forms import AuthenticateForm\n'), (38, 'django.shortcuts.render', 'render', (['request', 'Page.login', 'self.context'], {}), False, 'from django.shortcuts import render, redirect\n'), (26, 'django.contrib.auth.authenticate', 'authenticate', ([], {'username': "data['username']", 'password': "data['password']"}), False, 'from django.contrib.auth import login, authenticate\n'), (34, 'django.contrib.messages.error', 'messages.error', (['request', '"""Invalid username and password pair."""'], {'extra_tags': '"""danger"""'}), False, 'from django.contrib import messages\n'), (36, 'django.contrib.messages.error', 'messages.error', (['request', '"""Invalid username and password pair."""'], {'extra_tags': '"""danger"""'}), False, 'from django.contrib import messages\n'), (31, 'django.contrib.auth.login', 'login', (['request', 'user'], {}), False, 'from django.contrib.auth import login, authenticate\n'), (32, 'django.contrib.messages.success', 'messages.success', (['request', '"""You have successfully logged in!"""'], {}), False, 'from django.contrib import messages\n'), (33, 'django.urls.reverse', 'reverse', (['"""main"""'], {}), False, 'from django.urls import reverse\n')]