def _clean_next_url(request): if 'next' in request.POST: url = request.POST.get('next') elif 'next' in request.GET: url = request.GET.get('next') elif 'HTTP_REFERER' in request.META: url = request.META.get('HTTP_REFERER').decode('latin1', 'ignore') else: return None site = Site.objects.get_current() if not is_safe_url(url, site.domain): return None parsed_url = urlparse.urlparse(url) # Don't redirect right back to login, logout, register, or # change email pages locale, register_url = split_path(reverse( 'users.browserid_register')) locale, change_email_url = split_path(reverse( 'users.change_email')) LOOPING_NEXT_URLS = [settings.LOGIN_URL, settings.LOGOUT_URL, register_url, change_email_url] for looping_url in LOOPING_NEXT_URLS: if looping_url in parsed_url.path: return None # TODO?HACK: can't use urllib.quote_plus because mod_rewrite quotes the # next url value already. url = url.replace(' ', '+') return url
def _clean_next_url(request): if "next" in request.POST: url = request.POST.get("next") elif "next" in request.GET: url = request.GET.get("next") elif "HTTP_REFERER" in request.META: url = request.META.get("HTTP_REFERER").decode("latin1", "ignore") else: url = None if url: parsed_url = urlparse.urlparse(url) # Don't redirect outside of site_domain. # Don't include protocol+domain, so if we are https we stay that way. if parsed_url.scheme: site_domain = Site.objects.get_current().domain url_domain = parsed_url.netloc if site_domain != url_domain: url = None else: url = u"?".join([getattr(parsed_url, x) for x in ("path", "query") if getattr(parsed_url, x)]) # Don't redirect right back to login, logout, register, or change email # pages locale, register_url = split_path(reverse("users.browserid_register")) locale, change_email_url = split_path(reverse("users.change_email")) for looping_url in [settings.LOGIN_URL, settings.LOGOUT_URL, register_url, change_email_url]: if looping_url in parsed_url.path: url = None # TODO?HACK: can't use urllib.quote_plus because mod_rewrite quotes the # next url value already. if url: url = url.replace(" ", "+") return url
def from_url(cls, url, id_only=False): """Returns the question that the URL represents. If the question doesn't exist or the URL isn't a question URL, this returns None. If id_only is requested, we just return the question id and we don't validate the existence of the question (this saves us from making a million or so db calls). """ parsed = urlparse(url) locale, path = split_path(parsed.path) path = '/' + path try: view, view_args, view_kwargs = resolve(path) except Http404: return None import questions.views # Views import models; models import views. if view != questions.views.answers: return None question_id = view_kwargs['question_id'] if id_only: return int(question_id) try: question = cls.objects.get(id=question_id) except cls.DoesNotExist: return None return question
def redirect_url(self, source_locale=settings.LANGUAGE_CODE): """If I am a redirect, return the URL to which I redirect. Otherwise, return None. """ # If a document starts with REDIRECT_HTML and contains any <a> tags # with hrefs, return the href of the first one. This trick saves us # from having to parse the HTML every time. if self.html.startswith(REDIRECT_HTML): anchors = PyQuery(self.html)('a[href]') if anchors: # Articles with a redirect have a link that has the locale # hardcoded into it, and so by simply redirecting to the given # link, we end up possibly losing the locale. So, instead, # we strip out the locale and replace it with the original # source locale only in the case where an article is going # from one locale and redirecting it to a different one. # This only applies when it's a non-default locale because we # don't want to override the redirects that are forcibly # changing to (or staying within) a specific locale. full_url = anchors[0].get('href') (dest_locale, url) = split_path(full_url) if (source_locale != dest_locale and dest_locale == settings.LANGUAGE_CODE): return '/' + source_locale + '/' + url return full_url
def process_request(self, request): prefixer = Prefixer(request) set_url_prefixer(prefixer) full_path = prefixer.fix(prefixer.shortened_path) if 'lang' in request.GET: # Blank out the locale so that we can set a new one. Remove lang # from the query params so we don't have an infinite loop. prefixer.locale = '' new_path = prefixer.fix(prefixer.shortened_path) query = dict((smart_str(k), v) for k, v in request.GET.iteritems() if k != 'lang') return HttpResponsePermanentRedirect(urlparams(new_path, **query)) if full_path != request.path: query_string = request.META.get('QUERY_STRING', '') full_path = urllib.quote(full_path.encode('utf-8')) if query_string: full_path = '%s?%s' % (full_path, query_string) response = HttpResponsePermanentRedirect(full_path) # Vary on Accept-Language if we changed the locale old_locale = prefixer.locale new_locale, _ = split_path(full_path) if old_locale != new_locale: response['Vary'] = 'Accept-Language' return response request.path_info = '/' + prefixer.shortened_path request.locale = prefixer.locale tower.activate(prefixer.locale)
def _doc_components_from_url(url, required_locale=None, check_host=True): """Return (locale, path, slug) if URL is a Document, False otherwise. If URL doesn't even point to the document view, raise _NotDocumentView. """ # Extract locale and path from URL: parsed = urlparse(url) # Never has errors AFAICT if check_host and parsed.netloc: return False locale, path = split_path(parsed.path) if required_locale and locale != required_locale: return False path = "/" + path try: view, view_args, view_kwargs = resolve(path) except Http404: return False import wiki.views # Views import models; models import views. if view != wiki.views.document: raise _NotDocumentView return locale, path, view_kwargs["document_slug"]
def process_request(self, request): prefixer = Prefixer(request) set_url_prefixer(prefixer) full_path = prefixer.fix(prefixer.shortened_path) if "lang" in request.GET: # Blank out the locale so that we can set a new one. Remove lang # from the query params so we don't have an infinite loop. prefixer.locale = "" new_path = prefixer.fix(prefixer.shortened_path) query = dict((smart_str(k), v) for k, v in request.GET.iteritems() if k != "lang") return HttpResponseRedirect(urlparams(new_path, **query)) if full_path != request.path: query_string = request.META.get("QUERY_STRING", "") full_path = urllib.quote(full_path.encode("utf-8")) if query_string: full_path = "%s?%s" % (full_path, query_string) response = HttpResponseRedirect(full_path) # Vary on Accept-Language if we changed the locale old_locale = prefixer.locale new_locale, _ = split_path(full_path) if old_locale != new_locale: response["Vary"] = "Accept-Language" return response request.path_info = "/" + prefixer.shortened_path request.LANGUAGE_CODE = prefixer.locale tower.activate(prefixer.locale)
def request(self, **request): """Make a request, but prepend a locale if there isn't one already.""" # Fall back to defaults as in the superclass's implementation: path = request.get("PATH_INFO", self.defaults.get("PATH_INFO", "/")) locale, shortened = split_path(path) if not locale: request["PATH_INFO"] = "/%s/%s" % (settings.LANGUAGE_CODE, shortened) return super(LocalizingClient, self).request(**request)
def request(self, **request): """Make a request, but prepend a locale if there isn't one already.""" # Fall back to defaults as in the superclass's implementation: path = request.get('PATH_INFO', self.defaults.get('PATH_INFO', '/')) locale, shortened = split_path(path) if not locale: request['PATH_INFO'] = '/%s/%s' % (settings.LANGUAGE_CODE, shortened) return super(LocalizingClient, self).request(**request)
def _clean_next_url(request): if 'next' in request.POST: url = request.POST.get('next') elif 'next' in request.GET: url = request.GET.get('next') elif 'HTTP_REFERER' in request.META: url = request.META.get('HTTP_REFERER').decode('latin1', 'ignore') else: url = None if url: parsed_url = urlparse.urlparse(url) # Don't redirect outside of site_domain. # Don't include protocol+domain, so if we are https we stay that way. # http://bugzil.la/847190 relative_url_prefix = urllib.quote_plus(urllib.quote_plus('//')) if url.upper().startswith(relative_url_prefix): url = None if parsed_url.scheme: site_domain = Site.objects.get_current().domain url_domain = parsed_url.netloc if site_domain != url_domain: url = None else: url = u'?'.join([getattr(parsed_url, x) for x in ('path', 'query') if getattr(parsed_url, x)]) # Don't redirect right back to login, logout, register, or change email # pages locale, register_url = split_path(reverse('users.browserid_register')) locale, change_email_url = split_path( reverse('users.change_email')) for looping_url in [settings.LOGIN_URL, settings.LOGOUT_URL, register_url, change_email_url]: if looping_url in parsed_url.path: url = None # TODO?HACK: can't use urllib.quote_plus because mod_rewrite quotes the # next url value already. if url: url = url.replace(' ', '+') return url
def _clean_next_url(request): if 'next' in request.POST: url = request.POST.get('next') elif 'next' in request.GET: url = request.GET.get('next') elif 'HTTP_REFERER' in request.META: url = request.META.get('HTTP_REFERER').decode('latin1', 'ignore') else: url = None if url: parsed_url = urlparse.urlparse(url) # Don't redirect outside of site_domain. # Don't include protocol+domain, so if we are https we stay that way. if parsed_url.scheme: site_domain = Site.objects.get_current().domain url_domain = parsed_url.netloc if site_domain != url_domain: url = None else: url = u'?'.join([ getattr(parsed_url, x) for x in ('path', 'query') if getattr(parsed_url, x) ]) # Don't redirect right back to login, logout, register, or change email # pages locale, register_url = split_path(reverse('users.browserid_register')) locale, change_email_url = split_path(reverse('users.change_email')) for looping_url in [ settings.LOGIN_URL, settings.LOGOUT_URL, register_url, change_email_url ]: if looping_url in parsed_url.path: url = None # TODO?HACK: can't use urllib.quote_plus because mod_rewrite quotes the # next url value already. if url: url = url.replace(' ', '+') return url
def process_request(self, request): prefixer = Prefixer(request) set_url_prefixer(prefixer) full_path = prefixer.fix(prefixer.shortened_path) if 'lang' in request.GET: # Blank out the locale so that we can set a new one. Remove lang # from the query params so we don't have an infinite loop. prefixer.locale = '' new_path = prefixer.fix(prefixer.shortened_path) query = dict((smart_str(k), v) for k, v in request.GET.iteritems() if k != 'lang') # 'lang' is only used on the language selection page. If this is # present it is safe to set language preference for the current # user. if request.user.is_anonymous(): cookie = settings.LANGUAGE_COOKIE_NAME request.session[cookie] = request.GET['lang'] return HttpResponseRedirect(urlparams(new_path, **query)) if full_path != request.path: query_string = request.META.get('QUERY_STRING', '') full_path = urllib.quote(full_path.encode('utf-8')) if query_string: full_path = '%s?%s' % (full_path, query_string) response = HttpResponseRedirect(full_path) # Vary on Accept-Language if we changed the locale old_locale = prefixer.locale new_locale, _ = split_path(full_path) if old_locale != new_locale: response['Vary'] = 'Accept-Language' return response request.path_info = '/' + prefixer.shortened_path request.LANGUAGE_CODE = prefixer.locale tower.activate(prefixer.locale)
def from_url(url, required_locale=None, id_only=False): """Return the approved Document the URL represents, None if there isn't one. Return None if the URL is a 404, the URL doesn't point to the right view, or the indicated document doesn't exist. To limit the universe of discourse to a certain locale, pass in a `required_locale`. To fetch only the ID of the returned Document, set `id_only` to True. """ # Extract locale and path from URL: path = urlparse(url)[2] # never has errors AFAICT locale, path = split_path(path) if required_locale and locale != required_locale: return None path = '/' + path try: view, view_args, view_kwargs = resolve(path) except Http404: return None import wiki.views # Views import models; models import views. if view != wiki.views.document: return None # Map locale-slug pair to Document ID: doc_query = Document.objects.exclude(current_revision__isnull=True) if id_only: doc_query = doc_query.only('id') try: return doc_query.get( locale=locale, slug=view_kwargs['document_slug']) except Document.DoesNotExist: return None
def _doc_components_from_url(url, required_locale=None, check_host=True): """Return (locale, path, slug) if URL is a Document, False otherwise. If URL doesn't even point to the document view, raise _NotDocumentView. """ # Extract locale and path from URL: parsed = urlparse(url) # Never has errors AFAICT if check_host and parsed.netloc: return False locale, path = split_path(parsed.path) if required_locale and locale != required_locale: return False path = '/' + path try: view, view_args, view_kwargs = resolve(path) except Http404: return False import wiki.views # Views import models; models import views. if view != wiki.views.document: raise _NotDocumentView return locale, path, view_kwargs['document_slug']
def get_soapbox_messages(url): _, path = split_path(url) return Message.objects.match(path)