from django.shortcuts import render, redirect
from fbvApp.models import Student
from fbvApp.forms import StudentForm
from django.contrib.auth.decorators import login_required,permission_required


# Create your views here.
@login_required()
def get_student(request):
    students = Student.objects.all()
    return render(request, 'fbvApp/index.html', {'students': students})


@login_required()
def create_student(request):
    form = StudentForm()
    if request.method == "POST":
        form = StudentForm(request.POST)
        if form.is_valid():
            form.save()
        return redirect('/home/')

    return render(request, 'fbvApp/create.html', {'form': form})


@login_required()
@permission_required('fbvApp.delete_student')
def delete_student(request, id):
    student = Student.objects.get(id=id)
    student.delete()
    return redirect('/')


@login_required()
def update_student(request, id):
    student = Student.objects.get(id=id)
    form = StudentForm(instance=student)
    if request.method == 'POST':
        form = StudentForm(request.POST, instance=student)
        if form.is_valid:
            form.save()
            return redirect('/')
    return render(request, 'fbvApp/update.html', {'form': form})


@login_required()
def post(request):
    students = Student.objects.all()
    return render(request, 'fbvApp/post.html', {'students': students})


def logout(request):
    return render(request, 'fbvApp/logout.html')
