示例#1
0
def question_create(request):
    if request.method == 'POST':
        question_form = QuestionForm(request.POST)
        if question_form.is_valid():
            question_post = question_form.save(commit=False)
            question_post.author = request.user
            question_post.save()
            tag_ids = request.POST.get('tags').split(',')
            print(tag_ids)
            for tag_id in tag_ids:
                if tag_id == None:
                    tag_id = 8
                question_tag = QuestionTag(tag_id=tag_id, question_id=question_post.id)
                question_tag.save()
            messages.success(
                request, f'Your question has been added!', extra_tags='success')
            return redirect('questions-home')
        else:
            messages.error(request, f'Something went wrong!',
                           extra_tags='danger')
    else:
        question_form = QuestionForm()
        context = {
            'question_form': question_form,
        }
    return render(request, 'questions/question_create.html', context)
示例#2
0
    def create_question(self, user_id, question_detaiis: dict):
        """
        :param user_id: str
        :param question_detaiis: {
            "title": str
            "body": str
            "tags": List[str]

        }
        :return:
        """
        question = Question.object.create(user_id=user_id,
                                          title=question_detaiis['title'],
                                          body=question_detaiis['body'])
        question_tag_objs = []
        for tag in question_detaiis['tags']:
            question_tag_objs.append(QuestionTag(question=question,
                                                 tag_id=tag))

        QuestionTag.objects.bulk_create(question_tag_objs)
示例#3
0
def add_question(text, imgpath, tagwords=None):
    '''
    text: question text
    imgpath: path to the image
    '''
    if tagwords is None:
        tagwords = []

    question = Question(text=text, diagram=imgpath)
    question.save()

    for tagword in tagwords:
        if QuestionTag.objects.filter(word=tagword).exists():
            tag = QuestionTag.objects.get(word=tagword)
        else:
            tag = QuestionTag(word=tagword)
            tag.save()
        question.tags.add(tag)

    # Update new tags
    question.save()