Example #1
0
    def bitly_link(self):
        """Returns a mzl.la shortlink for this video, generating one if one
        doesn't exist.
        """
        if self.bitly_link_db:
            return self.bitly_link_db

        # Generate a URL, remove the locale (let the site handle redirecting
        # to the proper locale), and finally add the domain to the URL.
        url = reverse('flicks.videos.details', kwargs={'video_id': self.id})
        locale, url = split_path(url)
        url = absolutify(url)

        # Don't actually generate a shortlink if we're developing locally.
        if settings.DEV:
            bitly_link = None
        else:
            bitly_link = generate_bitly_link(url)

        # Fallback to long URL if needed.
        if bitly_link is None:
            return url
        else:
            Video.objects.filter(pk=self.pk).update(bitly_link_db=bitly_link)
            return bitly_link
Example #2
0
def render(request, template, context=None, **kwargs):
    """
    Same as django's render() shortcut, but with l10n template support.
    If used like this::

        return l10n_utils.render(request, 'myapp/mytemplate.html')

    ... this helper will render the following template::

        l10n/LANG/myapp/mytemplate.html

    if present, otherwise, it'll render the specified (en-US) template.
    """
    context = {} if context is None else context

    # Make sure we have a single template
    if isinstance(template, list):
        template = template[0]

    # Every template gets its own .lang file, so figure out what it is
    # and pass it in the context
    context['template'] = template
    context['langfile'] = get_lang_path(template)

    # Get the available translation list of the current page
    context['translations'] = translations_for_template(template)

    # Look for localized template if not default lang.
    if hasattr(request, 'locale') and request.locale != settings.LANGUAGE_CODE:

        # Redirect to one of the user's accept languages or the site's default
        # language (en-US) if the current locale not active
        if not template_is_active(template, get_locale(request)):
            matched = None

            for lang in get_accept_languages(request):
                if template_is_active(template, lang):
                    matched = lang
                    break

            response = HttpResponseRedirect('/' + '/'.join([
                matched or settings.LANGUAGE_CODE,
                split_path(request.get_full_path())[1]
            ]))

            # Add the Vary header to avoid wrong redirects due to a cache
            response['Vary'] = 'Accept-Language'

            return response

        localized_tmpl = '%s/templates/%s' % (request.locale, template)
        try:
            return django_render(request, localized_tmpl, context, **kwargs)
        except TemplateDoesNotExist:
            # If not found, just go on and try rendering the parent template.
            pass

    return django_render(request, template, context, **kwargs)
Example #3
0
 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)
Example #4
0
 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)
Example #5
0
def render(request, template, context=None, **kwargs):
    """
    Same as django's render() shortcut, but with l10n template support.
    If used like this::

        return l10n_utils.render(request, 'myapp/mytemplate.html')

    ... this helper will render the following template::

        l10n/LANG/myapp/mytemplate.html

    if present, otherwise, it'll render the specified (en-US) template.
    """
    context = {} if context is None else context

    # Make sure we have a single template
    if isinstance(template, list):
        template = template[0]

    # Every template gets its own .lang file, so figure out what it is
    # and pass it in the context
    context['langfile'] = get_lang_path(template)

    # Get the available translation list of the current page
    context['translations'] = get_translations(context['langfile'])

    # Look for localized template if not default lang.
    if hasattr(request, 'locale') and request.locale != settings.LANGUAGE_CODE:

        # Redirect to one of the user's accept languages or the site's default
        # language (en-US) if the current locale not active
        if not template_is_active(template, get_locale(request)):
            matched = None

            for lang in get_accept_languages(request):
                if template_is_active(template, lang):
                    matched = lang
                    break

            response = HttpResponseRedirect('/' + '/'.join([
                matched or settings.LANGUAGE_CODE,
                split_path(request.get_full_path())[1]
            ]))

            # Add the Vary header to avoid wrong redirects due to a cache
            response['Vary'] = 'Accept-Language'

            return response

        localized_tmpl = '%s/templates/%s' % (request.locale, template)
        try:
            return django_render(request, localized_tmpl, context, **kwargs)
        except TemplateDoesNotExist:
            # If not found, just go on and try rendering the parent template.
            pass

    return django_render(request, template, context, **kwargs)
Example #6
0
    def request(self, **request):
        """Make a request, ensuring it has a locale and a user agent."""
        # 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)
        if "HTTP_USER_AGENT" not in request and self.user_agent:
            request["HTTP_USER_AGENT"] = self.user_agent

        return super(LocalizingClient, self).request(**request)
Example #7
0
    def request(self, **request):
        """Make a request, ensuring it has a locale and a user agent."""
        # 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)
        if 'HTTP_USER_AGENT' not in request and self.user_agent:
            request['HTTP_USER_AGENT'] = self.user_agent

        return super(LocalizingClient, self).request(**request)
