Commit cd41d35f authored by NARRA SURAJ's avatar NARRA SURAJ

max_marks done, frontend and backend

parent 0a4a3412
......@@ -45,7 +45,7 @@ class TestForm(forms.ModelForm):
# 'expected_time_for_completion': forms.TimeInput(attrs={'type':'time'}),
# 'start_time': forms.DateInput(attrs={'type':'datetime-local'}),
}
exclude = ['test_ID', 'ownership']
exclude = ['test_ID', 'ownership', 'max_marks']
# def clean_start_time(self):
# dt = self.cleaned_data['start_time']
# print dt
......
......@@ -34,6 +34,7 @@ class Test(models.Model):
start_time = models.DateTimeField(auto_now=False, auto_now_add=False,blank=False)
max_marks = models.IntegerField(null=True)
# class TestQuestions(models.Model):
# test_ID = models.ForeignKey(
# 'Test', on_delete=models.CASCADE)
......
{%extends 'base2.html'%}
{%block head%}
<script>
total_marks = 0;
num_questions = 0;
function dispTotalMarks() {
$('#total_marks').html(total_marks);
}
function computeTotalMarks() {
val = 0;
$('.pos_marks').each(function() {
//add '#' to the id of checkbox to obtain that element
//'if' condition checks if the box is checked
//if it is, add to val
if(document.getElementById($(this).attr('checkBoxID')).checked) {
console.log($(this).attr('checkBoxID'))
val += parseFloat($(this).val());
}
})
total_marks = val;
}
function handleMarkChange() {
computeTotalMarks();
dispTotalMarks();
}
function handleCheckBoxClick(cb) {
posmarks = parseFloat((document.getElementById(
cb.getAttribute('posMarksID'))).value);
// get value of input field like so: element.value
if(cb.checked) {
total_marks += posmarks;
//update num_questions
num_questions++;
}
else {
total_marks -= posmarks
//update num_questions
num_questions--;
}
dispTotalMarks();
dispNumQuestions();
}
function dispNumQuestions() {
$('#num_questions').html(num_questions);
}
</script>
{%endblock%}
{%block title%}
Create Test
......@@ -18,17 +67,28 @@ Create Test
</div>
<ul class="list-group">
<!-- <ul> -->
<div>
<div style="float:right ; ">
Total Marks : <p id='total_marks'></p>
</div>
<br/> <br/> <br/>
<div style="float:right ; ">
Number of Questions : <p id='num_questions'></p>
</div>
<form method="POST" action="create_Test">
{% csrf_token %}
{{ TestForm.as_p }}
{{TestForm.errors}}
{{TestForm.non_field_errors}}
{%for question in questions%}
<li class="list-group-item">
<!-- <li> -->
<div class="row toggle">
<div class="col-xs-10">
<input type="checkbox" value= "True" name = "{{question.q_id}}" >
<input type="checkbox" value= "True" name = "{{question.q_id}}"
id = "{{question.q_id}}" onclick="handleCheckBoxClick(this)"
posMarksID="{{question.q_id}}pos_marks">
<!-- link to posmarks in checkbox -->
{{question.question_text}} {{question.q_id}} </br>
</div>
</div>
......@@ -48,8 +108,15 @@ Create Test
{%endfor%}
<label> marks
<input type="number" name="{{question.q_id}}" min="0" max="10" placeholder="4" step="0.25" value="4">
<input type="number" name="{{question.q_id}}" min="-10" max="0" placeholder="-1" step="0.25" value="0">
<!-- the id of checkboxes is q_id. I'm linking pos_marks to checkboxes
by id -->
<input class = "pos_marks" type="number" name="{{question.q_id}}"
min="0" max="10" placeholder="4" step="0.25" value="4"
checkBoxID = "{{question.q_id}}" onchange="handleMarkChange()"
id="{{question.q_id}}pos_marks">
<input type="number" name="{{question.q_id}}"
min="-10" max="0" placeholder="-1" step="0.25" value="0">
</label>
</ul>
......@@ -58,6 +125,8 @@ Create Test
{%endfor%}
<button type="submit" class="btn btn-login float-right">Submit</button>
</form>
</div>
</ul>
</div>
</div>
......
......@@ -550,16 +550,7 @@ def create_Test(request):
# print vars(form)
# #debug
if form.is_valid():
test = form.save()
creator = models.Created_BY(test_ID=test,faculty=models.Faculty.objects.filter(username=request.session['username']).get())
creator.save()
else:
if form.errors:
for field in form:
print field.errors
return HttpResponse("Test Form Invalid")
max_marks = 0
for attr in dir(Test):
# print attr
......@@ -581,6 +572,7 @@ def create_Test(request):
containsObj.marks_pos = data[q][1]
containsObj.marks_neg = data[q][2]
max_marks += int(data[q][1])
containsObj.pk = None
containsObj.save()
......@@ -591,6 +583,20 @@ def create_Test(request):
print e
continue
if form.is_valid():
test = form.save(commit=False) #not sure, but if a required attribute (like max_marks)
# is not present this may throw an error.
test.max_marks = max_marks
test.save()
creator = models.Created_BY(test_ID=test,faculty=models.Faculty.objects.filter(username=request.session['username']).get())
creator.save()
else:
if form.errors:
for field in form:
print field.errors
return HttpResponse("Test Form Invalid")
#schedule ranking function when test is finished
print type(test.start_time)
print type(test.expected_time_for_completion)
......
pip install django-background-tasks
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment