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 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 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 NewQuestion(request): if not request.user.is_authenticated(): return HttpResponseRedirect('/login/') if request.method == 'POST': form = NewQuestionForm(request.POST) if form.is_valid(): question = Question() question.user=request.user question.title = form.cleaned_data['title'] question.text = form.cleaned_data['text'] question.save() print "Ok" return HttpResponseRedirect('/question/'+str(question.pk)+'/') else: return render_to_response('new_question.html', {'form': form}, context_instance=RequestContext(request)) else: ''' ''' form = NewQuestionForm() context = {'form':form} return render_to_response('new_question.html',context, context_instance=RequestContext(request))
def aaq(request, product_key=None, category_key=None, showform=False, template=None, step=0): """Ask a new question.""" if product_key is None: product_key = request.GET.get('product') if request.MOBILE and product_key is None: product_key = 'desktop' product = products.get(product_key) if product_key and not product: raise Http404 if category_key is None: category_key = request.GET.get('category') if product and category_key: category = product['categories'].get(category_key) if not category: # If we get an invalid category, redirect to previous step. return HttpResponseRedirect( reverse('questions.aaq_step2', args=[product_key])) deadend = category.get('deadend', False) topic = category.get('topic') if topic: html = None articles, fallback = documents_for( locale=settings.WIKI_DEFAULT_LANGUAGE, # en-US only for now. products=Product.objects.filter( slug__in=product.get('products')), topics=[Topic.objects.get(slug=topic)]) else: 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 product: # User is on the select category step statsd.incr('questions.aaq.select-category') else: # User is on the select product step statsd.incr('questions.aaq.select-product') 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: results = _search_suggestions( request, search, locale_or_default(request.locale), product.get('tags'), product.get('products')) tried_search = True else: results = [] tried_search = False if category: # User is on the "Ask This" step statsd.incr('questions.aaq.search-form') if showform or request.GET.get('showform'): # Before we show the form, make sure the user is auth'd: if not request.user.is_authenticated(): # User is on the login or register Step statsd.incr('questions.aaq.login-or-register') 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}) # User is on the question details step statsd.incr('questions.aaq.details-form') else: form = None if search: # User is on the article and questions suggestions step statsd.incr('questions.aaq.suggestions') return jingo.render(request, template, {'form': form, 'results': results, 'tried_search': tried_search, 'products': products, 'current_product': product, 'current_category': category, 'current_html': html, 'current_articles': articles, 'current_step': step, '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) statsd.incr('questions.user.login') 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) statsd.incr('questions.user.register') 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 replacing the step parameter. # This is also required for the csrf middleware to set the auth'd # tokens appropriately. url = urlparams(request.get_full_path(), step='aaq-question') return HttpResponseRedirect(url) 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() # User successfully submitted a new question statsd.incr('questions.new') question.add_metadata(**form.cleaned_metadata) if product: # TODO: This add_metadata call should be removed once we are # fully IA-driven (sync isn't special case anymore). question.add_metadata(product=product['key']) for p in Product.objects.filter(slug__in=product.get('products')): question.products.add(p) if category: # TODO: This add_metadata call should be removed once we are # fully IA-driven (sync isn't special case anymore). question.add_metadata(category=category['key']) t = category.get('topic') if t: question.topics.add(Topic.objects.get(slug=t)) # 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, _('Done! Your question is now posted on the Mozilla community ' 'support forum.')) url = reverse('questions.answers', kwargs={'question_id': question.id}) return HttpResponseRedirect(url) return HttpResponseRedirect(reverse('questions.aaq_confirm')) statsd.incr('questions.aaq.details-form-error') return jingo.render(request, template, {'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: results = _search_suggestions( search, locale_or_default(request.locale)) except SearchError: # Just quietly advance the user to the next step. results = [] tried_search = True else: 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, 'results': 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) statsd.incr('questions.user.login') 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) statsd.incr('questions.user.register') 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() statsd.incr('questions.new') 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) statsd.incr('questions.user.logout') 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})
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 })
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 if product: # User is on the select category step statsd.incr("questions.aaq.select-category") else: # User is on the select product step statsd.incr("questions.aaq.select-product") 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: results = _search_suggestions(search, locale_or_default(request.locale), product.get("tags")) except SearchError: # Just quietly advance the user to the next step. results = [] tried_search = True else: results = [] tried_search = False if category: # User is on the "Ask This" step statsd.incr("questions.aaq.search-form") if request.GET.get("showform"): # Before we show the form, make sure the user is auth'd: if not request.user.is_authenticated(): # User is on the login or register Step statsd.incr("questions.aaq.login-or-register") 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}) # User is on the question details step statsd.incr("questions.aaq.details-form") else: form = None if search: # User is on the article and questions suggestions step statsd.incr("questions.aaq.suggestions") return jingo.render( request, template, { "form": form, "results": 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) statsd.incr("questions.user.login") 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) statsd.incr("questions.user.register") 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() # User successfully submitted a new question statsd.incr("questions.new") 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) statsd.incr("questions.user.logout") confirm_t = "questions/mobile/confirm_email.html" if request.MOBILE else "questions/confirm_email.html" return jingo.render(request, confirm_t, {"question": question}) statsd.incr("questions.aaq.details-form-error") return jingo.render( request, template, { "form": form, "products": products, "current_product": product, "current_category": category, "current_articles": articles, }, )
def aaq(request, product_key=None, category_key=None, showform=False, template=None, step=0): """Ask a new question.""" if product_key is None: product_key = request.GET.get("product") if request.MOBILE and product_key is None: product_key = "mobile" product = products.get(product_key) if product_key and not product: raise Http404 if category_key is None: category_key = request.GET.get("category") if product and category_key: category = product["categories"].get(category_key) if not category: # If we get an invalid category, redirect to previous step. return HttpResponseRedirect(reverse("questions.aaq_step2", args=[product_key])) deadend = category.get("deadend", False) topic = category.get("topic") if topic: html = None articles, fallback = documents_for( locale=request.LANGUAGE_CODE, products=Product.objects.filter(slug__in=product.get("products")), topics=[Topic.objects.get(slug=topic)], ) else: 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 product: # User is on the select category step statsd.incr("questions.aaq.select-category") else: # User is on the select product step statsd.incr("questions.aaq.select-product") 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: results = _search_suggestions( request, search, locale_or_default(request.LANGUAGE_CODE), product.get("products") ) tried_search = True else: results = [] tried_search = False if category: # User is on the "Ask This" step statsd.incr("questions.aaq.search-form") if showform or request.GET.get("showform"): # Before we show the form, make sure the user is auth'd: if not request.user.is_authenticated(): # User is on the login or register Step statsd.incr("questions.aaq.login-or-register") 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}) # User is on the question details step statsd.incr("questions.aaq.details-form") else: form = None if search: # User is on the article and questions suggestions step statsd.incr("questions.aaq.suggestions") return jingo.render( request, template, { "form": form, "results": results, "tried_search": tried_search, "products": products, "current_product": product, "current_category": category, "current_html": html, "current_articles": articles, "current_step": step, "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) statsd.incr("questions.user.login") 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) statsd.incr("questions.user.register") 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 replacing the step parameter. # This is also required for the csrf middleware to set the auth'd # tokens appropriately. url = urlparams(request.get_full_path(), step="aaq-question") return HttpResponseRedirect(url) 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"], locale=request.LANGUAGE_CODE, ) question.save() # User successfully submitted a new question statsd.incr("questions.new") question.add_metadata(**form.cleaned_metadata) if product: # TODO: This add_metadata call should be removed once we are # fully IA-driven (sync isn't special case anymore). question.add_metadata(product=product["key"]) if product.get("products"): for p in Product.objects.filter(slug__in=product["products"]): question.products.add(p) if category: # TODO: This add_metadata call should be removed once we are # fully IA-driven (sync isn't special case anymore). question.add_metadata(category=category["key"]) t = category.get("topic") if t: question.topics.add(Topic.objects.get(slug=t)) # 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, _("Done! Your question is now posted on the Mozilla community " "support forum."), ) url = reverse("questions.answers", kwargs={"question_id": question.id}) return HttpResponseRedirect(url) return HttpResponseRedirect(reverse("questions.aaq_confirm")) statsd.incr("questions.aaq.details-form-error") return jingo.render( request, template, { "form": form, "products": products, "current_product": product, "current_category": category, "current_articles": articles, }, )