from django.http import Http404
from django.shortcuts import render
from .models import Assignment, Course
from django.views.generic import CreateView

# Create your views here.
def index(request):
    course_list = Course.objects.order_by("course_code")
    assignment_list = Assignment.objects.order_by("name")
    context = {
       "assignment_list": assignment_list,
       "course_list": course_list,
    }
    
    return render(request, "assignments/index.html", context)

def details(request, assignment_id):
   try:
         assignment = Assignment.objects.get(pk=assignment_id)
   except Assignment.DoesNotExist:
      raise Http404("Assignment does not exist!")

   return render(request, "assignments/detail.html", {"assignment": assignment})

class addAssignment(CreateView):
    model = Assignment
    template_name = "assignments/add.html"
    fields = ['name', 'description', 'max_points', 'course', 'assignment_image']