# Path: survey/compat.py
# def is_authenticated(request):
# 
# Path: survey/forms.py
# class QuestionForm(forms.ModelForm):
# 
#     class Meta:
#         model = get_question_model()
#         fields = ('path', 'title', 'text', 'default_metric', 'extra')
# 
#     def clean_choices(self):
#         self.cleaned_data['choices'] = self.cleaned_data['choices'].strip()
#         return self.cleaned_data['choices']
# 
# Path: survey/models.py
# class Choice(models.Model):
#     """
#     Choice for a multiple choice question.
#     """
#     unit = models.ForeignKey(Unit, on_delete=models.PROTECT)
#     rank = models.IntegerField(
#         help_text=_("used to order choice when presenting a question"))
#     text = models.TextField()
#     descr = models.TextField()
# 
#     class Meta:
#         unique_together = ('unit', 'rank')
# 
#     def __str__(self):
#         return str(self.text)
# 
# class EnumeratedQuestions(models.Model):
# 
#     campaign = models.ForeignKey(Campaign, on_delete=models.CASCADE)
#     question = models.ForeignKey(settings.QUESTION_MODEL,
#         on_delete=models.CASCADE)
#     rank = models.IntegerField(
#         help_text=_("used to order questions when presenting a campaign."))
#     required = models.BooleanField(default=True,
#         help_text=_("If checked, an answer is required"))
# 
#     class Meta:
#         unique_together = ('campaign', 'rank')
# 
#     def __str__(self):
#         return str(self.question.path)
# 
# class Metric(models.Model):
#     """
#     Metric on a ``Question``.
#     """
#     slug = models.SlugField(max_length=150, unique=True, db_index=True)
#     title = models.CharField(max_length=150)
#     unit = models.ForeignKey(Unit, on_delete=models.PROTECT)
# 
#     def __str__(self):
#         return str(self.slug)
# 
# class Unit(models.Model):
#     """
#     Unit in which an ``Answer.measured`` value is collected.
#     """
# 
#     SYSTEM_STANDARD = 0
#     SYSTEM_IMPERIAL = 1
#     SYSTEM_RANK = 2
#     SYSTEM_ENUMERATED = 3
#     SYSTEM_FREETEXT = 4
# 
#     SYSTEMS = [
#             (SYSTEM_STANDARD, 'standard'),
#             (SYSTEM_IMPERIAL, 'imperial'),
#             (SYSTEM_RANK, 'rank'),
#             (SYSTEM_ENUMERATED, 'enum'),
#             (SYSTEM_FREETEXT, 'freetext'),
#         ]
# 
#     NUMERICAL_SYSTEMS = [
#         SYSTEM_STANDARD,
#         SYSTEM_IMPERIAL,
#         SYSTEM_RANK
#     ]
# 
#     slug = models.SlugField(max_length=150, unique=True, db_index=True)
#     title = models.CharField(max_length=150)
#     system = models.PositiveSmallIntegerField(
#         choices=SYSTEMS, default=SYSTEM_STANDARD)
# 
#     def __str__(self):
#         return str(self.slug)
# 
# Path: survey/mixins.py
# class CampaignQuestionMixin(CampaignMixin):
# 
#     num_url_kwarg = 'num'
#     model = EnumeratedQuestions
# 
#     def get_queryset(self):
#         return self.model.objects.filter(
#             campaign=self.campaign).order_by('rank')
# 
#     def get_object(self, queryset=None):
#         """
#         Returns a question object based on the URL.
#         """
#         if not queryset:
#             queryset = self.get_queryset()
#         return get_object_or_404(queryset,
#             rank=self.kwargs.get(self.num_url_kwarg, 1))


from django.db import transaction
from django.db.models import Max
from django.template.defaultfilters import slugify
from django.views.generic import (CreateView, DeleteView, ListView,
    RedirectView, UpdateView)
from ..compat import csrf, reverse
from ..forms import QuestionForm
from ..models import Choice, EnumeratedQuestions, Metric, Unit
from ..mixins import CampaignQuestionMixin
                    if correct_answer and choice.text in correct_answer:
                        form.instance.correct_answer = choice
            result = super(QuestionFormMixin, self).form_valid(form)
            last_rank = EnumeratedQuestions.objects.filter(
                campaign=self.campaign).aggregate(Max('rank')).get(
                    'rank__max', 0)
            _ = EnumeratedQuestions.objects.get_or_create(
                question=self.object,
                campaign=self.campaign,
                defaults={'rank': last_rank + 1})
        return result

    def get_success_url(self):
        return reverse(self.success_url, args=(self.campaign,))


class QuestionCreateView(QuestionFormMixin, CreateView):
    """
    Create a new question within a campaign.
    """
    template_name = 'survey/question_form.html'


class QuestionDeleteView(CampaignQuestionMixin, DeleteView):
    """
    Delete a question.
    """
    success_url = 'survey_question_list'

    def get_success_url(self):

