From: Jan Szejko Date: Tue, 16 Jan 2018 11:14:13 +0000 (+0100) Subject: fix field counts X-Git-Url: https://git.mdrn.pl/edumed.git/commitdiff_plain/38be7e54873c139e95ac51842c77d6d145710b46 fix field counts --- diff --git a/stage2/models.py b/stage2/models.py index a5815fd..4a5b103 100644 --- a/stage2/models.py +++ b/stage2/models.py @@ -93,6 +93,15 @@ class Assignment(models.Model): answers = answers.filter(need_arbiter=True) return answers + def field_counts(self, answers): + for field_desc in self.field_descriptions: + field_name, params = field_desc + if params['type'] == 'options': + field_count = FieldOption.objects.filter(answer__in=list(answers), set__name=params['option_set']).count() + else: # text, link + field_count = sum(1 for answer in answers if answer.field_values.get(field_name)) + yield field_name, field_count + def is_active(self): return self.deadline >= timezone.now() @@ -118,7 +127,7 @@ class Answer(models.Model): value = self.field_values.get(field_name, '') if params['type'] == 'link': value = format_html(u'{url}', url=value) - yield (field_name, value) + yield field_name, value def update_complete(self): marks = self.mark_set.all() diff --git a/stage2/views.py b/stage2/views.py index 22eeda4..825be9e 100644 --- a/stage2/views.py +++ b/stage2/views.py @@ -156,6 +156,7 @@ def answer_list(request, assignment_id): return render(request, 'stage2/answer_list.html', { 'answers': answers, 'assignment': assignment, + 'field_counts': assignment.field_counts(answers) })