Пример #1
0
    def save(self):
        data = self.cleaned_data

        # Create new question instance
        self.instance = Question.objects.create()

        self.instance.category = get_object_or_404(Category,
                                                   name=data['category'])

        if data['text']:
            # Question with normal text
            self.instance.text = data['text']
            for i in xrange(
                    1,
                    IntegerSetting.get(
                        'question_number_of_answers').get_value() + 1):
                a = Answer.objects.create(question=self.instance)
                a.text = data['answer_%d' % i]
                a.correct = data['correct_%d' % i]
                a.active = data['active_%d' % i]
                a.save()
        else:
            # Question with rich text
            self.instance.rich_text = data['rich_text']
            for i in xrange(
                    1,
                    IntegerSetting.get(
                        'question_number_of_answers').get_value() + 1):
                a = Answer.objects.create(question=self.instance)
                a.rich_text = data['rich_answer_%d' % i]
                a.correct = data['rich_correct_%d' % i]
                a.active = data['rich_active_%d' % i]
                a.save()

        self.instance.active = data['active']
        self.instance.answer_type = data['answer_type']

        if self.instance.category.name == 'workshop':
            self.instance.answer_type = 'F'

        # Do tags
        for t in data['tags']:
            tag = Tag.objects.get(name=t)
            self.instance.tags.add(tag)

        # Schedule for qotd
        if self.instance.category.name == 'qotd':
            sched = Schedule.objects.create(question=self.instance)
            sched.day = data['schedule']
            sched.save()

        self.instance.save()
        return self.instance
Пример #2
0
    def clean(self):
        data = self.cleaned_data
        num = IntegerSetting.get('question_number_of_answers').get_value()

        # Check the question text. If the normal text is missing, check for the
        # rich text. If that is missing too, something is wrong. Also, don't
        # allow any empty active answers.
        if 'text' in data and data['text'].strip() == '':
            if 'rich_text' not in data or data['rich_text'].strip() == '':
                raise forms.ValidationError("Invalid question text")
            else:
                for i in xrange(1, num + 1):
                    if data['rich_active_%d' % i] is True and \
                       data['rich_answer_%d' % i].strip() == '':
                        raise forms.ValidationError("Empty active answer")
        else:
            for i in xrange(1, num + 1):
                if data['active_%d' % i] is True and \
                   data['answer_%d' % i].strip() == '':
                    raise forms.ValidationError("Empty active answer")

        # In case of QotD, check for a valid schedule.
        if 'category' in data and data['category'] == 'qotd':
            if 'schedule' not in data or data['schedule'] is None:
                raise forms.ValidationError("Invalid schedule")

        return self.cleaned_data
Пример #3
0
 def get_context_data(self, **kwargs):
     context = super(AddQuestionView, self).get_context_data(**kwargs)
     answers_range = [str(i) for i in range(1, IntegerSetting.get('question_number_of_answers').get_value() + 1)]
     categories = [(c.name.capitalize(), c.name) for c in Category.objects.all()]
     context['categories'] = categories
     context['answers_range'] = answers_range
     return context
Пример #4
0
 def get_context_data(self, **kwargs):
     context = super(AddQuestionView, self).get_context_data(**kwargs)
     answers_range = [str(i) for i in range(1, IntegerSetting.get('question_number_of_answers').get_value() + 1)]
     categories = [(c.name.capitalize(), c.name) for c in Category.objects.all()]
     context['categories'] = categories
     context['answers_range'] = answers_range
     return context
Пример #5
0
    def __init__(self, data=None, instance=None):
        super(AddQuestionForm, self).__init__(data)

        for i in xrange(
                1,
                IntegerSetting.get('question_number_of_answers').get_value() +
                1):
            self.fields['answer_%d' % i] = forms.CharField(
                max_length=500, widget=forms.Textarea, required=False)
            self.fields['correct_%d' % i] = forms.BooleanField(required=False)
            self.fields['active_%d' % i] = forms.BooleanField(required=False)

            self.fields['rich_answer_%d' % i] = forms.CharField(
                required=False, widget=CKEditorWidget())
            self.fields['rich_correct_%d' %
                        i] = forms.BooleanField(required=False)
            self.fields['rich_active_%d' %
                        i] = forms.BooleanField(required=False)

        self.fields['tags'] = MultipleField(required=False)

        self.fields['active'] = forms.BooleanField(
            required=False, initial=instance.active if instance else False)
        self.fields['answer_type'] = forms.ChoiceField(
            choices=(("C", "multiple choice"), ("R", "single choice"),
                     ("F", "free text")))

        self.instance = instance
Пример #6
0
    def clean(self):
        data = self.cleaned_data
        num = IntegerSetting.get('question_number_of_answers').get_value()

        # Check the question text. If the normal text is missing, check for the
        # rich text. If that is missing too, something is wrong. Also, don't
        # allow any empty active answers.
        if 'text' in data and data['text'].strip() == '':
            if 'rich_text' not in data or data['rich_text'].strip() == '':
                raise forms.ValidationError("Invalid question text")
            else:
                for i in xrange(1, num + 1):
                    if data['rich_active_%d' % i] is True and \
                       data['rich_answer_%d' % i].strip() == '':
                            raise forms.ValidationError("Empty active answer")
        else:
            for i in xrange(1, num + 1):
                if data['active_%d' % i] is True and \
                   data['answer_%d' % i].strip() == '':
                        raise forms.ValidationError("Empty active answer")

        # In case of QotD, check for a valid schedule.
        if 'category' in data and data['category'] == 'qotd':
            if 'schedule' not in data or data['schedule'] is None:
                raise forms.ValidationError("Invalid schedule")

        return self.cleaned_data
