Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
5.7 kB
5
Indexable
Never
    def changeform_view(self, request, object_id=None, form_url='', extra_context=None):
        try:
            if request.user.groups.filter(name__in=['accounts_admin']):
                extra_context = {'is_account_admin':True,'title':'Super Proctor Details'}
            else:
                extra_context = {'title':''}
            if request.user.groups.filter(name='GuestFaculty').exists():
                extra_context = {'title':''}
                if request.method == 'POST':
                    exam_master_id = request.POST.get('exam_master', '')
                    location_name = request.POST.get('location_name', '')
                    available_for = request.POST.get('available_for', '')
                    userpan = User.objects.filter(id=request.user.id).values_list('pan_number', flat=True)[0]  
                    gf = GuestFaculty.objects.filter(pan_number=userpan).last()

                    if exam_master_id and location_name and available_for and gf:
                        exam_master = ExamMaster.objects.get(id=exam_master_id)
                        check = SuperProctor.objects.filter(exam_master = exam_master, guest_faculty=gf)
                        if check:
                            messages.error(request, "Already submitted for this exam.")
                            return super(SuperProctorAdmin, self).changeform_view(request, object_id, form_url, extra_context)
                        ex_type = available_for.split(' ')[0]
                        session_rate = exam_master.compensation_per_session
                        if ex_type == 'Regular':
                            total_no_of_ses = exam_master.regular.total_session
                        elif ex_type == 'Makeup':
                            total_no_of_ses = exam_master.makeup.total_session
                        else:
                            total_no_of_ses = exam_master.regular.total_session + exam_master.makeup.total_session

                        comp_amt = (session_rate*total_no_of_ses)
                        super_proctor = SuperProctor.objects.create(
                                exam_master=exam_master,
                                location_name=location_name,
                                available_for=available_for,
                                guest_faculty=gf,
                                compensation_amount=comp_amt,
                                status='SUBMITTED')
                        messages.success(request, "Super Proctor created successfully!")
                        redirect_url = reverse('admin:%s_%s_changelist' % (self.opts.app_label, self.opts.model_name))
                        return HttpResponseRedirect(redirect_url)
                    else:
                        messages.error(request, "Please select all fields.")
            elif request.method == 'POST':
                comp_status = request.POST.get('review_status','')
                review_comment = request.POST.get('review_comment','')
                exam_master_id = request.POST.get('exam_master', '')
                center_name = request.POST.get('center_name', '')
                status = request.POST.get('status', '')
                
                super_proctor = SuperProctor.objects.filter(pk=object_id).last()
                print(center_name,'---------------------------------------------')

                if super_proctor.center_name == None:
                    if center_name:
                        super_proctor.center_name = center_name
                        super_proctor.save()
                        return super(SuperProctorAdmin, self).changeform_view(request, object_id, form_url,
                                                                              extra_context)
                    else:
                        messages.error(request, "Please select center name.")
                        return HttpResponseRedirect(
                            reverse('admin:%s_%s_change' % (self.opts.app_label, self.opts.model_name), args=(object_id,)))
                elif super_proctor.status == SUPERPROCTORSTATUS[1][1]:
                    if comp_status and review_comment:
                        if super_proctor:
                            super_proctor.review_status = comp_status
                            super_proctor.review_comment = review_comment
                            super_proctor.save()
                            messages.success(request, "SuperProctor updated successfully!")
                        return HttpResponseRedirect(reverse('admin:%s_%s_change' % (self.opts.app_label, self.opts.model_name), args=(object_id,)))
                    elif comp_status:
                        messages.error(request, "Please enter Review comment.")
                        return HttpResponseRedirect(
                            reverse('admin:%s_%s_change' % (self.opts.app_label, self.opts.model_name), args=(object_id,)))
                    elif review_comment:
                        messages.error(request, "Please select Review status.")
                        return HttpResponseRedirect(
                            reverse('admin:%s_%s_change' % (self.opts.app_label, self.opts.model_name), args=(object_id,)))
                    elif not review_comment and not comp_status:
                        messages.error(request, "Review status and Review comment are mandatory.")
                        return HttpResponseRedirect(
                            reverse('admin:%s_%s_change' % (self.opts.app_label, self.opts.model_name), args=(object_id,)))
                    else:
                        return super(SuperProctorAdmin, self).changeform_view(request, object_id, form_url, extra_context)
Leave a Comment