from django.views.generic import DetailView
#from django.http import Http404

from devilry.apps.core.models import Delivery


class SingleDeliveryView(DetailView):
    template_name = "devilry_examiner/singledelivery.django.html"
    model = Delivery
    pk_url_kwarg = 'deliveryid'
    context_object_name = 'delivery'

    def get_queryset(self):
        return Delivery.objects.filter_examiner_has_access(self.request.user)\
            .select_related( # Use select_related to lookup all the related objects in the query
                'deadline',
                'deadline__assignment_group',
                'deadline__assignment_group__parentnode', # Assignment
                'deadline__assignment_group__parentnode__parentnode', # Period
                'deadline__assignment_group__parentnode__parentnode__parentnode', # Subject
                )

    def get_context_data(self, **kwargs):
        context = super(SingleDeliveryView, self).get_context_data(**kwargs)
        delivery = self.object
        
        assignment = delivery.deadline.assignment_group.assignment
        context['valid_grading_system_setup'] = assignment.has_valid_grading_setup()

        edit_feedback = delivery.last_feedback and self.request.GET.get('edit_feedback', False) == 'true'
        if edit_feedback:
            if assignment.has_valid_grading_setup():
                # TODO: Redirect to edit feedback view of the current grading system
                pass
        return context
@@ -37,16 +37,54 @@
 ailView%0A
+from django.shortcuts import redirect%0A
 #from dj
@@ -906,26 +906,20 @@
  get
-_context_data(self
+(self, *args
 , **
@@ -939,77 +939,109 @@
     
-context = super(SingleDeliveryView, self).get_context_data(**kwargs)%0A
+edit_feedback = self.request.GET.get('edit_feedback', False) == 'true'%0A        if edit_feedback:%0A    
     
@@ -1060,22 +1060,28 @@
  = self.
+get_
 object
+()
 %0A       
@@ -1081,21 +1081,16 @@
         
-%0A    
     assi
@@ -1111,92 +1111,33 @@
 ery.
-deadline.assignment_group.assignment%0A        context%5B'valid_grading_system_setup'%5D =
+assignment%0A            if
  ass
@@ -1173,18 +1173,23 @@
 up()
-%0A%0A
+:%0A     
         
 edit
@@ -1188,73 +1188,73 @@
     
-edit_feedback = delivery.last_feedback and self.request.GET
+   return redirect(assignment.get_gradingsystem_plugin_api()
 .get
-('
+_
 edit
@@ -1266,212 +1266,358 @@
 back
-', False) == 'true'%0A        if edit_feedback:%0A            if assignment.has_valid_grading_setup():%0A                # TODO: Redirect to edit feedback view of the current 
+_url(delivery.id))%0A        else:%0A            return super(SingleDeliveryView, self).get(*args, **kwargs)%0A%0A%0A    def get_context_data(self, **kwargs):%0A        context = super(SingleDeliveryView, self).get_context_data(**kwargs)%0A        delivery = self.object%0A        context%5B'valid_
 grading
- 
+_
 system
-%0A                pass
+_setup'%5D = delivery.assignment.has_valid_grading_setup()
 %0A   

Handle redirect on edit.