def question_new(request): # TODO - user has to be logged in ctx = {} if request.method == 'POST': initial = {'author': users.get_current_user()} form = NewQuestionForm(request.POST, initial=initial) if form.is_valid(): # XXX FIXME - why author property does not exist in cleaned data? form.cleaned_data['author'] = users.get_current_user() question = form.save() QuestionTag.increment_tags(question.tags) redirect_url = reverse('questions_question_details', kwargs={'question_key': question.key()}) return redirect(redirect_url) ctx['form'] = form else: ctx['form'] = NewQuestionForm() return render(request, 'questions/question_new.html', ctx)
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)
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()
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()
def question_list(request, tag=None): ctx = { 'active_tags': [], } # TODO - pagination questions = Question.all().order('-score') # TODO - multiple tags filter support if tag: questions = questions.filter('tags =', tag) ctx['active_tags'].append(tag) tags = QuestionTag.all().order('-ref_count') ctx['questions'] = questions ctx['tags'] = tags return render(request, 'questions/question_list.html', ctx)
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)