def post(self, request, subject_id):
     subject = Subject.objects.get(id=subject_id)
     is_hod = is_user_hod(request, subject)
     staff_list = subject.staff.all()
     try:
         if is_hod:
             form = AssignOrRemoveStaffForm(request.POST)
             if form.is_valid():
                 for staff_id in form.cleaned_data['staffselect']:
                     staff = User.objects.get(id=staff_id)
                     subject.staff.remove(staff)
                     subject.save()
             else:
                 self.error = 'Something went wrong.'
                 self.status = 500
                 raise
         else:
             self.error = 'You are not an HOD'
             self.status = 403
     except:
         return render(request, self.template,
                       {
                           'is_hod': is_hod,
                           'staff_list': staff_list,
                           'subject': subject,
                           'error': self.error
                       }, status=self.status)
     return HttpResponseRedirect('/subject/' + subject_id)
 def post(self, request, subject_id):
     subject = Subject.objects.get(id=subject_id)
     is_hod = is_user_hod(request, subject)
     staff_list = {}
     for department in Department.objects.all():
         staff_list[department.name] = [x.user for
                                        x in department.profile_set.all()
                                        if is_user_hod_or_teacher(x)]
     try:
         if is_hod:
             form = AssignOrRemoveStaffForm(request.POST)
             if form.is_valid():
                 for staff_id in form.cleaned_data['staffselect']:
                     staff = User.objects.get(id=staff_id)
                     subject.staff.add(staff)
             else:
                 self.error = 'Something went wrong.'
                 self.status = 500
                 raise
         else:
             self.error = 'You are not an HOD'
             self.status = 403
     except:
         return render(request, self.template,
                       {
                           'is_hod': is_hod,
                           'staff_list': staff_list,
                           'subject': subject,
                           'error': self.error
                       }, status=self.status)
     return HttpResponseRedirect('/subject/' + subject_id)
 def get(self, request, subject_id):
     subject = Subject.objects.get(id=subject_id)
     is_hod = is_user_hod(request, subject)
     staff_list = subject.staff.all()
     return render(request, self.template,
                   {
                       'is_hod': is_hod,
                       'staff_list': staff_list,
                       'subject': subject
                   })
    def get(self, request, subject_id):
        RESOURCE_TYPES = {
            'presentation': 'Presentation',
            'paper_publication': 'Paper Publication',
            'subject_note': 'Subject Note',
            'project_thesis': 'Project Thesis',
            'seminar_report': 'Seminar Report',
            'university_question_paper': 'Previous University Question Paper'
        }

        try:
            subject = Subject.objects.get(id=subject_id)
            resource_list = {}
            for resource in subject.resource_set.all():
                restype = RESOURCE_TYPES[resource.category]
                if restype not in resource_list:
                    resource_list[restype] = []
                resource_list[restype].append(resource)
            number_of_categories = len(resource_list) * -1
            subscription_status = True
            is_hod = False
            has_staff = False
            is_staff = False
            subject_staff_list = subject.staff.all()
            if subject_staff_list:
                has_staff = True
            if request.user.is_authenticated():
                user = request.user
                if subject not in user.subscribedsubjects.all():
                    subscription_status = False
                is_hod = is_user_hod(request, subject)
                is_staff = is_user_hod_or_teacher(request, subject)
            return render(request, 'subject_resource_list.html',
                          {
                              'subject': subject,
                              'resource_list': resource_list,
                              'subscription_status': subscription_status,
                              'is_hod': is_hod,
                              'is_staff': is_staff,
                              'has_staff': has_staff,
                              'subject_staff_list': subject_staff_list,
                              'number_of_categories': number_of_categories
                          })
        except ObjectDoesNotExist, e:
            print e
            self.error = 'The subject you requested does not exist.'
            self.status = 404
            return render(request, 'error.html',
                          {
                              'error': self.error
                          }, status=self.status)
 def get(self, request, subject_id):
     subject = Subject.objects.get(id=subject_id)
     is_hod = is_user_hod(request, subject)
     staff_list = {}
     for department in Department.objects.all():
         staff_list[department.name] = [x.user for
                                        x in department.profile_set.all()
                                        if is_user_hod_or_teacher(x)]
     return render(request, self.template,
                   {
                       'is_hod': is_hod,
                       'staff_list': staff_list,
                       'subject': subject
                   })