def login(request, template): """Try to log the user in.""" if request.method == 'GET' and not request.MOBILE: url = reverse('users.auth') + '?' + request.GET.urlencode() return HttpResponsePermanentRedirect(url) next_url = get_next_url(request) or reverse('home') form = handle_login(request) if request.user.is_authenticated(): # Add a parameter so we know the user just logged in. # fpa = "first page authed" or something. next_url = urlparams(next_url, fpa=1) res = HttpResponseRedirect(next_url) max_age = (None if settings.SESSION_EXPIRE_AT_BROWSER_CLOSE else settings.SESSION_COOKIE_AGE) res.set_cookie(settings.SESSION_EXISTS_COOKIE, '1', secure=False, max_age=max_age) return res if request.MOBILE: return jingo.render(request, template, { 'form': form, }) return user_auth(request, login_form=form)
def login(request, template): """Try to log the user in.""" if request.method == 'GET' and not request.MOBILE: url = reverse('users.auth') + '?' + request.GET.urlencode() return HttpResponsePermanentRedirect(url) next_url = get_next_url(request) or reverse('home') form = handle_login(request) if request.user.is_authenticated(): # Add a parameter so we know the user just logged in. # fpa = "first page authed" or something. next_url = urlparams(next_url, fpa=1) res = HttpResponseRedirect(next_url) max_age = (None if settings.SESSION_EXPIRE_AT_BROWSER_CLOSE else settings.SESSION_COOKIE_AGE) res.set_cookie(settings.SESSION_EXISTS_COOKIE, '1', secure=False, max_age=max_age) return res if request.MOBILE: return render(request, template, {'form': form}) return user_auth(request, login_form=form)
def login(request, mobile=False): """Try to log the user in.""" form = handle_login(request) next = clean_next_url(request) if mobile: next_url = next or reverse('mobile.home') if request.user.is_authenticated(): profile = request.user.profile if not profile.login_mobile: profile.login_mobile = True profile.save() profile.trigger_multisparker_badge() return HttpResponseRedirect(next_url) return jingo.render(request, 'users/mobile/login.html', {'form': form, 'next_url': next_url}) else: # ajax login if request.method == 'POST' and request.is_ajax(): if not form.is_valid(): return {'status': 'error', 'errors': dict(form.errors.iteritems())} else: profile = request.user.profile if not profile.login_desktop: profile.login_desktop = True profile.save() profile.trigger_multisparker_badge() return {'status': 'success', 'next': next or reverse('desktop.home')} return HttpResponseBadRequest()
def browserid_signin_html(request): next_url = _clean_next_url(request) or reverse('home') browserid_locales = constance.config.BROWSERID_LOCALES if request.locale.lower() not in browserid_locales.lower(): raise Http404 form = handle_login(request) return jingo.render(request, 'users/browserid_signin.html', {'form': form, 'next_url': next_url})
def login(request): """Try to log the user in.""" next_url = _clean_next_url(request) or reverse('home') form = handle_login(request) if request.user.is_authenticated(): return HttpResponseRedirect(next_url) return jingo.render(request, 'users/login.html', {'form': form, 'next_url': next_url})
def browserid_signin_html(request): next_url = _clean_next_url(request) or reverse('home') browserid_locales = constance.config.BROWSERID_LOCALES if request.locale.lower() not in browserid_locales.lower(): raise Http404 form = handle_login(request) return jingo.render(request, 'users/browserid_signin.html', { 'form': form, 'next_url': next_url })
def login(request): """Try to log the user in.""" next_url = get_next_url(request) or reverse('home') form = handle_login(request) if request.user.is_authenticated(): return HttpResponseRedirect(next_url) return jingo.render(request, 'users/login.html', { 'form': form, 'next_url': next_url })
def login(request): """Try to log the user in.""" next_url = get_next_url(request) or reverse('home') form = handle_login(request) if request.user.is_authenticated(): res = HttpResponseRedirect(next_url) res.set_cookie(settings.SESSION_EXISTS_COOKIE, '1', secure=False) return res return jingo.render(request, 'users/login.html', {'form': form, 'next_url': next_url})
def login(request): """Try to log the user in.""" next_url = _clean_next_url(request) or reverse('home') form = handle_login(request) if request.user.is_authenticated(): return _redirect_with_mindtouch_login(next_url, form.cleaned_data.get('username'), form.cleaned_data.get('password')) response = jingo.render(request, 'users/login.html', {'form': form, 'next_url': next_url}) response['x-frame-options'] = 'SAMEORIGIN' return response
def login(request): """Try to log the user in.""" next_url = _clean_next_url(request) or reverse('home') form = handle_login(request) if request.user.is_authenticated(): return HttpResponseRedirect(next_url) response = jingo.render(request, 'users/login.html', { 'form': form, 'next_url': next_url }) response['x-frame-options'] = 'SAMEORIGIN' return response
def login(request): """Try to log the user in.""" next_url = _clean_next_url(request) if request.method == 'GET' and request.user.is_authenticated(): if next_url: return HttpResponseRedirect(next_url) else: next_url = _clean_next_url(request) or reverse('home') form = handle_login(request) if form.is_valid() and request.user.is_authenticated(): next_url = next_url or reverse('home') return HttpResponseRedirect(next_url) return render(request, 'users/login.html', {'form': form, 'next_url': next_url})
def login(request): """Try to log the user in.""" next_url = _clean_next_url(request) if request.method == "GET" and request.user.is_authenticated(): if next_url: return HttpResponseRedirect(next_url) else: next_url = _clean_next_url(request) or reverse("home") form = handle_login(request) if form.is_valid() and request.user.is_authenticated(): next_url = next_url or reverse("home") return _redirect_with_mindtouch_login( next_url, form.cleaned_data.get("username"), form.cleaned_data.get("password") ) return render(request, "users/login.html", {"form": form, "next_url": next_url})
def login(request): """Try to log the user in.""" next_url = _clean_next_url(request) if request.method == 'GET' and request.user.is_authenticated(): if next_url: return HttpResponseRedirect(next_url) else: next_url = _clean_next_url(request) or reverse('home') form = handle_login(request) if form.is_valid() and request.user.is_authenticated(): next_url = next_url or reverse('home') return _redirect_with_mindtouch_login(next_url, form.cleaned_data.get('username'), form.cleaned_data.get('password')) return render(request, 'users/login.html', {'form': form, 'next_url': next_url})
def login(request): """Try to log the user in.""" next_url = _clean_next_url(request) if request.method == 'GET' and request.user.is_authenticated(): if next_url: return HttpResponseRedirect(next_url) else: next_url = _clean_next_url(request) or reverse('home') form = handle_login(request) if form.is_valid() and request.user.is_authenticated(): next_url = next_url or reverse('home') return HttpResponseRedirect(next_url) return render(request, 'users/login.html', { 'form': form, 'next_url': next_url })
def login(request): """Try to log the user in.""" next_url = _clean_next_url(request) if request.method == 'GET' and request.user.is_authenticated(): if next_url: return HttpResponseRedirect(next_url) else: next_url = _clean_next_url(request) or reverse('home') form = handle_login(request) if form.is_valid() and request.user.is_authenticated(): next_url = next_url or reverse('home') return _redirect_with_mindtouch_login(next_url, form.cleaned_data.get('username'), form.cleaned_data.get('password')) return jingo.render(request, 'users/login.html', {'form': form, 'next_url': next_url})
def login(request): """Try to log the user in.""" next_url = get_next_url(request) or reverse('home') form = handle_login(request) if request.user.is_authenticated(): res = HttpResponseRedirect(next_url) max_age = (None if settings.SESSION_EXPIRE_AT_BROWSER_CLOSE else settings.SESSION_COOKIE_AGE) res.set_cookie(settings.SESSION_EXISTS_COOKIE, '1', secure=False, max_age=max_age) return res return jingo.render(request, 'users/login.html', { 'form': form, 'next_url': next_url })
def login(request, template): """Try to log the user in.""" if request.method == 'GET' and not request.MOBILE: url = reverse('users.auth') + '?' + request.GET.urlencode() return HttpResponsePermanentRedirect(url) next_url = get_next_url(request) or reverse('home') form = handle_login(request) if request.user.is_authenticated(): res = HttpResponseRedirect(next_url) max_age = (None if settings.SESSION_EXPIRE_AT_BROWSER_CLOSE else settings.SESSION_COOKIE_AGE) res.set_cookie(settings.SESSION_EXISTS_COOKIE, '1', secure=False, max_age=max_age) return res return user_auth(request, login_form=form)
def login(request, mobile=False): """Try to log the user in.""" form = handle_login(request) next = clean_next_url(request) if mobile: next_url = next or reverse('mobile.home') if request.user.is_authenticated(): profile = request.user.profile if not profile.login_mobile: profile.login_mobile = True profile.save() profile.trigger_multisparker_badge() return HttpResponseRedirect(next_url) return jingo.render(request, 'users/mobile/login.html', { 'form': form, 'next_url': next_url }) else: # ajax login if request.method == 'POST' and request.is_ajax(): if not form.is_valid(): return { 'status': 'error', 'errors': dict(form.errors.iteritems()) } else: profile = request.user.profile if not profile.login_desktop: profile.login_desktop = True profile.save() profile.trigger_multisparker_badge() return { 'status': 'success', 'next': next or reverse('desktop.home') } return HttpResponseBadRequest()
def login(request, mobile=False): """Try to log the user in.""" form = handle_login(request) if mobile: next_url = _clean_next_url(request) or reverse('mobile.home') if request.user.is_authenticated(): return HttpResponseRedirect(next_url) return jingo.render(request, 'users/mobile/login.html', {'form': form, 'next_url': next_url}) else: # ajax login if request.method == 'POST' and request.is_ajax(): if not form.is_valid(): return {'status': 'error', 'errors': dict(form.errors.iteritems())} else: return {'status': 'success', 'next': reverse('desktop.dashboard')} return HttpResponseBadRequest()
def browserid_register(request): """Handle user creation when assertion is valid, but no existing user""" redirect_to = request.session.get( SESSION_REDIRECT_TO, getattr(settings, 'LOGIN_REDIRECT_URL', reverse('home'))) email = request.session.get(SESSION_VERIFIED_EMAIL, None) if not email: # This is pointless without a verified email. return HttpResponseRedirect(redirect_to) # Set up the initial forms register_form = BrowserIDRegisterForm() login_form = AuthenticationForm() if request.method == 'POST': # If the profile creation form was submitted... if 'register' == request.POST.get('action', None): register_form = BrowserIDRegisterForm(request.POST) if register_form.is_valid(): try: # If the registration form is valid, then create a new # Django user, a new MindTouch user, and link the two # together. # TODO: This all belongs in model classes username = register_form.cleaned_data['username'] user = User.objects.create(username=username, email=email) user.set_unusable_password() user.save() profile = UserProfile.objects.create(user=user) if settings.DEKIWIKI_ENDPOINT: deki_user = DekiUserBackend.post_mindtouch_user(user) profile.deki_user_id = deki_user.id profile.save() user.backend = 'django_browserid.auth.BrowserIDBackend' auth.login(request, user) # Bounce to the newly created profile page, since the user # might want to review & edit. redirect_to = request.session.get( SESSION_REDIRECT_TO, profile.get_absolute_url()) return set_browserid_explained( _redirect_with_mindtouch_login(redirect_to, user.username)) except MindTouchAPIError: if user: user.delete() return jingo.render( request, '500.html', { 'error_message': "We couldn't " "register a new account at this time. " "Please try again later." }) else: # If login was valid, then set to the verified email login_form = handle_login(request) if login_form.is_valid(): if request.user.is_authenticated(): # Change email to new verified email, for next time user = request.user user.email = email user.save() return _redirect_with_mindtouch_login( redirect_to, login_form.cleaned_data.get('username'), login_form.cleaned_data.get('password')) # HACK: Pretend the session was modified. Otherwise, the data disappears # for the next request. request.session.modified = True return jingo.render(request, 'users/browserid_register.html', { 'login_form': login_form, 'register_form': register_form })
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 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, }, )
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 browserid_register(request): """Handle user creation when assertion is valid, but no existing user""" redirect_to = request.session.get(SESSION_REDIRECT_TO, getattr(settings, 'LOGIN_REDIRECT_URL', reverse('home'))) email = request.session.get(SESSION_VERIFIED_EMAIL, None) if not email: # This is pointless without a verified email. return HttpResponseRedirect(redirect_to) # Set up the initial forms register_form = BrowserIDRegisterForm() login_form = AuthenticationForm() if request.method == 'POST': # If the profile creation form was submitted... if 'register' == request.POST.get('action', None): register_form = BrowserIDRegisterForm(request.POST) if register_form.is_valid(): try: # If the registration form is valid, then create a new # Django user, a new MindTouch user, and link the two # together. # TODO: This all belongs in model classes username = register_form.cleaned_data['username'] user = User.objects.create(username=username, email=email) user.set_unusable_password() user.save() profile = UserProfile.objects.create(user=user) deki_user = DekiUserBackend.post_mindtouch_user(user) profile.deki_user_id = deki_user.id profile.save() user.backend = 'django_browserid.auth.BrowserIDBackend' auth.login(request, user) # Bounce to the newly created profile page, since the user # might want to review & edit. redirect_to = request.session.get(SESSION_REDIRECT_TO, profile.get_absolute_url()) return set_browserid_explained( _redirect_with_mindtouch_login(redirect_to, user.username)) except MindTouchAPIError: if user: user.delete() return jingo.render(request, '500.html', {'error_message': "We couldn't " "register a new account at this time. " "Please try again later."}) else: # If login was valid, then set to the verified email login_form = handle_login(request) if login_form.is_valid(): if request.user.is_authenticated(): # Change email to new verified email, for next time user = request.user user.email = email user.save() return _redirect_with_mindtouch_login(redirect_to, login_form.cleaned_data.get('username'), login_form.cleaned_data.get('password')) # HACK: Pretend the session was modified. Otherwise, the data disappears # for the next request. request.session.modified = True return jingo.render(request, 'users/browserid_register.html', {'login_form': login_form, 'register_form': register_form})
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: 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 })