Merge remote-tracking branch 'origin/master'

This commit is contained in:
tkupek 2016-02-21 17:55:06 +01:00
commit 789d042f95
3 changed files with 33 additions and 12 deletions

View file

@ -16,7 +16,7 @@ class Setting(models.Model):
logo = models.CharField(max_length=256, null=False)
active = models.BooleanField(unique=True, default=False)
def __unicode__(self):
def __str__(self):
return self.title
@ -26,7 +26,7 @@ class Question(models.Model):
text = models.TextField(null=True)
explanation = models.TextField(null=True)
def __unicode__(self):
def __str__(self):
return self.title
@ -35,7 +35,7 @@ class Option(models.Model):
correct = models.BooleanField(null=False, default=False)
question = models.ForeignKey(Question, on_delete=models.CASCADE, null=False)
def __unicode__(self):
def __str__(self):
return self.text
@ -49,15 +49,16 @@ class User(models.Model):
name = models.CharField(max_length=100, null=False)
last_seen = models.DateTimeField(null=True)
def __unicode__(self):
def __str__(self):
return self.name
class UserAnswer(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, null=False)
question = models.ForeignKey(Question, on_delete=models.CASCADE, null=False)
correct = models.NullBooleanField(null=True)
def __unicode__(self):
def __str__(self):
return str(self.user) + " - " + str(self.question)
class Meta:

View file

@ -42,11 +42,15 @@
<tr class="table_headline">
<td>title</td>
<td>answers</td>
<td>correct</td>
<td>correct percentage</td>
</tr>
{% for question in questions %}
<tr>
<td>{{ question.title }}</td>
<td>{{ question.answers }}</td>
<td>{{ question.correct_answers }}</td>
<td>{{ question.correct_answers_percentage }}</td>
</tr>
{% endfor %}
</table>

View file

@ -60,12 +60,33 @@ def get_answer(request):
user = User.objects.get(token=request_token)
log_last_seen(user)
correct_options = Option.objects.filter(question=question.id, correct=True)
correct_option_checklist = []
for option in correct_options:
correct_option_checklist.append(option.id)
options_id = []
for option in correct_options:
options_id.append(option.id)
options_id = json.dumps(options_id)
user_answer = get_user_answer(question, user)
if user_answer is None:
correct = False
user_answer = UserAnswer()
user_answer.question = question
user_answer.user = user
for option in request_answers:
if option in correct_option_checklist:
correct_option_checklist.remove(option)
if len(correct_option_checklist) is 0:
correct = True
user_answer.correct = correct
user_answer.save()
for option in request_answers:
@ -74,13 +95,6 @@ def get_answer(request):
user_answer_options.option = Option.objects.get(id=option)
user_answer_options.save()
options = Option.objects.filter(question=question.id, correct=True)
options_id = []
for option in options:
options_id.append(option.id)
options_id = json.dumps(options_id)
return HttpResponse(options_id)
@ -109,5 +123,7 @@ def statistic(request):
questions = Question.objects.all()
for question in questions:
question.answers = UserAnswer.objects.filter(question=question.id).count()
question.correct_answers = UserAnswer.objects.filter(question=question.id, correct=True).count()
question.correct_answers_percentage = question.correct_answers / question.answers
return render_to_response('statistic.html', {'settings': settings, 'users': users, 'questions': questions})