Пример #7
0
    def save(self):
        data = self.cleaned_data

        # Create new question instance
        self.instance = Question.objects.create()

        self.instance.category = get_object_or_404(Category, name=data['category'])

        if data['text']:
            # Question with normal text
            self.instance.text = data['text']
            for i in xrange(1, IntegerSetting.get('question_number_of_answers').get_value() + 1):
                a = Answer.objects.create(question=self.instance)
                a.text = data['answer_%d' % i]
                a.correct = data['correct_%d' % i]
                a.active = data['active_%d' % i]
                a.save()
        else:
            # Question with rich text
            self.instance.rich_text = data['rich_text']
            for i in xrange(1, IntegerSetting.get('question_number_of_answers').get_value() + 1):
                a = Answer.objects.create(question=self.instance)
                a.rich_text = data['rich_answer_%d' % i]
                a.correct = data['rich_correct_%d' % i]
                a.active = data['rich_active_%d' % i]
                a.save()

        self.instance.active = data['active']
        self.instance.answer_type = data['answer_type']

        if self.instance.category.name == 'workshop':
            self.instance.answer_type = 'F'

        # Do tags
        for t in data['tags']:
            tag = Tag.objects.get(name=t)
            self.instance.tags.add(tag)

        # Schedule for qotd
        if self.instance.category.name == 'qotd':
            sched = Schedule.objects.create(question=self.instance)
            sched.day = data['schedule']
            sched.save()

        self.instance.save()
        return self.instance
Пример #8
0
    def start(cls):
        """
         Create challenges for each consecutive players. Return a list of created challenges.
        """
        cls.create_users()
        challenges = []
        round = 1
        last = None
        for user in cls.base_query():
            u = user.user.get_profile()
            if last is None:
                last = u
            else:
                c = GrandChallenge.create(u, last, round)
                challenges.append(c)
                last = None

        setting_round = IntegerSetting.get('gc_round')
        setting_round.set_value(round)
        return challenges
Пример #9
0
    def start(cls):
        """
         Create challenges for each consecutive players. Return a list of created challenges.
        """
        cls.create_users()
        challenges = []
        round = 1
        last = None
        for user in cls.base_query():
            u = user.user.get_profile()
            if last is None:
                last = u
            else:
                c = GrandChallenge.create(u, last, round)
                challenges.append(c)
                last = None

        setting_round = IntegerSetting.get('gc_round')
        setting_round.set_value(round)
        return challenges
Пример #10
0
    def __init__(self, data=None, instance=None):
        super(AddQuestionForm, self).__init__(data)

        for i in xrange(1, IntegerSetting.get('question_number_of_answers').get_value() + 1):
            self.fields['answer_%d' % i] = forms.CharField(max_length=500,
                                                           widget=forms.Textarea, required=False)
            self.fields['correct_%d' % i] = forms.BooleanField(required=False)
            self.fields['active_%d' % i] = forms.BooleanField(required=False)

            self.fields['rich_answer_%d' % i] = forms.CharField(required=False, widget=CKEditorWidget())
            self.fields['rich_correct_%d' % i] = forms.BooleanField(required=False)
            self.fields['rich_active_%d' % i] = forms.BooleanField(required=False)

        self.fields['tags'] = MultipleField(required=False)

        self.fields['active'] = forms.BooleanField(required=False, initial=instance.active if instance else False)
        self.fields['answer_type'] = forms.ChoiceField(
            choices=(("C", "multiple choice"), ("R", "single choice"), ("F", "free text")))

        self.instance = instance
Пример #11
0
def add_question(request):
    context = {}
    num = IntegerSetting.get('question_number_of_answers').get_value()

    if request.method == 'POST':
        form = AddQuestionForm(request.POST)
        if form.is_valid():
            new_question = form.save()
            return redirect('qpool_home', cat=new_question.category.name)
    else:
        form = AddQuestionForm()

    answers_range = [str(i) for i in xrange(1, num + 1)]
    categories = [(c.name.capitalize(), c.name) for c in Category.objects.all()]
    context['categories'] = categories
    context['answers_range'] = answers_range
    context['form'] = form

    return render_to_response('cpanel/add_question.html',
                              context,
                              context_instance=RequestContext(request))
Пример #12
0
 def is_started(cls):
     setting_round = IntegerSetting.get('gc_round')
     return setting_round.get_value() > 0
Пример #13
0
 def set_current_round(cls, number):
     setting_round = IntegerSetting.get('gc_round')
     setting_round.set_value(number)
Пример #14
0
 def get_current_round(cls):
     setting_round = IntegerSetting.get('gc_round')
     round = setting_round.get_value()
     if round == 0:
         return None
     return cls.get_round(round)
Пример #15
0
 def get_current_round(cls):
     setting_round = IntegerSetting.get('gc_round')
     round = setting_round.get_value()
     if round == 0:
         return None
     return cls.get_round(round)
Пример #16
0
 def set_current_round(cls, number):
     setting_round = IntegerSetting.get('gc_round')
     setting_round.set_value(number)
Пример #17
0
 def is_started(cls):
     setting_round = IntegerSetting.get('gc_round')
     return setting_round.get_value() > 0