Example #8
0
def render(request, template, context=None, **kwargs):
    """
    Same as django's render() shortcut, but with l10n template support.
    If used like this::

        return l10n_utils.render(request, 'myapp/mytemplate.html')

    ... this helper will render the following template::

        l10n/LANG/myapp/mytemplate.html

    if present, otherwise, it'll render the specified (en-US) template.
    """
    context = {} if context is None else context

    # Make sure we have a single template
    if isinstance(template, list):
        template = template[0]

    # Every template gets its own .lang file, so figure out what it is
    # and pass it in the context
    context['langfile'] = get_lang_path(template)

    # Get the available translation list of the current page
    context['translations'] = get_translations(context['langfile'])

    # Look for localized template if not default lang.
    if hasattr(request, 'locale') and request.locale != settings.LANGUAGE_CODE:

        # redirect to default lang if locale not active
        if not (settings.DEV or
                lang_file_is_active(context['langfile'], request.locale)):
            return HttpResponseRedirect('/' + '/'.join([
                settings.LANGUAGE_CODE,
                split_path(request.get_full_path())[1]
            ]))

        localized_tmpl = '%s/templates/%s' % (request.locale, template)
        try:
            return django_render(request, localized_tmpl, context, **kwargs)
        except TemplateDoesNotExist:
            # If not found, just go on and try rendering the parent template.
            pass

    return django_render(request, template, context, **kwargs)
Example #9
0
def render(request, template, context=None, **kwargs):
    """
    Same as django's render() shortcut, but with l10n template support.
    If used like this::

        return l10n_utils.render(request, 'myapp/mytemplate.html')

    ... this helper will render the following template::

        l10n/LANG/myapp/mytemplate.html

    if present, otherwise, it'll render the specified (en-US) template.
    """
    context = {} if context is None else context

    # Every template gets its own .lang file, so figure out what it is
    # and pass it in the context
    context['langfile'] = get_lang_path(template)

    # Get the available translation list of the current page
    context['translations'] = get_translations(context['langfile'])

    # Look for localized template if not default lang.
    if hasattr(request, 'locale') and request.locale != settings.LANGUAGE_CODE:

        # redirect to default lang if locale not active
        if not (settings.DEV
                or lang_file_is_active(context['langfile'], request.locale)):
            return HttpResponseRedirect('/' + '/'.join([
                settings.LANGUAGE_CODE,
                split_path(request.get_full_path())[1]
            ]))

        localized_tmpl = '%s/templates/%s' % (request.locale, template)
        try:
            return django_render(request, localized_tmpl, context, **kwargs)
        except TemplateDoesNotExist:
            # If not found, just go on and try rendering the parent template.
            pass

    return django_render(request, template, context, **kwargs)
Example #10
0
def expire_page_cache(path, key_prefix=None):
    # pass the path through funfactory resolver in order to get locale
    resolved_path = resolve(path)
    path_with_locale = urlresolvers.reverse(
        resolved_path.func,
        args = resolved_path.args,
        kwargs = resolved_path.kwargs
    )
    try:
        language = urlresolvers.split_path(path_with_locale)[0].lower()
    except:
        language = None

    # get cache key, expire if the cached item exists
    key = get_url_cache_key(
        path_with_locale, language=language, key_prefix=key_prefix
    )

    if key:
        if cache.get(key):
            cache.set(key, None, 0)
        return True
    return False
Example #11
0
def render(request, template, context={}, **kwargs):
    """
    Same as django's render() shortcut, but with l10n template support.
    If used like this::

        return l10n_utils.render(request, 'myapp/mytemplate.html')

    ... this helper will render the following template::

        l10n/LANG/myapp/mytemplate.html

    if present, otherwise, it'll render the specified (en-US) template.
    """
    # Every template gets its own .lang file, so figure out what it is
    # and pass it in the context
    context['langfile'] = get_lang_path(template)

    # Look for localized template if not default lang.
    if request.locale != settings.LANGUAGE_CODE:

        # redirect to default lang if locale not active
        if not (settings.DEV or
                lang_file_is_active(context['langfile'], request.locale)):
            return HttpResponseRedirect('/' + '/'.join([
                settings.LANGUAGE_CODE,
                split_path(request.get_full_path())[1]
            ]))

        localized_tmpl = '%s/templates/%s' % (request.locale, template)
        try:
            return django_render(request, localized_tmpl, context, **kwargs)
        except TemplateNotFound:
            # If not found, just go on and try rendering the parent template.
            pass

    return django_render(request, template, context, **kwargs)
Example #12
0
def check_split_path(path, result):
    res = split_path(path)
    eq_(res, result)
Example #13
0
def check_split_path(path, result):
    res = split_path(path)
    eq_(res, result)