# Path: survey/compat.py
# def is_authenticated(request):
# 
# Path: survey/forms.py
# class QuestionForm(forms.ModelForm):
# 
#     class Meta:
#         model = get_question_model()
#         fields = ('path', 'title', 'text', 'default_metric', 'extra')
# 
#     def clean_choices(self):
#         self.cleaned_data['choices'] = self.cleaned_data['choices'].strip()
#         return self.cleaned_data['choices']
# 
# Path: survey/models.py
# class Choice(models.Model):
#     """
#     Choice for a multiple choice question.
#     """
#     unit = models.ForeignKey(Unit, on_delete=models.PROTECT)
#     rank = models.IntegerField(
#         help_text=_("used to order choice when presenting a question"))
#     text = models.TextField()
#     descr = models.TextField()
# 
#     class Meta:
#         unique_together = ('unit', 'rank')
# 
#     def __str__(self):
#         return str(self.text)
# 
# class EnumeratedQuestions(models.Model):
# 
#     campaign = models.ForeignKey(Campaign, on_delete=models.CASCADE)
#     question = models.ForeignKey(settings.QUESTION_MODEL,
#         on_delete=models.CASCADE)
#     rank = models.IntegerField(
#         help_text=_("used to order questions when presenting a campaign."))
#     required = models.BooleanField(default=True,
#         help_text=_("If checked, an answer is required"))
# 
#     class Meta:
#         unique_together = ('campaign', 'rank')
# 
#     def __str__(self):
#         return str(self.question.path)
# 
# class Metric(models.Model):
#     """
#     Metric on a ``Question``.
#     """
#     slug = models.SlugField(max_length=150, unique=True, db_index=True)
#     title = models.CharField(max_length=150)
#     unit = models.ForeignKey(Unit, on_delete=models.PROTECT)
# 
#     def __str__(self):
#         return str(self.slug)
# 
# class Unit(models.Model):
#     """
#     Unit in which an ``Answer.measured`` value is collected.
#     """
# 
#     SYSTEM_STANDARD = 0
#     SYSTEM_IMPERIAL = 1
#     SYSTEM_RANK = 2
#     SYSTEM_ENUMERATED = 3
#     SYSTEM_FREETEXT = 4
# 
#     SYSTEMS = [
#             (SYSTEM_STANDARD, 'standard'),
#             (SYSTEM_IMPERIAL, 'imperial'),
#             (SYSTEM_RANK, 'rank'),
#             (SYSTEM_ENUMERATED, 'enum'),
#             (SYSTEM_FREETEXT, 'freetext'),
#         ]
# 
#     NUMERICAL_SYSTEMS = [
#         SYSTEM_STANDARD,
#         SYSTEM_IMPERIAL,
#         SYSTEM_RANK
#     ]
# 
#     slug = models.SlugField(max_length=150, unique=True, db_index=True)
#     title = models.CharField(max_length=150)
#     system = models.PositiveSmallIntegerField(
#         choices=SYSTEMS, default=SYSTEM_STANDARD)
# 
#     def __str__(self):
#         return str(self.slug)
# 
# Path: survey/mixins.py
# class CampaignQuestionMixin(CampaignMixin):
# 
#     num_url_kwarg = 'num'
#     model = EnumeratedQuestions
# 
#     def get_queryset(self):
#         return self.model.objects.filter(
#             campaign=self.campaign).order_by('rank')
# 
#     def get_object(self, queryset=None):
#         """
#         Returns a question object based on the URL.
#         """
#         if not queryset:
#             queryset = self.get_queryset()
#         return get_object_or_404(queryset,
#             rank=self.kwargs.get(self.num_url_kwarg, 1))


# Path: survey/views/createquestion.py
from django.db import transaction
from django.db.models import Max
from django.template.defaultfilters import slugify
from django.views.generic import (CreateView, DeleteView, ListView,
    RedirectView, UpdateView)
from ..compat import csrf, reverse
from ..forms import QuestionForm
from ..models import Choice, EnumeratedQuestions, Metric, Unit
from ..mixins import CampaignQuestionMixin

                    if correct_answer and choice.text in correct_answer:
                        form.instance.correct_answer = choice
            result = super(QuestionFormMixin, self).form_valid(form)
            last_rank = EnumeratedQuestions.objects.filter(
                campaign=self.campaign).aggregate(Max('rank')).get(
                    'rank__max', 0)
            _ = EnumeratedQuestions.objects.get_or_create(
                question=self.object,
                campaign=self.campaign,
                defaults={'rank': last_rank + 1})
        return result

    def get_success_url(self):
        return reverse(self.success_url, args=(self.campaign,))


class QuestionCreateView(QuestionFormMixin, CreateView):
    """
    Create a new question within a campaign.
    """
    template_name = 'survey/question_form.html'


class QuestionDeleteView(CampaignQuestionMixin, DeleteView):
    """
    Delete a question.
    """
    success_url = 'survey_question_list'

    def get_success_url(self):

        return reverse(self.success_url, args=(self.campaign,))