Beispiel #1
0
def replace(match):
    url = match.group('url')
    kind = match.group('kind')
    external_task = (match.group('kind') == 'externaltask')
    try:
        url_validate(url)
    except ValidationError:
        return '[%s:Invalid Url]' % kind
    for prefix in settings.P2PU_EMBEDS:
        if url.startswith(prefix):
            return render_to_string('richtext/_p2pu_embed.html', {'url': url})
    if not external_task:
        expiration_date = datetime.now() - settings.EMBEDLY_CACHE_EXPIRES
        embedded_urls = EmbeddedUrl.objects.filter(original_url=url,
            created_on__gte=expiration_date).order_by('-created_on')
        embedded_url = None
        if embedded_urls.exists():
            embedded_url = embedded_urls[0]
        else:
            embedly_key = getattr(settings, 'EMBEDLY_KEY', False)
            if embedly_key:
                client = Embedly(embedly_key)
                obj = client.oembed(url, maxwidth=460)
                embedded_url = EmbeddedUrl(original_url=obj.original_url,
                    html=(obj.html or ''), extra_data=obj.dict)
                embedded_url.save()
        if embedded_url and embedded_url.html:
            return embedded_url.html
    context = {'url': url, 'external_task': external_task}
    return render_to_string('richtext/_external_link.html', context)
Beispiel #2
0
def replace(match):
    url = match.group('url')
    try:
        url_validate(url)
        expiration_date = datetime.now() - settings.EMBEDLY_CACHE_EXPIRES
        embedded_urls = EmbeddedUrl.objects.filter(
            original_url=url,
            created_on__gte=expiration_date).order_by('-created_on')
        embedded_url = None
        if embedded_urls.exists():
            embedded_url = embedded_urls[0]
        else:
            embedly_key = getattr(settings, 'EMBEDLY_KEY', False)
            if embedly_key:
                client = Embedly(embedly_key)
                obj = client.oembed(url, maxwidth=460)
                embedded_url = EmbeddedUrl(original_url=obj.original_url,
                                           html=(obj.html or ''),
                                           extra_data=obj.dict)
                embedded_url.save()
        if embedded_url and embedded_url.html:
            return embedded_url.html
    except ValidationError:
        return '[embed:Invalid Url]'
    return DEFAULT_EMBED % (url, url)
Beispiel #3
0
def replace(match):
    url = match.group('url')
    try:
        url_validate(url)
        if match.group('kind') == 'externaltask':
            return '<button class="external-task" data-url="%s">%s</button>' % (
                url, # TODO: Should we escape/encode this somehow?
                _('Start This Task')
                )
        expiration_date = datetime.now() - settings.EMBEDLY_CACHE_EXPIRES
        embedded_urls = EmbeddedUrl.objects.filter(original_url=url,
            created_on__gte=expiration_date).order_by('-created_on')
        embedded_url = None
        if embedded_urls.exists():
            embedded_url = embedded_urls[0]
        else:
            embedly_key = getattr(settings, 'EMBEDLY_KEY', False)
            if embedly_key:
                client = Embedly(embedly_key)
                obj = client.oembed(url, maxwidth=460)
                embedded_url = EmbeddedUrl(original_url=obj.original_url,
                    html=(obj.html or ''), extra_data=obj.dict)
                embedded_url.save()
        if embedded_url and embedded_url.html:
            return embedded_url.html
    except ValidationError:
        return '[embed:Invalid Url]'
    return DEFAULT_EMBED % (url, url)
Beispiel #4
0
def replace(match):
    url = match.group('url')
    try:
        url_validate(url)
        expiration_date = datetime.now() - settings.EMBEDLY_CACHE_EXPIRES
        embedded_urls = EmbeddedUrl.objects.filter(original_url=url,
            created_on__gte=expiration_date).order_by('-created_on')
        embedded_url = None
        if embedded_urls.exists():
            embedded_url = embedded_urls[0]
        else:
            embedly_key = getattr(settings, 'EMBEDLY_KEY', False)
            if embedly_key:
                client = Embedly(embedly_key)
                obj = client.oembed(url)
                embedded_url = EmbeddedUrl(original_url=obj.original_url,
                    html=obj.html, extra_data=obj.dict)
                embedded_url.save()
        if embedded_url and embedded_url.html:
            return embedded_url.html
    except ValidationError:
        return '[embed:Invalid Url]'
    return DEFAULT_EMBED % (url, url)