Esempio n. 1
0
def _make_question_for_today(user, text):
    category = Category(name='qotd')
    question = Question(text=text,
                        proposed_by=user,
                        category=category,
                        active=1)
    question.save()
    sched = Schedule(question=question)
    sched.save()
Esempio n. 2
0
def _make_question_for_today(user, text):
    category = Category(name='qotd')
    question = Question(text=text, proposed_by=user, category=category, active=1)
    question.save()
    for i in range(4):
        Answer.objects.create(question=question, correct=i==1, text='a %d'%i)
    sched = Schedule(question=question)
    sched.save()
    return question
Esempio n. 3
0
def _make_question_for_today(user, text):
    category = Category(name='qotd')
    question = Question(text=text, proposed_by=user, category=category, active=1)
    question.save()
    for i in range(4):
        Answer.objects.create(question=question, correct=i == 1, text='a %d' % i)
    sched = Schedule(question=question)
    sched.save()
    return question
Esempio n. 4
0
def add(question, answers, category=None, tags=[]):
    ''' question is a dict with the following keys: text, endorsed_by, answer_type
    [, proposed_by, active, type, code]
    answers is a list of dicts with the following keys: text, correct [, explanation]
    '''

    # imports valid only after init()
    from wouso.core.qpool.models import Question, Answer

    #print question
    #for a in answers:
    #    print a

    # create and save question
    q = Question(**question)
    q.save()

    if len(tags) > 0:
        for tag in tags:
            q.tags.add(tag)
        q.save()

    if category is not None:
        q.category = category
        q.save()

    # create and save answers for question
    for answer in answers:
        a = Answer(question=q, **answer)
        a.save()

    return q
Esempio n. 5
0
 def _get_foo_question(self, correct=2):
     """ Return a Question """
     class Question: pass
     class Answer: pass
     q = Question()
     q.text = 'How many'
     q.answers = []
     for i in range(4):
         a = Answer()
         a.id, a.text, a.correct = i, str(i), True if i == correct else False
         q.answers.append(a)
     return q
Esempio n. 6
0
 def _get_foo_question(self, correct=2):
     """ Return a Question """
     class Question: pass
     class Answer: pass
     q = Question()
     q.text = 'How many'
     q.answers = []
     for i in range(4):
         a = Answer()
         a.id, a.text, a.correct = i, str(i), True if i == correct else False
         q.answers.append(a)
     return q
Esempio n. 7
0
def add(question, answers, category=None, tags=[]):
    ''' question is a dict with the following keys: text, endorsed_by, answer_type
    [, proposed_by, active, type, code]
    answers is a list of dicts with the following keys: text, correct [, explanation]
    '''

    # imports valid only after init()
    from wouso.core.qpool.models import Question, Answer

    #print question
    #for a in answers:
    #    print a

    # create and save question
    q = Question(**question)
    q.save()

    if len(tags) > 0:
        for tag in tags:
            q.tags.add(tag)
        q.save()

    if category is not None:
        q.category = category
        q.save()

    # create and save answers for question
    for answer in answers:
        a = Answer(question=q, **answer)
        a.save()

    return q
Esempio n. 8
0
def propose(request):

    MAX_ANSWERS = 2

    if request.method == 'POST':
        form = QuestionForm(nr_ans=MAX_ANSWERS, data=request.POST)
        if form.is_valid():
            # create and save the question
            qdict = {}
            qdict['text'] = form.cleaned_data['text']
            qdict['answer_type'] = form.cleaned_data['answer_type']
            qdict['proposed_by'] = request.user
            #qdict['category'] = Category.objects.filter(name='proposed')[0]
            qdict['category'], created  = Category.objects.get_or_create(name='proposed-'+form.cleaned_data['category'])
            q = Question(**qdict)
            q.save()

            #tag = Tag.objects.filter(name=form.cleaned_data['category'])[0]
            tag, created = Tag.objects.get_or_create(name=q.category)
            tag_prefix = q.category
            q.tags.add(tag)

            # add the tags
            for tag_name in form.cleaned_data['tags']:
                #tag = Tag.objects.filter(name=tag_name)[0]
                tag, created = Tag.objects.get_or_create(name=tag_prefix+tag_name)
                q.tags.add(tag)
            q.save()

            # add the answers
            for i in range(form.nr_ans):
                ansdict = {}
                if not form.cleaned_data['answer_%d' % i]:
                    continue
                ansdict['text'] = form.cleaned_data['answer_%d' % i]
                ansdict['correct'] = form.cleaned_data['correct_%d' % i]
                ans = Answer(question=q, **ansdict)
                ans.save()

            return render_to_response('qproposal/thanks.html',
                                      context_instance=RequestContext(request))
    else:
        form = QuestionForm(MAX_ANSWERS)
    return render_to_response('qproposal/propose.html',
                              {'form': form},
                              context_instance=RequestContext(request))
Esempio n. 9
0
def add(question, answers, category=None, tags=None, file_tags=None):
    ''' question is a dict with the following keys: text, endorsed_by, answer_type
    [, proposed_by, active, type, code]
    answers is a list of dicts with the following keys: text, correct [, explanation]
    '''

    # imports valid only after init()
    from wouso.core.qpool.models import Question, Answer, Tag, Category

    #print question
    #for a in answers:
    #    print a

    # create and save question
    q = Question(**question)
    q.save()

    if tags:
        for tag in tags:
            q.tags.add(tag)
        q.save()

    if file_tags:
        for tag_name in file_tags:
            tag, created = Tag.objects.get_or_create(name=tag_name)
            if created:
                tag.save()
                if category is not None:
                    category.tag_set.add(tag)
                    category.save()
            q.tags.add(tag)
        q.save()

    if category is not None:
        q.category = category
        q.save()

    # create and save answers for question
    for answer in answers:
        a = Answer(question=q, **answer)
        a.save()

    return q
