示例#1
0
def view_edit_question(request, question_id, question_slug, edit_question_template):
    userprofile = loggedin_userprofile(request)
    question = get_object_or_404(Question, id=int(question_id))
    if userprofile.is_my_question(question):
        asked_questions = list(userprofile.asked_questions)
        for question_info in asked_questions:
            if question_info['id'] == int(question_id):
                asked_questions.remove({'title':question.title,
                                        'id':int(question_id),
                                        'slug':question_slug})
        
        if request.method == 'GET':
            question_data = {'title':question.title,
                             'description':question.description,
                             'tags':",".join([tag['name'] for tag in question.tags.values('name')])}
            form = AskQuestionForm(question_data)
            return response(request, edit_question_template, {'form':form,
                                                              'question':question,
                                                              'previous_questions':asked_questions})
        form = AskQuestionForm(post_data(request))
        if form.is_valid():
            Question.objects.update_question(question,
                                             title=form.cleaned_data.get('title'),
                                             description=form.cleaned_data.get('description'),
                                             tags=form.cleaned_data.get('tags'))
            from quest.messages import QUESTION_UPDATED_SUCCESSFULLY
            messages.success(request, QUESTION_UPDATED_SUCCESSFULLY)
            return HttpResponseRedirect(redirect_to=url_reverse('quest.views.view_question', args=(question.id, question.slug)))
        return response(request, edit_question_template, {'form':form,
                                                          'question':question,
                                                          'previous_questions':asked_questions})
    raise Http404
示例#2
0
 def test_invalid_ask_question(self):
     form_data = {'title': '', 'description': ''}
     form = AskQuestionForm(form_data)
     self.assertFalse(form.is_valid())
     self.assertTrue(form.errors)
     self.assertTrue(form.errors.has_key('title'))
     self.assertTrue(form.errors.has_key('description'))
示例#3
0
 def test_valid_ask_question(self):
     form_data = {'title':'How do we handle recursion in Python',
                  'description':'I am a newbee to Python. Can you share me ways to handle recurion in python ?',
                  'tags':'recursion,python'}
     form = AskQuestionForm(form_data)
     self.assertTrue(form.is_valid())
     self.assertFalse(form.errors)
示例#4
0
 def test_invalid_ask_question(self):
     form_data = {'title':'', 'description':''}
     form = AskQuestionForm(form_data)
     self.assertFalse(form.is_valid())
     self.assertTrue(form.errors)
     self.assertTrue(form.errors.has_key('title'))
     self.assertTrue(form.errors.has_key('description'))
示例#5
0
 def test_valid_ask_question(self):
     form_data = {
         'title': 'How do we handle recursion in Python',
         'description':
         'I am a newbee to Python. Can you share me ways to handle recurion in python ?',
         'tags': 'recursion,python'
     }
     form = AskQuestionForm(form_data)
     self.assertTrue(form.is_valid())
     self.assertFalse(form.errors)
示例#6
0
def view_edit_question(request, question_id, question_slug,
                       edit_question_template):
    userprofile = loggedin_userprofile(request)
    question = get_object_or_404(Question, id=int(question_id))
    if userprofile.is_my_question(question):
        asked_questions = list(userprofile.asked_questions)
        for question_info in asked_questions:
            if question_info['id'] == int(question_id):
                asked_questions.remove({
                    'title': question.title,
                    'id': int(question_id),
                    'slug': question_slug
                })

        if request.method == 'GET':
            question_data = {
                'title':
                question.title,
                'description':
                question.description,
                'tags':
                ",".join([tag['name'] for tag in question.tags.values('name')])
            }
            form = AskQuestionForm(question_data)
            return response(
                request, edit_question_template, {
                    'form': form,
                    'question': question,
                    'previous_questions': asked_questions
                })
        form = AskQuestionForm(post_data(request))
        if form.is_valid():
            Question.objects.update_question(
                question,
                title=form.cleaned_data.get('title'),
                description=form.cleaned_data.get('description'),
                tags=form.cleaned_data.get('tags'))
            from quest.messages import QUESTION_UPDATED_SUCCESSFULLY
            messages.success(request, QUESTION_UPDATED_SUCCESSFULLY)
            return HttpResponseRedirect(redirect_to=url_reverse(
                'quest.views.view_question', args=(question.id,
                                                   question.slug)))
        return response(
            request, edit_question_template, {
                'form': form,
                'question': question,
                'previous_questions': asked_questions
            })
    raise Http404
示例#7
0
def view_ask_question(request, ask_question_template):
    userprofile = loggedin_userprofile(request)
    asked_questions = userprofile.asked_questions
    if request.method == 'GET':
        form = AskQuestionForm()
        return response(request, ask_question_template, {'form':form,
                                                         'asked_questions':asked_questions})
    form = AskQuestionForm(post_data(request))
    if form.is_valid():
        question = Question.objects.create_question(title=form.cleaned_data.get('title'),
                                                    description=form.cleaned_data.get('description'),
                                                    userprofile=userprofile,
                                                    tags=form.cleaned_data.get('tags'))
        messages.success(request, QUESTION_POSTING_SUCCESSFUL)
        return HttpResponseRedirect(redirect_to=url_reverse('quest.views.view_question', args=(question.id, question.slug)))
    return response(request, ask_question_template, {'form':form,
                                                     'asked_questions':asked_questions})
示例#8
0
def view_ask_question(request, ask_question_template):
    userprofile = loggedin_userprofile(request)
    asked_questions = userprofile.asked_questions
    if request.method == 'GET':
        form = AskQuestionForm()
        return response(request, ask_question_template, {
            'form': form,
            'asked_questions': asked_questions
        })
    form = AskQuestionForm(post_data(request))
    if form.is_valid():
        question = Question.objects.create_question(
            title=form.cleaned_data.get('title'),
            description=form.cleaned_data.get('description'),
            userprofile=userprofile,
            tags=form.cleaned_data.get('tags'))
        messages.success(request, QUESTION_POSTING_SUCCESSFUL)
        return HttpResponseRedirect(redirect_to=url_reverse(
            'quest.views.view_question', args=(question.id, question.slug)))
    return response(request, ask_question_template, {
        'form': form,
        'asked_questions': asked_questions
    })