# Path: cms_saq/models.py
# class Submission(models.Model):
#     question = models.SlugField()
#     answer = models.TextField(blank=True)
#     score = models.IntegerField()
#     user = models.ForeignKey('auth.User', related_name='saq_submissions')
# 
#     submission_set = models.ForeignKey(
#         SubmissionSet, related_name='submissions', null=True)
# 
#     class Meta:
#         ordering = ('submission_set', 'user', 'question')
#         unique_together = ('question', 'user', 'submission_set')
# 
#     def answer_list(self):
#         return self.answer.split(",")
# 
#     def __unicode__(self):
#         return u"%s answer to %s (%s)" % (
#             self.user, self.question, self.submission_set.slug
#             if self.submission_set else "default")
# 
# class Question(CMSPlugin):
#     QUESTION_TYPES = [
#         ('S', 'Single-choice question'),
#         ('M', 'Multi-choice question'),
#         ('F', 'Free-text question'),
#     ]
# 
#     slug = models.SlugField(
#         help_text="A slug for identifying answers to this specific question "
#         "(allows multiple only for multiple languages)")
#     tags = TaggableManager(blank=True)
#     label = models.CharField(max_length=512, blank=True)
#     help_text = models.CharField(max_length=512, blank=True)
#     question_type = models.CharField(max_length=1, choices=QUESTION_TYPES)
#     optional = models.BooleanField(
#         default=False,
#         help_text="Only applies to free text questions",
#     )
# 
#     depends_on_answer = models.ForeignKey(
#         Answer, null=True, blank=True, related_name='trigger_questions')
# 
#     def copy_relations(self, oldinstance):
#         for answer in oldinstance.answers.all():
#             answer.pk = None
#             answer.question = self
#             answer.save()
# 
#         self.depends_on_answer = oldinstance.depends_on_answer
# 
#     @staticmethod
#     def all_in_tree(page):
#         root = page.get_root()
#         # Remember that there might be questions on the root page as well!
#         tree = root.get_descendants() | Page.objects.filter(id=root.id)
#         placeholders = Placeholder.objects.filter(page__in=tree)
#         return Question.objects.filter(placeholder__in=placeholders)
# 
#     @staticmethod
#     def all_in_page(page):
#         placeholders = Placeholder.objects.filter(page=page)
#         return Question.objects.filter(placeholder__in=placeholders)
# 
#     def score(self, answers):
#         if self.question_type == 'F':
#             return 0
#         elif self.question_type == 'S':
#             return self.answers.get(slug=answers).score
#         elif self.question_type == 'M':
#             answers_list = answers.split(',')
#             return sum([self.answers.get(slug=a).score for a in answers_list])
# 
#     @property
#     def max_score(self):
#         if not hasattr(self, '_max_score'):
#             if self.question_type == "S":
#                 self._max_score = self.answers.aggregate(
#                     Max('score'))['score__max']
#             elif self.question_type == "M":
#                 self._max_score = self.answers.aggregate(
#                     Sum('score'))['score__sum']
#             else:
#                 self._max_score = None  # don't score free-text answers
#         return self._max_score
# 
#     def percent_score_for_user(self, user):
#         if self.max_score:
#             try:
#                 score = Submission.objects.get(
#                     question=self.slug,
#                     user=user,
#                 ).score
#             except Submission.DoesNotExist:
#                 return 0
#             return 100.0 * score / self.max_score
#         else:
#             return None
# 
#     def __unicode__(self):
#         return self.slug


import json
from django.test import TestCase
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test.client import Client, RequestFactory
from django.template import Template, RequestContext
from cms_saq.models import Submission, Question
        self.assertEqual(out, '')

    def test_saq_nice_answer(self):
        request = self._request_for_user('auntie_rach')
        # test for an existing answer
        template = Template("{% load saq_tags %}{% saq_nice_answer \"favourite-sport\" %}")
        out = template.render(RequestContext(request))
        self.assertEqual(out, 'Cricket')
        # test for a non-existent answer
        template = Template("{% load saq_tags %}{% saq_nice_answer \"favourite-ice-cream\" %}")
        out = template.render(RequestContext(request))
        self.assertEqual(out, '')

    def test_saq_percent_score(self):
        request = self._request_for_user('uncle_bill')
        template = Template("{% load saq_tags %}{% saq_percent_score \"favourite-colour\" %}")
        out = template.render(RequestContext(request))
        self.assertEqual(out, '33')
        request = self._request_for_user('auntie_rach')
        template = Template("{% load saq_tags %}{% saq_percent_score \"favourite-colour\" %}")
        out = template.render(RequestContext(request))
        self.assertEqual(out, '100')
        template = Template("{% load saq_tags %}{% saq_percent_score \"favourite-ice-cream\" %}")
        out = template.render(RequestContext(request))
        self.assertEqual(out, '0')

    def test_saq_aggregate_percent_score_by_tags(self):
        # setup tags, 'cos fixtures suck for tags
        Question.objects.get(slug='favourite-colour').tags.add('favourites')
        Question.objects.get(slug='favourite-sport').tags.add('favourites', 'sports')