Esempio n. 10
0
def propose(request):

    MAX_ANSWERS = 2

    if request.method == 'POST':
        form = QuestionForm(nr_ans=MAX_ANSWERS, data=request.POST)
        if form.is_valid():
            # create and save the question
            qdict = {}
            qdict['text'] = form.cleaned_data['text']
            qdict['answer_type'] = form.cleaned_data['answer_type']
            qdict['proposed_by'] = request.user
            #qdict['category'] = Category.objects.filter(name='proposed')[0]
            qdict['category'], created = Category.objects.get_or_create(
                name='proposed-' + form.cleaned_data['category'])
            q = Question(**qdict)
            q.save()

            #tag = Tag.objects.filter(name=form.cleaned_data['category'])[0]
            tag, created = Tag.objects.get_or_create(name=q.category)
            tag_prefix = q.category
            q.tags.add(tag)

            # add the tags
            for tag_name in form.cleaned_data['tags']:
                #tag = Tag.objects.filter(name=tag_name)[0]
                tag, created = Tag.objects.get_or_create(name=tag_prefix +
                                                         tag_name)
                q.tags.add(tag)
            q.save()

            # add the answers
            for i in range(form.nr_ans):
                ansdict = {}
                if not form.cleaned_data['answer_%d' % i]:
                    continue
                ansdict['text'] = form.cleaned_data['answer_%d' % i]
                ansdict['correct'] = form.cleaned_data['correct_%d' % i]
                ans = Answer(question=q, **ansdict)
                ans.save()

            return render_to_response('qproposal/thanks.html',
                                      context_instance=RequestContext(request))
    else:
        form = QuestionForm(MAX_ANSWERS)
    return render_to_response('qproposal/propose.html', {'form': form},
                              context_instance=RequestContext(request))
Esempio n. 11
0
def add(question, answers, category=None, tags=None, file_tags=None):
    ''' question is a dict with the following keys: text, endorsed_by, answer_type
    [, proposed_by, active, type, code]
    answers is a list of dicts with the following keys: text, correct [, explanation]
    '''

    # imports valid only after init()
    from wouso.core.qpool.models import Question, Answer, Tag, Category

    # create and save question
    q = Question(**question)
    q.save()

    if tags:
        for tag in tags:
            q.tags.add(tag)
        q.save()

    if file_tags:
        for tag_name in file_tags:
            tag, created = Tag.objects.get_or_create(name=tag_name,
                                                     category=category)
            if created:
                tag.save()
                if category is not None:
                    category.tag_set.add(tag)
                    category.save()
            q.tags.add(tag)
        q.save()

    if category is not None:
        q.category = category
        q.save()

    # create and save answers for question
    for answer in answers:
        a = Answer(question=q, **answer)
        a.save()

    return q
Esempio n. 12
0
def add_question(question):
    """ question is a dictionary with the following keys:
        'text': the question text (string)
        'answer_type': whether the question is single choice or multiple choice (string)
        'answers': a dictionary of answers with the following keys:
            'text': the answer text (string)
            'correct': whether the answer is correct (boolean)
        'proposed_by': the user that proposed the question (User object)
        'endorsed_by': the user endorsing the question (User object)
        'active': whether the question is active (boolean)
        'category': the question category (i.e. quiz) (Category object)
        'tag': the question primary tag (from file name) (Tag object)
        'file_tags': secondary tags (from file contents) (array of Tag objects)
    """
    q = Question()
    q.save()
    q.text = question['text']
    q.answer_type = question['answer_type']
    q.save()

    if question['proposed_by']:
        q.proposed_by = question['proposed_by']
    q.save()

    if question['endorsed_by']:
        q.endorsed_by = question['endorsed_by']
    q.save()

    q.active = question['active']

    if question['category']:
        q.category = question['category']
    q.save()

    if question['tag']:
        q.tags.add(question['tag'])
        q.save()

    if question['file_tags']:
        for tag in question['file_tags']:
            q.tags.add(tag)
        q.save()

    for answer in question['answers']:
        a = Answer(question=q, **answer)
        a.save()

    return q
Esempio n. 13
0
def _make_question_for_today(user, text):
    category = Category(name='qotd')
    question = Question(text=text, proposed_by=user, category=category, active=1)
    question.save()
    sched = Schedule(question=question)
    sched.save()
Esempio n. 14
0
def add_question(question):
    """ question is a dictionary with the following keys:
        'text': the question text (string)
        'answer_type': whether the question is single choice or multiple choice (string)
        'answers': a dictionary of answers with the following keys:
            'text': the answer text (string)
            'correct': whether the answer is correct (boolean)
        'proposed_by': the user that proposed the question (User object)
        'endorsed_by': the user endorsing the question (User object)
        'active': whether the question is active (boolean)
        'category': the question category (i.e. quiz) (Category object)
        'tag': the question primary tag (from file name) (Tag object)
        'file_tags': secondary tags (from file contents) (array of Tag objects)
    """
    q = Question()
    q.save()
    q.text = question['text']
    q.answer_type = question['answer_type']
    q.save()

    if question['proposed_by']:
        q.proposed_by = question['proposed_by']
    q.save()

    if question['endorsed_by']:
        q.endorsed_by = question['endorsed_by']
    q.save()

    q.active = question['active']

    if question['category']:
        q.category = question['category']
    q.save()

    if question['tag']:
        q.tags.add(question['tag'])
        q.save()

    if question['file_tags']:
        for tag in question['file_tags']:
            q.tags.add(tag)
        q.save()

    for answer in question['answers']:
        a = Answer(question=q, **answer)
        a.save()

    return q