# 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
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.




class QuestionFormMixin(CampaignQuestionMixin):

    form_class = QuestionForm
    success_url = 'survey_question_list'

    def get_object(self, queryset=None):
        """
        Returns a question object based on the URL.
        """
        return super(QuestionFormMixin, self).get_object(
            queryset=queryset).question

    def form_valid(self, form):
        with transaction.atomic():
            if form.cleaned_data['question_type'] == 'radio':
                enum_slug = slugify(form.cleaned_data['text'])
                unit = Unit.objects.create(
                    slug=enum_slug,

# 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

# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.




class QuestionFormMixin(CampaignQuestionMixin):

    form_class = QuestionForm
    success_url = 'survey_question_list'

    def get_object(self, queryset=None):
        """
        Returns a question object based on the URL.
        """
        return super(QuestionFormMixin, self).get_object(
            queryset=queryset).question

    def form_valid(self, form):
        with transaction.atomic():
            if form.cleaned_data['question_type'] == 'radio':
                enum_slug = slugify(form.cleaned_data['text'])
                unit = Unit.objects.create(
                    slug=enum_slug,

                    system=Unit.SYSTEM_ENUMERATED)