# Path: cms_saq/models.py
# class Submission(models.Model):
#     question = models.SlugField()
#     answer = models.TextField(blank=True)
#     score = models.IntegerField()
#     user = models.ForeignKey('auth.User', related_name='saq_submissions')
# 
#     submission_set = models.ForeignKey(
#         SubmissionSet, related_name='submissions', null=True)
# 
#     class Meta:
#         ordering = ('submission_set', 'user', 'question')
#         unique_together = ('question', 'user', 'submission_set')
# 
#     def answer_list(self):
#         return self.answer.split(",")
# 
#     def __unicode__(self):
#         return u"%s answer to %s (%s)" % (
#             self.user, self.question, self.submission_set.slug
#             if self.submission_set else "default")
# 
# class Question(CMSPlugin):
#     QUESTION_TYPES = [
#         ('S', 'Single-choice question'),
#         ('M', 'Multi-choice question'),
#         ('F', 'Free-text question'),
#     ]
# 
#     slug = models.SlugField(
#         help_text="A slug for identifying answers to this specific question "
#         "(allows multiple only for multiple languages)")
#     tags = TaggableManager(blank=True)
#     label = models.CharField(max_length=512, blank=True)
#     help_text = models.CharField(max_length=512, blank=True)
#     question_type = models.CharField(max_length=1, choices=QUESTION_TYPES)
#     optional = models.BooleanField(
#         default=False,
#         help_text="Only applies to free text questions",
#     )
# 
#     depends_on_answer = models.ForeignKey(
#         Answer, null=True, blank=True, related_name='trigger_questions')
# 
#     def copy_relations(self, oldinstance):
#         for answer in oldinstance.answers.all():
#             answer.pk = None
#             answer.question = self
#             answer.save()
# 
#         self.depends_on_answer = oldinstance.depends_on_answer
# 
#     @staticmethod
#     def all_in_tree(page):
#         root = page.get_root()
#         # Remember that there might be questions on the root page as well!
#         tree = root.get_descendants() | Page.objects.filter(id=root.id)
#         placeholders = Placeholder.objects.filter(page__in=tree)
#         return Question.objects.filter(placeholder__in=placeholders)
# 
#     @staticmethod
#     def all_in_page(page):
#         placeholders = Placeholder.objects.filter(page=page)
#         return Question.objects.filter(placeholder__in=placeholders)
# 
#     def score(self, answers):
#         if self.question_type == 'F':
#             return 0
#         elif self.question_type == 'S':
#             return self.answers.get(slug=answers).score
#         elif self.question_type == 'M':
#             answers_list = answers.split(',')
#             return sum([self.answers.get(slug=a).score for a in answers_list])
# 
#     @property
#     def max_score(self):
#         if not hasattr(self, '_max_score'):
#             if self.question_type == "S":
#                 self._max_score = self.answers.aggregate(
#                     Max('score'))['score__max']
#             elif self.question_type == "M":
#                 self._max_score = self.answers.aggregate(
#                     Sum('score'))['score__sum']
#             else:
#                 self._max_score = None  # don't score free-text answers
#         return self._max_score
# 
#     def percent_score_for_user(self, user):
#         if self.max_score:
#             try:
#                 score = Submission.objects.get(
#                     question=self.slug,
#                     user=user,
#                 ).score
#             except Submission.DoesNotExist:
#                 return 0
#             return 100.0 * score / self.max_score
#         else:
#             return None
# 
#     def __unicode__(self):
#         return self.slug


# Path: cms_saq/tests.py
import json
from django.test import TestCase
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.test.client import Client, RequestFactory
from django.template import Template, RequestContext
from cms_saq.models import Submission, Question

        self.assertEqual(out, '')

    def test_saq_nice_answer(self):
        request = self._request_for_user('auntie_rach')
        # test for an existing answer
        template = Template("{% load saq_tags %}{% saq_nice_answer \"favourite-sport\" %}")
        out = template.render(RequestContext(request))
        self.assertEqual(out, 'Cricket')
        # test for a non-existent answer
        template = Template("{% load saq_tags %}{% saq_nice_answer \"favourite-ice-cream\" %}")
        out = template.render(RequestContext(request))
        self.assertEqual(out, '')

    def test_saq_percent_score(self):
        request = self._request_for_user('uncle_bill')
        template = Template("{% load saq_tags %}{% saq_percent_score \"favourite-colour\" %}")
        out = template.render(RequestContext(request))
        self.assertEqual(out, '33')
        request = self._request_for_user('auntie_rach')
        template = Template("{% load saq_tags %}{% saq_percent_score \"favourite-colour\" %}")
        out = template.render(RequestContext(request))
        self.assertEqual(out, '100')
        template = Template("{% load saq_tags %}{% saq_percent_score \"favourite-ice-cream\" %}")
        out = template.render(RequestContext(request))
        self.assertEqual(out, '0')

    def test_saq_aggregate_percent_score_by_tags(self):
        # setup tags, 'cos fixtures suck for tags
        Question.objects.get(slug='favourite-colour').tags.add('favourites')
        Question.objects.get(slug='favourite-sport').tags.add('favourites', 'sports')

        Question.objects.get(slug='sports-you-play').tags.add('sports')