def get_oembed_property(oembed_url, json_property): """ Use it like this: {% url_string|get_oembed_property:'thumbnail_url' %} """ if not oembed_url in _oembed_objects: matches = StoredOEmbed.objects.filter(match=oembed_url) if matches: # Found it, happy path oembed = matches[0] else: # If not found, we will try to fetch it _ = replace(oembed_url) matches = StoredOEmbed.objects.filter(match=oembed_url) if matches: # Haven't been parsed before, but now is available oembed = matches[0] else: # Nothing to do, is not an oembed oembed = None _oembed_objects[oembed_url] = oembed if _oembed_objects[oembed_url]: return _oembed_objects[oembed_url].get_json(json_property) else: return ''
def oembed(input, args=''): ''' args - optional oembed max size argument args="640x480" - max_width = 640, max_height = 480 args="!640x..." - max_width = width = 640 args="...x!480" - max_height = height = 480 ''' if args: max_width, max_height = args.lower().split('x') if not (max_width and max_height): raise template.TemplateSyntaxError( "Oembed's optional WIDTHxHEIGHT argument requires WIDTH and HEIGHT to be positive integers." ) if max_width[0] == '!': max_width = max_width[1:] fixed_width = True else: fixed_width = False if max_height[0] == '!': max_height = max_height[1:] fixed_height = True else: fixed_height = False else: max_width, max_height = None, None fixed_width, fixed_height = False, False return replace(input, max_width=max_width, max_height=max_height, fixed_width=fixed_width, fixed_height=fixed_height)
def testManySameEmbeds(self): loc = self.locs[1] embed = self.get_oembed(loc) text = " ".join([self.middle % loc] * 100) resp = " ".join([self.middle % embed] * 100) self.assertEquals(replace(text), resp)
def render(self): import bleach from oembed.core import replace # Clean self.content = bleach.clean(self.content) # Embed videos try: self.content = replace(self.content, max_width=527, fixed_width=527) except: pass # Linkify self.content = bleach.linkify( self.content, target='_blank', filter_url=add_http) ctype = ContentType.objects.get_for_model(self) images = Image.objects.filter(owner_type=ctype, owner_id=self.id) images_div = '' if len(images) > 0: c = {'images': images} render_images = render_to_string('post/_post_images.html', c) images_div = "<div class='image_container feed'>{0}</div>".format( render_images) return mark_safe("<a href='%s'>%s</a><br />" "<div class='post_content'> %s</div>%s" % (self.user.get_absolute_url(), self.user.get_full_name(), self.content, images_div))
def render(self): import bleach from oembed.core import replace # Clean self.content = bleach.clean(self.content) # Embed videos self.content = replace(self.content, max_width=527, fixed_width=527) # Linkify self.content = bleach.linkify( self.content, target='_blank', filter_url=add_http) # attached images ctype = ContentType.objects.get_for_model(self) images = Image.objects.filter(owner_type=ctype, owner_id=self.id) images_div = '' if len(images) > 0: c = {'images': images} render_images = render_to_string('post/_post_images.html', c) images_div = "<div class='image_container feed'>{0}</div>".format( render_images) post_template = render_to_string('post/_discpost.html', {'user': self.user, 'content': mark_safe(self.content), 'images_div': images_div, }) # replace last linebreak post_template = post_template.strip() return post_template
def get_context_data(self, **kwargs): data = super(ToolbarViewItem, self).get_context_data(**kwargs) item = self.item request = self.request data["comment_form"] = ReviewForm() if request.user.is_authenticated(): review = get_object_or_None( Review, content_type=self.content_type, object_id=item.id, user=request.user, ) if review: data["comment_form"] = ReviewForm(instance=review) if self.YOUTUBE_URL_RE.match(item.url): oembed = replace(item.url, 600, 450) if oembed != item.url: # Add wmode=transparent to youtube iframe to fix z-index oembed = re.sub(r'src="(http://www.youtube\.com.+?)"', r'src="\1&wmode=transparent"', oembed) data["oembed"] = oembed return data
def oembed(input, args=''): ''' args - optional oembed max size argument args="640x480" - max_width = 640, max_height = 480 args="!640x..." - max_width = width = 640 args="...x!480" - max_height = height = 480 ''' if args: max_width, max_height = args.lower().split('x') if not (max_width and max_height): raise template.TemplateSyntaxError("Oembed's optional WIDTHxHEIGHT argument requires WIDTH and HEIGHT to be positive integers.") if max_width[0] == '!': max_width = max_width[1:] fixed_width = True else: fixed_width = False if max_height[0] == '!': max_height = max_height[1:] fixed_height = True else: fixed_height = False else: max_width, max_height = None, None fixed_width, fixed_height = False, False return replace(input, max_width=max_width, max_height=max_height, fixed_width=fixed_width, fixed_height=fixed_height)
def render(self, context): kwargs = {} if self.width and self.height: kwargs['max_width'] = self.width kwargs['max_height'] = self.height if self.simple: kwargs['simple'] = self.simple return replace(self.nodelist.render(context), **kwargs)
def render(self, context): kwargs = {} if self.width and self.height: kwargs['max_width'] = self.width kwargs['max_height'] = self.height kwargs['template_dir'] = self.template_dir kwargs['context'] = context return replace(self.nodelist.render(context), **kwargs)
def oembed(input, args): if args: width, height = args.lower().split('x') if not width and height: raise template.TemplateSyntaxError("Oembed's optional WIDTHxHEIGH" \ "T argument requires WIDTH and HEIGHT to be positive integers.") else: width, height = None, None return replace(input, max_width=width, max_height=height)
def render(self, context): kwargs = {} if self.max_width and self.max_height: kwargs['max_width'] = self.max_width kwargs['max_height'] = self.max_height kwargs['fixed_width'] = self.fixed_width kwargs['fixed_height'] = self.fixed_height return replace(self.nodelist.render(context), **kwargs)
def testEnd(self): for loc in self.locs: embed = self.get_oembed(loc) if not embed or embed == loc: self.fail("URL: %s did not produce an embed object" % loc) for text in (self.end, self.start, self.middle, self.trailing_comma, self.trailing_period): self.assertEquals(replace(text % loc), text % embed)
def oembed(input, args=None): if args: try: width, height = map(int, args.lower().split('x')) except ValueError: raise template.TemplateSyntaxError("Oembed's optional " \ "WIDTHxHEIGHT argument requires WIDTH and HEIGHT to be " \ "positive integers.") else: width, height = None, None return replace(input, max_width=width, max_height=height)
def testEnd(self): for loc in self.locs: embed = self.get_oembed(loc) if not embed or embed == loc: self.fail("URL: %s did not produce an embed object" % loc) for text in (self.end, self.start, self.middle, self.trailing_comma, self.trailing_period): self.assertEquals( replace(text % loc), text % embed )
def return_embeds(post, position): embeds = post.embeds.filter(position=str(position)) text = '' for e in embeds: if e.music_embed: text += '\n' + e.music_embed.get_html() else: text += e.body + '\n' text = replace(text) return text
def return_embeds(post, position): embeds = post.embeds.filter(position = str(position)) text = '' for e in embeds: if e.music_embed: text += '\n' + e.music_embed.get_html() else: text += e.body + '\n' text = replace(text) return text
def get_oembed_thumbnail_url(oembed_url): # could refactor into get_oembed_<property_name> matches = StoredOEmbed.objects.filter(match=oembed_url).exclude(json='') # exclude blank json for backward compatibility without flushing table if not matches: _ = replace(oembed_url) matches = StoredOEmbed.objects.filter( match=oembed_url).exclude(json='') try: return matches[0].get_json('thumbnail_url') except IndexError: # oh dear, something is seriously wrong here if settings.DEBUG: raise RuntimeError( "StoredOEmbeds aren't gettings stored correctly!?") else: return '#oembed-failure' # fail silently-ish
def testNoEmbed(self): self.assertEquals( replace(self.noembed), self.noembed )
def get_oembed(self, url): try: return replace('%s' % url) except Exception, e: self.fail("URL: %s failed for this reason: %s" % (url, str(e)))
def render(self, context): kwargs = {} if self.width and self.height: kwargs["max_width"] = self.width kwargs["max_height"] = self.height return replace(self.nodelist.render(context), **kwargs)
def testManySameEmbeds(self): text = " ".join([self.middle % self.loc] * 100) resp = " ".join([self.middle % self.embed] * 100) self.assertEquals(replace(text), resp)
def testEnd(self): for text in (self.end, self.start, self.middle, self.trailing_comma, self.trailing_period): self.assertEquals( replace(text % self.loc), text % self.embed )
def testNoEmbed(self): fetch_count = fetch.count for noembed in self.noembeds: self.assertEquals(replace(noembed), noembed) self.assertEquals(fetch.count, fetch_count)
def oembed(input): return replace(input)