def test_cleaned_metadata(self): """Test the cleaned_metadata property.""" # Test with no metadata data = {'title': 'Lorem', 'content': 'ipsum', 'email': '*****@*****.**'} product = {'key': 'desktop', 'name': 'Firefox on desktop', 'extra_fields': ['troubleshooting', 'ff_version', 'os', 'plugins'], } form = NewQuestionForm(product=product, data=data) form.is_valid() expected = {} actual = form.cleaned_metadata eq_(expected, actual) # Test with metadata data['os'] = u'Linux' form = NewQuestionForm(product=product, data=data) form.is_valid() expected = {'os': u'Linux'} actual = form.cleaned_metadata eq_(expected, actual) # Add an empty metadata value data['ff_version'] = u'' form = NewQuestionForm(product=product, data=data) form.is_valid() expected = {'os': u'Linux'} actual = form.cleaned_metadata eq_(expected, actual)
def test_metadata_keys(self): """Test metadata_field_keys property.""" # Test the default form form = NewQuestionForm() expected = ['useragent'] actual = form.metadata_field_keys eq_(expected, actual) # Test the form with a product product = {'key': 'desktop', 'name': 'Firefox on desktop', 'extra_fields': ['troubleshooting', 'ff_version', 'os', 'plugins'], } form = NewQuestionForm(product=product) expected = ['troubleshooting', 'ff_version', 'os', 'plugins', 'useragent'] actual = form.metadata_field_keys eq_(expected, actual) # Test the form with a product and category category = {'key': 'd6', 'name': 'I have another kind of problem with Firefox', 'extra_fields': ['frequency', 'started'], } form = NewQuestionForm(product=product, category=category) expected = ['frequency', 'started', 'troubleshooting', 'ff_version', 'os', 'plugins', 'useragent'] actual = form.metadata_field_keys eq_(expected, actual)
def ask(request): if request.method == 'POST': form = NewQuestionForm(request.POST) if form.is_valid(): pub = form.save(commit=False) pub.author = User.objects.get(username=request.user) pub.save() pub.tags.clear() tags = str(request.POST.get('tags')) tags = [tag.strip() for tag in tags.split(',')] for tag in tags: obj, create = Tag.objects.get_or_create(tagword=tag) pub.tags.add(obj) pub.save() return redirect('question', pub.id) else: print(form.errors) else: form = NewQuestionForm(instance=request.user) return render(request, 'questions/ask.html', { 'form': form, 'title': 'Ask', 'trending': _get_trending(), })
def new_question(request, politician_id=None): user = request.user request_info = get_request_information(request) question = Question( created_by=user, user_ip=request_info.client_ip, user_agent=request_info.client_user_agent, user_country=request_info.client_country ) selected_politician = PoliticianInfo.active.filter(id=politician_id).first() if politician_id else None initial = {} if selected_politician: initial['politician'] = selected_politician new_question_form = NewQuestionForm(instance=question, initial=initial) success = None if request.method == 'POST': success = False new_question_form = NewQuestionForm(request.POST, instance=question) if new_question_form.is_valid(): with reversion.create_revision(): new_question_form.save() return redirect('question', question_id=question.id) return render(request, 'questions/new-question.html', { 'new_question_form': new_question_form, 'success': success })
def new_question(request): """Ask a new question.""" product_key = request.GET.get('product') product = products.get(product_key) if product_key and not product: raise Http404 category_key = request.GET.get('category') if product and category_key: category = product['categories'].get(category_key) if not category: raise Http404 deadend = category.get('deadend', False) html = category.get('html') articles = category.get('articles') else: category = None deadend = product.get('deadend', False) if product else False html = product.get('html') if product else None articles = None if request.method == 'GET': search = request.GET.get('search', '') if search: try: search_results = _search_suggestions( search, locale_or_default(request.locale)) except SearchError: # Just quietly advance the user to the next step. search_results = [] tried_search = True else: search_results = [] tried_search = False if request.GET.get('showform'): # Before we show the form, make sure the user is auth'd: if not request.user.is_authenticated(): login_form = AuthenticationForm() register_form = RegisterForm() return jingo.render(request, 'questions/new_question_login.html', {'product': product, 'category': category, 'title': search, 'register_form': register_form, 'login_form': login_form}) form = NewQuestionForm(product=product, category=category, initial={'title': search}) else: form = None return jingo.render(request, 'questions/new_question.html', {'form': form, 'search_results': search_results, 'tried_search': tried_search, 'products': products, 'current_product': product, 'current_category': category, 'current_html': html, 'current_articles': articles, 'deadend': deadend, 'host': Site.objects.get_current().domain}) # Handle the form post. just_logged_in = False # Used below for whether to pre-load Question form. if not request.user.is_authenticated(): type = request.POST.get('type') if type not in ('login', 'register'): # L10n: This shouldn't happen unless people tamper with POST data message = _lazy('Request type not recognized.') return jingo.render(request, 'handlers/400.html', {'message': message}, status=400) if type == 'login': login_form = handle_login(request, only_active=False) register_form = RegisterForm() else: # must be 'register' login_form = AuthenticationForm() register_form = handle_register(request) if register_form.is_valid(): # now try to log in user = auth.authenticate(username=request.POST.get('username'), password=request.POST.get('password')) auth.login(request, user) if not request.user.is_authenticated(): return jingo.render(request, 'questions/new_question_login.html', {'product': product, 'category': category, 'title': request.POST.get('title'), 'register_form': register_form, 'login_form': login_form}) else: just_logged_in = True if just_logged_in: form = NewQuestionForm(product=product, category=category, initial={'title': request.GET.get('search')}) else: form = NewQuestionForm(product=product, category=category, data=request.POST) if form.is_valid(): question = Question(creator=request.user, title=form.cleaned_data['title'], content=form.cleaned_data['content']) question.save() question.add_metadata(**form.cleaned_metadata) if product: question.add_metadata(product=product['key']) if category: question.add_metadata(category=category['key']) # The first time a question is saved, automatically apply some tags: question.auto_tag() # Submitting the question counts as a vote question_vote(request, question.id) if request.user.is_active: url = reverse('questions.answers', kwargs={'question_id': question.id}) return HttpResponseRedirect(urlparams(url, new=1)) auth.logout(request) return jingo.render(request, 'questions/confirm_email.html', {'question': question}) return jingo.render(request, 'questions/new_question.html', {'form': form, 'products': products, 'current_product': product, 'current_category': category, 'current_articles': articles})
def new_question(request, template=None): """Ask a new question.""" product_key = request.GET.get('product') product = products.get(product_key) if product_key and not product: raise Http404 category_key = request.GET.get('category') if product and category_key: category = product['categories'].get(category_key) if not category: raise Http404 deadend = category.get('deadend', False) html = category.get('html') articles = category.get('articles') else: category = None deadend = product.get('deadend', False) if product else False html = product.get('html') if product else None articles = None login_t = ('questions/mobile/new_question_login.html' if request.MOBILE else 'questions/new_question_login.html') if request.method == 'GET': search = request.GET.get('search', '') if search: try: search_results = _search_suggestions( search, locale_or_default(request.locale)) except SearchError: # Just quietly advance the user to the next step. search_results = [] tried_search = True else: search_results = [] tried_search = False if request.GET.get('showform'): # Before we show the form, make sure the user is auth'd: if not request.user.is_authenticated(): login_form = AuthenticationForm() register_form = RegisterForm() return jingo.render( request, login_t, { 'product': product, 'category': category, 'title': search, 'register_form': register_form, 'login_form': login_form }) form = NewQuestionForm(product=product, category=category, initial={'title': search}) else: form = None return jingo.render( request, template, { 'form': form, 'search_results': search_results, 'tried_search': tried_search, 'products': products, 'current_product': product, 'current_category': category, 'current_html': html, 'current_articles': articles, 'deadend': deadend, 'host': Site.objects.get_current().domain }) # Handle the form post. if not request.user.is_authenticated(): if request.POST.get('login'): login_form = handle_login(request, only_active=False) register_form = RegisterForm() elif request.POST.get('register'): login_form = AuthenticationForm() email_template = 'questions/email/confirm_question.ltxt' email_subject = _('Please confirm your Firefox Help question') email_data = request.GET.get('search') register_form = handle_register(request, email_template, email_subject, email_data) if register_form.is_valid(): # now try to log in user = auth.authenticate(username=request.POST.get('username'), password=request.POST.get('password')) auth.login(request, user) else: # L10n: This shouldn't happen unless people tamper with POST data message = _lazy('Request type not recognized.') return jingo.render(request, 'handlers/400.html', {'message': message}, status=400) if request.user.is_authenticated(): # Redirect to GET the current URL. # This is required for the csrf middleware to set the auth'd tokens # appropriately. return HttpResponseRedirect(request.get_full_path()) else: return jingo.render( request, login_t, { 'product': product, 'category': category, 'title': request.POST.get('title'), 'register_form': register_form, 'login_form': login_form }) form = NewQuestionForm(product=product, category=category, data=request.POST) if form.is_valid(): question = Question(creator=request.user, title=form.cleaned_data['title'], content=form.cleaned_data['content']) question.save() question.add_metadata(**form.cleaned_metadata) if product: question.add_metadata(product=product['key']) if category: question.add_metadata(category=category['key']) # The first time a question is saved, automatically apply some tags: question.auto_tag() # Submitting the question counts as a vote question_vote(request, question.id) if request.user.is_active: messages.add_message( request, messages.SUCCESS, _('Thanks! Your question has been posted. See it below.')) url = reverse('questions.answers', kwargs={'question_id': question.id}) return HttpResponseRedirect(url) auth.logout(request) confirm_t = ('questions/mobile/confirm_email.html' if request.MOBILE else 'questions/confirm_email.html') return jingo.render(request, confirm_t, {'question': question}) return jingo.render( request, template, { 'form': form, 'products': products, 'current_product': product, 'current_category': category, 'current_articles': articles })