from django.shortcuts import render, redirect
from django.views.generic import ListView
from django.contrib import messages

from .forms import NewCourseForm, GradeForm
from .models import GradeColumn

from students.models import Student
from courses.models import Course
# Create your views here.


def course_create(request):
    if request.method == 'POST':
        form = NewCourseForm(request.POST)
        if form.is_valid():
            form.save(commit=False)
            return redirect()
    else:
        form = NewCourseForm()
    return render(
        request,
        'course_create.html',
        {
            "form": form,
        }
    )


class CourseGradeView(ListView):
    model = GradeColumn
    template_name = "course_grade.html"



def enroll_student_to_course(request, course_id, student_id):
    course = Course.objects.get(pk=course_id)
    student = Student.objects.get(pk=student_id)
    course.students.add(student)
    course.save()
    messages.success(request, 'The student is successfuly added.')
    return redirect('/')

def post_student_grade(request):
    if request.method == 'POST':
        form = GradeForm(request.POST)
        if form.is_valid():
            form.save(commit=False)
            return redirect('/')
    else:
        form = GradeForm()
    return render(
        request,
        'post_student_grade.html',
        {
            "form": form,
        }
    )

@@ -42,50 +42,27 @@
 rect
-%0Afrom django.views.generic import ListView
+, get_object_or_404
 %0Afro
@@ -143,39 +143,8 @@
 orm%0A
-from .models import GradeColumn
 %0A%0Afr
@@ -210,16 +210,36 @@
 t Course
+, CourseAnnouncement
 %0A# Creat
@@ -394,32 +394,38 @@
 d():%0A           
+ obj =
  form.save(commi
@@ -415,36 +415,24 @@
 = form.save(
-commit=False
 )%0A          
@@ -449,16 +449,19 @@
 edirect(
+%22/%22
 )%0A    el
@@ -619,105 +619,297 @@
 )%0A%0A%0A
-class C
+def list_c
 ourse
-G
+_g
 rade
-View(ListView):%0A    model = G
+_column(request, course_id):%0A    course_obj = get_object_or_404(Course, pk=course_id)%0A    qs = course_obj.g
 rade
-C
+c
 olumn
-%0A    template_name = %22course_grade.html%22%0A
+_set.all()%0A%0A    return render(request, %22course_grade.html%22,%0A        %7B%0A            'course': course_obj,%0A            'gradecolumns': qs,%0A        %7D%0A    )
 %0A%0A%0Ad
@@ -1550,28 +1550,545 @@
 orm%22: form,%0A        %7D%0A    )%0A
+%0Adef instructor_view_course_stundets_announcments(request, course_id):%0A    course = get_object_or_404(Course, pk=course_id)%0A    students = Student.objects.filter(course__pk=course_id)%0A    announcments = CourseAnnouncement.objects.filter(course__pk=course_id)%0A    return render(request, %22course_details_announcments.html%22,%0A                  %7B%0A                      'course': course,%0A                      'students': students,%0A                      'announcments': announcments%0A                  %7D%0A                  )%0A

work inprogress