Esempio n. 1
0
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 '' 
Esempio n. 2
0
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)
Esempio n. 3
0
 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)
Esempio n. 4
0
    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))
Esempio n. 5
0
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 ''
Esempio n. 6
0
    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
Esempio n. 7
0
    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
Esempio n. 8
0
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)
Esempio n. 9
0
 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)
Esempio n. 10
0
 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)
Esempio n. 11
0
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)
Esempio n. 12
0
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)
Esempio n. 13
0
    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)
Esempio n. 14
0
    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)
Esempio n. 15
0
    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)
Esempio n. 16
0
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)
Esempio n. 17
0
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)
Esempio n. 18
0
 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
             )
Esempio n. 19
0
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
Esempio n. 20
0
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
Esempio n. 21
0
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
Esempio n. 22
0
 def testNoEmbed(self):
     self.assertEquals(
         replace(self.noembed),
         self.noembed
     )
Esempio n. 23
0
 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)))
Esempio n. 24
0
 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)
Esempio n. 25
0
 def testManySameEmbeds(self):
     text = " ".join([self.middle % self.loc] * 100)
     resp = " ".join([self.middle % self.embed] * 100)
     self.assertEquals(replace(text), resp)
Esempio n. 26
0
 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
         )
Esempio n. 27
0
 def testManySameEmbeds(self):
     text = " ".join([self.middle % self.loc] * 100) 
     resp = " ".join([self.middle % self.embed] * 100)
     self.assertEquals(replace(text), resp)
Esempio n. 28
0
 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
         )
Esempio n. 29
0
 def testNoEmbed(self):
     fetch_count = fetch.count
     for noembed in self.noembeds:
         self.assertEquals(replace(noembed), noembed)
     self.assertEquals(fetch.count, fetch_count)
Esempio n. 30
0
def oembed(input):
    return replace(input)
Esempio n. 31
0
 def testNoEmbed(self):
     fetch_count = fetch.count
     for noembed in self.noembeds:
         self.assertEquals(replace(noembed), noembed)
     self.assertEquals(fetch.count, fetch_count)