Exemple #1
0
def ModifyAssignment(request, assg_id):
    if not request.user.is_authenticated():
        message = "Login Before To Continue."
        return direct_to_template(request, "users/login.html", locals())
    else:
        if request.user.is_staff:
            assignment = Assignment.objects.filter(id=assg_id)[0]
            if not assignment.assg_status == "Locked":
                students = User.objects.filter(is_staff=0)
                form = AssignmentCreateForm(
                    {"assg_title": assignment.assg_title, "assg_details": assignment.assg_details}
                )
                for student in students:
                    if Assg_Stats.objects.filter(assg_id=assg_id, assg_student_rno=student.username).count():
                        student.cbx_chk = True
                if request.method == "POST":
                    if request.POST.get("lock_now", ""):
                        Assignment.objects.filter(id=assg_id).update(assg_status="Locked")
                        assg_just_locked = True
                        return direct_to_template(request, "Assignment/modify.html", locals())
                    assg_title = request.POST["assg_title"]
                    assg_details = request.POST["assg_details"]
                    modify_success = False
                    if assg_title.__len__() < 6:
                        message = "Title to be minimum six characters"
                    elif assg_details.__len__() < 10:
                        message = "Insufficient Details"
                    else:
                        modify_success = True
                        Assignment.objects.filter(id=assg_id).update(assg_title=assg_title, assg_details=assg_details)
                        message = "Successfully modified the assignment."
                    for student in students:
                        stu_id = str(student.id)
                        if not request.POST.get("cbx" + stu_id, ""):
                            Assg_Stats.objects.filter(assg_id=assg_id, assg_student_rno=student.username).delete()
                            student.cbx_chk = False
                        else:  # overwrite or include new students, no students selected err
                            if not Assg_Stats.objects.filter(
                                assg_id=assg_id, assg_student_rno=student.username
                            ).count():
                                new_stat = Assg_Stats(
                                    assg_id=assg_id,
                                    assg_student_rno=student.username,
                                    assg_student_name=student.first_name,
                                    assg_solution_status="Pending",
                                    assg_submit_time="2099-12-31 23:59:59",
                                    assg_marks=-1,
                                )
                                new_stat.save()
                                student.cbx_chk = True
                    try:
                        t = request.POST["lock_dt_tm"]
                        y = int(t[6] + t[7] + t[8] + t[9])
                        m = int(t[0] + t[1])
                        d = int(t[3] + t[4])
                        h = int(t[11] + t[12])
                        mm = int(t[14] + t[15])
                        lock_dt_tm = datetime.datetime(y, m, d, h, mm)
                        Assignment.objects.filter(id=assg_id).update(assg_lock_datetime=lock_dt_tm)
                    except:
                        message = "Invalid Date!"
                        return direct_to_template(request, "Assignment/modify.html", locals())
                    assignment = Assignment.objects.filter(id=assg_id)[0]
                    if modify_success:
                        form = AssignmentCreateForm(
                            {"assg_title": assignment.assg_title, "assg_details": assignment.assg_details}
                        )
                    else:
                        form = AssignmentCreateForm(request.POST)
                lock_dt_tm = assignment.assg_lock_datetime
                if lock_dt_tm == datetime.datetime(2099, 12, 31, 0, 0):
                    lock_dt_tm = datetime.datetime.now() + timedelta(days=3)
            else:
                assg_locked = True
                message = "You cannot modify this assignment as it has been locked."
            return direct_to_template(request, "Assignment/modify.html", locals())
        else:
            return HttpResponseRedirect("/dashboard")
Exemple #2
0
def CreateAssignment(request):
    if request.user.is_authenticated():
        user_profile = UserProfile.objects.get(rno=request.user.username)
        if request.user.is_staff:
            form = AssignmentCreateForm(request.POST)
            students = User.objects.filter(is_staff=0)
            if request.method == "POST":
                if form.is_valid():
                    try:
                        t = request.POST["lock_dt_tm"]
                        y = int(t[6] + t[7] + t[8] + t[9])
                        m = int(t[0] + t[1])
                        d = int(t[3] + t[4])
                        h = int(t[11] + t[12])
                        mm = int(t[14] + t[15])
                        lock_dt_tm = datetime.datetime(y, m, d, h, mm)
                    except:
                        for student in students:
                            stu_id = str(student.id)
                            if request.POST.get("cbx" + stu_id, ""):
                                student.cbx_chk = True
                            else:
                                student.cbx_chk = False
                        message = "Invalid Date!"
                        return direct_to_template(request, "Assignment/create.html", locals())
                    new_assg = Assignment(
                        created_by=request.user.username,
                        assg_status="Active",
                        assg_details=request.POST["assg_details"],
                        assg_title=request.POST["assg_title"],
                        creation_time=datetime.datetime.now(),
                        assg_lock_datetime=lock_dt_tm,
                    )
                    new_assg.save()
                    for student in students:
                        stu_id = str(student.id)
                        if request.POST.get("cbx" + stu_id, ""):
                            new_stat = Assg_Stats(
                                assg_id=new_assg.id,
                                assg_student_rno=student.username,
                                assg_student_name=student.first_name,
                                assg_solution_status="Pending",
                                assg_submit_time="2099-12-31 23:59:59",
                                assg_marks=-1,
                            )
                            new_stat.save()
                    create_success = True
                    return direct_to_template(request, "Assignment/create.html", locals())
                else:
                    for student in students:
                        stu_id = str(student.id)
                        if request.POST.get("cbx" + stu_id, ""):
                            student.cbx_chk = True
                        else:
                            student.cbx_chk = False
                    return direct_to_template(request, "Assignment/create.html", locals())
            else:
                form = AssignmentCreateForm()
                req = request
                return direct_to_template(request, "Assignment/create.html", locals())
        else:
            return HttpResponseRedirect("/dashboard/")
    else:
        return HttpResponseRedirect("/login/")