from django.shortcuts import render, redirect
from django.views.generic.edit import FormView
from .forms import RegisterForm, LoginForm
from django.contrib.auth.hashers import check_password, make_password
from .models import Fcuser
# Create your views here.
def index(request):
    return render(request,'index.html' ,{'email' : request.session.get('user')})

class RegisterView(FormView):
    template_name = 'register.html' # register.html
    form_class = RegisterForm
    success_url = '/'

    def form_valid(self,form): # 유효성 검사 마치면 fcuser 생성
        fcuser = Fcuser(
            email = form.data.get('email'),
            password = make_password(form.data.get('password'))

        )
        fcuser.save()
        return super().form_valid(form) # 유효성 검사와 모델이 저장되는 부분을 분리 

class LoginView(FormView):
    template_name = 'login.html' # register.html
    form_class = LoginForm
    success_url = '/'

    def form_valid(self, form):
        self.request.session['user'] = form.data.get('email')
        return super().form_valid(form)
def logout(request):
    if 'user' in request.session:
        del(request.session['user'])
    return redirect('/')