turn on banner for 1%
[edumed.git] / stage2 / models.py
index fb50152..4a5b103 100644 (file)
@@ -8,6 +8,7 @@ from django.core.urlresolvers import reverse
 from django.db import models
 from django.db.models.signals import post_save
 from django.dispatch import receiver
+from django.utils.html import format_html
 from django.utils.translation import ugettext_lazy as _
 from django.utils import timezone
 from jsonfield import JSONField
@@ -92,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()
 
@@ -110,11 +120,14 @@ class Answer(models.Model):
     def fields(self):
         for field_desc in self.assignment.field_descriptions:
             field_name, params = field_desc
-            if params['type'] == 'text':
-                yield (field_name, self.field_values.get(field_name, ''))
-            else:  # options
+            if params['type'] == 'options':
                 option = self.fieldoption_set.filter(set__name=params['option_set'])
-                yield (field_name, option.get().value if option else '--------')
+                value = option.get().value if option else '--------'
+            else:  # text, link
+                value = self.field_values.get(field_name, '')
+            if params['type'] == 'link':
+                value = format_html(u'<a href="{url}">{url}</a>', url=value)
+            yield field_name, value
 
     def update_complete(self):
         marks = self.mark_set.all()