from django.contrib.auth.models import User
from django.shortcuts import render, get_object_or_404
from .models import Student, Classroom, StudentForm, ClassroomForm
from django.shortcuts import render, redirect
from django.contrib import messages

# Create your views here.
def createStudent(request):
    classroomsByUser = Classroom.objects.filter(teacher=request.user)
    if request.method == 'POST':
        form = StudentForm(request.POST)
        if form.is_valid():
            student = form.save(commit=False)
            student.teacher = request.user
            student.examTaken = 'N'
            student.grade = student.classroom.grade
            student.save()
            return redirect('dashboard:dashboard')
    else:
        form = StudentForm()
        form.fields['classroom'].queryset = classroomsByUser
    return render(request, 'studentForm.html', {'form':form})


def createClassroom(request):
    if request.method == 'POST':
        form = ClassroomForm(request.POST)
        if form.is_valid():
            classroom = form.save(commit=False)
            classroom.teacher = request.user
            classroom.save()
            return redirect('dashboard:dashboard')
    else:
        form = ClassroomForm()
    return  render(request, 'classroomForm.html', { 'form': form })


def students(request):
    studentList = Student.objects.filter(teacher=request.user)
    return render(request, 'students_list.html', {'students': studentList})


def studentInfo(request, pk):
    student = get_object_or_404(Student, pk=pk)
    return render(request, 'student_info.html', {'student': student})
