Example #1
0
    def request_resource(self, url, **kwargs):
        maxwidth = kwargs.get('maxwidth', None)
        maxheight = kwargs.get('maxheight', None)

        # calculate the appropriate width and height
        w, h = size_to_nearest(maxwidth, maxheight, self.MAP_SIZES, True)

        # prepare the dictionary of data to be returned as an oembed resource
        data = {
            'type': 'rich',
            'provider_name': 'Google',
            'version': '1.0',
            'width': w,
            'height': h,
            'title': '',
            'author_name': '',
            'author_url': ''
        }

        url_params = re.match(self.regex, url).groups()[0]
        url_params = url_params.replace('&', '&').split('&')

        map_params = ['output=embed']

        for param in url_params:
            k, v = param.split('=', 1)
            if k in self.VALID_PARAMS:
                map_params.append(param)

        data['html'] = '<iframe width="%d" height="%d" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?%s"></iframe>' % \
            (w, h, '&amp;'.join(map_params))

        return OEmbedResource.create(data)
Example #2
0
 def request_resource(self, url, **kwargs):
     maxwidth = kwargs.get('maxwidth', None)
     maxheight = kwargs.get('maxheight', None)
     
     # calculate the appropriate width and height
     w, h = size_to_nearest(maxwidth, maxheight, self.MAP_SIZES, True)
     
     # prepare the dictionary of data to be returned as an oembed resource
     data = {
         'type': 'rich', 'provider_name': 'Google', 'version': '1.0',
         'width': w, 'height': h, 'title': '', 'author_name': '',
         'author_url': ''
     }
     
     url_params = re.match(self.regex, url).groups()[0]
     url_params = url_params.replace('&amp;', '&').split('&')
     
     map_params = ['output=embed']
     
     for param in url_params:
         k, v = param.split('=', 1)
         if k in self.VALID_PARAMS:
             map_params.append(param)
     
     data['html'] = '<iframe width="%d" height="%d" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/maps?%s"></iframe>' % \
         (w, h, '&amp;'.join(map_params))
     
     return OEmbedResource.create(data)
 def thumbnail(self, obj, mapping):
     url, width, height = self.resize(
         self.get_image(obj),
         *size_to_nearest(allowed_sizes=self._meta.thumbnail_sizes))
     mapping.update(thumbnail_url=url,
                    thumbnail_width=width,
                    thumbnail_height=height)
Example #4
0
    def request_resource(self, url, **kwargs):
        maxwidth = kwargs.get('maxwidth', None)
        maxheight = kwargs.get('maxheight', None)

        # calculate the appropriate bounds for width and height
        w, h = size_to_nearest(maxwidth, maxheight, self.IMAGE_SIZES, True)

        # get the path, i.e. /media/img/kitties.jpg
        image_path = re.match(self.regex, url).groups()[0]

        # create the entire url as it would be on site, minus the filename
        base_url, ext = url.rsplit('.', 1)

        # create the file path minus the extension
        base_path, ext = image_path.rsplit('.', 1)

        append = '_%sx%s.%s' % (w, h, ext)

        new_path = '%s%s' % (base_path, append)

        if not default_storage.exists(new_path):
            # open the original to calculate its width and height
            source_file = default_storage.open(image_path)
            img = Image.open(source_file)

            # retrieve image format and dimensions
            format = img.format
            img_width, img_height = img.size

            # do the math-y parts
            new_width, new_height = scale(img_width, img_height, w, h)

            img = img.resize((new_width, new_height), Image.ANTIALIAS)

            img_buffer = StringIO()
            img.MAXBLOCK = 1024 * 1024
            img.save(img_buffer, format=format)

            source_file.close()
            default_storage.save(new_path, ContentFile(img_buffer.getvalue()))

        new_url = '%s%s' % (base_url, append)

        # get just the filename, i.e. test.jpg - used for generated the title
        # of the returned oembed resource
        image_filename = image_path.rsplit('/', 1)[-1]

        data = {
            'type': 'photo',
            'provider_name': '',
            'version': '1.0',
            'width': w,
            'height': h,
            'title': image_filename,
            'url': new_url,
            'author_name': '',
            'author_url': ''
        }

        return OEmbedResource.create(data)
Example #5
0
 def map_to_dictionary(self, url, obj, **kwargs):
     """
     Build a dictionary of metadata for the requested object.
     """
     maxwidth = kwargs.get('maxwidth', None)
     maxheight = kwargs.get('maxheight', None)
     
     provider_url, provider_name = self.provider_from_url(url)
     
     mapping = {
         'version': '1.0',
         'url': url,
         'provider_name': provider_name,
         'provider_url': provider_url,
         'type': self.resource_type
     }
     
     # a hook
     self.preprocess(obj, mapping, **kwargs)
     
     # resize image if we have a photo, otherwise use the given maximums
     if self.resource_type == 'photo' and self.get_image(obj):
         self.resize_photo(obj, mapping, maxwidth, maxheight)
     elif self.resource_type in ('html', 'rich', 'photo'):
         width, height = size_to_nearest(
             maxwidth,
             maxheight,
             self._meta.valid_sizes,
             self._meta.force_fit
         )
         mapping.update(width=width, height=height)
     
     # create a thumbnail
     if self.get_image(obj):
         self.thumbnail(obj, mapping)
     
     # map attributes to the mapping dictionary.  if the attribute is
     # a callable, it must have an argument signature of
     # (self, obj)
     for attr in ('title', 'author_name', 'author_url', 'html'):
         self.map_attr(mapping, attr, obj)
     
     # fix any urls
     if 'url' in mapping:
         mapping['url'] = relative_to_full(mapping['url'], url)
     
     if 'thumbnail_url' in mapping:
         mapping['thumbnail_url'] = relative_to_full(mapping['thumbnail_url'], url)
     
     if 'html' not in mapping and mapping['type'] in ('video', 'rich'):
         mapping['html'] = self.render_html(obj, context=Context(mapping))
     
     # a hook
     self.postprocess(obj, mapping, **kwargs)
     
     return mapping
Example #6
0
 def map_to_dictionary(self, url, obj, **kwargs):
     """
     Build a dictionary of metadata for the requested object.
     """
     maxwidth = kwargs.get('maxwidth', None)
     maxheight = kwargs.get('maxheight', None)
     
     provider_url, provider_name = self.provider_from_url(url)
     
     mapping = {
         'version': '1.0',
         'url': url,
         'provider_name': provider_name,
         'provider_url': provider_url,
         'type': self.resource_type
     }
     
     # a hook
     self.preprocess(obj, mapping, **kwargs)
     
     # resize image if we have a photo, otherwise use the given maximums
     if self.resource_type == 'photo' and self.get_image(obj):
         self.resize_photo(obj, mapping, maxwidth, maxheight)
     elif self.resource_type in ('video', 'rich', 'photo'):
         width, height = size_to_nearest(
             maxwidth,
             maxheight,
             self._meta.valid_sizes,
             self._meta.force_fit
         )
         mapping.update(width=width, height=height)
     
     # create a thumbnail
     if self.get_image(obj):
         self.thumbnail(obj, mapping)
     
     # map attributes to the mapping dictionary.  if the attribute is
     # a callable, it must have an argument signature of
     # (self, obj)
     for attr in ('title', 'author_name', 'author_url', 'html'):
         self.map_attr(mapping, attr, obj)
     
     # fix any urls
     if 'url' in mapping:
         mapping['url'] = relative_to_full(mapping['url'], url)
     
     if 'thumbnail_url' in mapping:
         mapping['thumbnail_url'] = relative_to_full(mapping['thumbnail_url'], url)
     
     if 'html' not in mapping and mapping['type'] in ('video', 'rich'):
         mapping['html'] = self.render_html(obj, context=Context(mapping))
     
     # a hook
     self.postprocess(obj, mapping, **kwargs)
     
     return mapping
Example #7
0
    def request_resource(self, url, **kwargs):
        maxwidth = kwargs.get('maxwidth', None)
        maxheight = kwargs.get('maxheight', None)

        # calculate the appropriate bounds for width and height
        w, h = size_to_nearest(maxwidth, maxheight, self.IMAGE_SIZES, True)

        # get the path, i.e. /media/img/kitties.jpg
        image_path = re.match(self.regex, url).groups()[0]

        # create the entire filename as it would be on disk
        filename = settings.MEDIA_ROOT + image_path

        # create the entire url as it would be on site, minus the filename
        base_url, ext = url.rsplit('.', 1)

        # create the file path on disk minus the extension
        base_file, ext = filename.rsplit('.', 1)

        append = '_%sx%s.%s' % (w, h, ext)

        new_filename = '%s%s' % (base_file, append)

        if not os.path.isfile(new_filename):
            # open the original to calculate its width and height
            img = Image.open(filename)
            img_width, img_height = img.size

            # do the math-y parts
            new_width, new_height = scale(img_width, img_height, w, h)

            img = img.resize((new_width, new_height), Image.ANTIALIAS)
            img.save(new_filename)

        new_url = '%s%s' % (base_url, append)

        # get just the filename, i.e. test.jpg - used for generated the title
        # of the returned oembed resource
        image_filename = image_path.rsplit('/', 1)[1]

        data = {
            'type': 'photo',
            'provider_name': '',
            'version': '1.0',
            'width': w,
            'height': h,
            'title': image_filename,
            'url': new_url,
            'author_name': '',
            'author_url': ''
        }

        return OEmbedResource.create(data)
Example #8
0
    def request_resource(self, url, **kwargs):
        maxwidth = kwargs.get('maxwidth', None)
        maxheight = kwargs.get('maxheight', None)
        
        # calculate the appropriate bounds for width and height
        w, h = size_to_nearest(maxwidth, maxheight, self.IMAGE_SIZES, True)

        # get the path, i.e. /media/img/kitties.jpg
        image_path = re.match(self.regex, url).groups()[0]
        
        # create the entire url as it would be on site, minus the filename
        base_url, ext = url.rsplit('.', 1)
                
        # create the file path minus the extension
        base_path, ext = image_path.rsplit('.', 1)
        
        append = '_%sx%s.%s' % (w, h, ext)
        
        new_path = '%s%s' % (base_path, append)
        
        if not default_storage.exists(new_path):
            # open the original to calculate its width and height
            source_file = default_storage.open(image_path)
            img = Image.open(source_file)

            # retrieve image format and dimensions
            format = img.format
            img_width, img_height = img.size

            # do the math-y parts
            new_width, new_height = scale(img_width, img_height, w, h)
            
            img = img.resize((new_width, new_height), Image.ANTIALIAS)

            img_buffer = StringIO()
            img.MAXBLOCK = 1024*1024
            img.save(img_buffer, format=format)

            source_file.close()
            default_storage.save(new_path, ContentFile(img_buffer.getvalue()))
        
        new_url = '%s%s' % (base_url, append)
        
        # get just the filename, i.e. test.jpg - used for generated the title
        # of the returned oembed resource
        image_filename = image_path.rsplit('/', 1)[-1]
        
        data = {'type': 'photo', 'provider_name': '', 'version': '1.0',
                'width': w, 'height': h, 'title': image_filename,
                'url': new_url, 'author_name': '', 'author_url': ''}
        
        return OEmbedResource.create(data)
Example #9
0
    def test_size_to_nearest_defaults(self):
        self.assertEqual((800, 600), size_to_nearest(800, 600))
        self.assertEqual((800, 600), size_to_nearest(850, 650))

        self.assertEqual((800, 300), size_to_nearest(None, 350))
        self.assertEqual((400, 800), size_to_nearest(450, None))

        self.assertEqual((800, 300), size_to_nearest(None, 350))
        self.assertEqual((400, 800), size_to_nearest(450, None))
        
        self.assertEqual((200, 200), size_to_nearest(400, 250, force_fit=True))
Example #10
0
    def test_size_to_nearest_defaults(self):
        self.assertEqual((800, 600), size_to_nearest(800, 600))
        self.assertEqual((800, 600), size_to_nearest(850, 650))

        self.assertEqual((800, 300), size_to_nearest(None, 350))
        self.assertEqual((400, 800), size_to_nearest(450, None))

        self.assertEqual((800, 300), size_to_nearest(None, 350))
        self.assertEqual((400, 800), size_to_nearest(450, None))
        
        self.assertEqual((200, 200), size_to_nearest(400, 250, force_fit=True))
Example #11
0
    def map_to_dictionary(self, url, obj, **kwargs):
        """
        Build a dictionary of metadata for the requested object.
        """
        maxwidth = kwargs.get("maxwidth", None)
        maxheight = kwargs.get("maxheight", None)

        provider_url, provider_name = self.provider_from_url(url)

        mapping = {
            "version": "1.0",
            "url": url,
            "provider_name": provider_name,
            "provider_url": provider_url,
            "type": self.resource_type,
        }

        # a hook
        self.preprocess(obj, mapping, **kwargs)

        # resize image if we have a photo, otherwise use the given maximums
        if self.resource_type == "photo" and self.get_image(obj):
            self.resize_photo(obj, mapping, maxwidth, maxheight)
        elif self.resource_type in ("video", "rich", "photo"):
            width, height = size_to_nearest(maxwidth, maxheight, self._meta.valid_sizes, self._meta.force_fit)
            mapping.update(width=width, height=height)

        # create a thumbnail
        if self.get_image(obj):
            self.thumbnail(obj, mapping)

        # map attributes to the mapping dictionary.  if the attribute is
        # a callable, it must have an argument signature of
        # (self, obj)
        for attr in ("title", "author_name", "author_url", "html"):
            self.map_attr(mapping, attr, obj)

        # fix any urls
        if "url" in mapping:
            mapping["url"] = relative_to_full(mapping["url"], url)

        if "thumbnail_url" in mapping:
            mapping["thumbnail_url"] = relative_to_full(mapping["thumbnail_url"], url)

        if "html" not in mapping and mapping["type"] in ("video", "rich"):
            mapping["html"] = self.render_html(obj, context=Context(mapping))

        # a hook
        self.postprocess(obj, mapping, **kwargs)

        return mapping
Example #12
0
    def request_resource(self, url, **kwargs):
        maxwidth = kwargs.get('maxwidth', None)
        maxheight = kwargs.get('maxheight', None)
        
        # calculate the appropriate bounds for width and height
        w, h = size_to_nearest(maxwidth, maxheight, self.IMAGE_SIZES, True)

        # get the path, i.e. /media/img/kitties.jpg
        image_path = re.match(self.regex, url).groups()[0]
        
        # create the entire filename as it would be on disk
        filename = settings.MEDIA_ROOT + image_path
        
        # create the entire url as it would be on site, minus the filename
        base_url, ext = url.rsplit('.', 1)
                
        # create the file path on disk minus the extension
        base_file, ext = filename.rsplit('.', 1)
        
        append = '_%sx%s.%s' % (w, h, ext)
        
        new_filename = '%s%s' % (base_file, append)
        
        if not os.path.isfile(new_filename):
            # open the original to calculate its width and height
            img = Image.open(filename)
            img_width, img_height = img.size
            
            # do the math-y parts
            new_width, new_height = scale(img_width, img_height, w, h)
            
            img = img.resize((new_width, new_height), Image.ANTIALIAS)
            img.save(new_filename)
        
        new_url = '%s%s' % (base_url, append)
        
        # get just the filename, i.e. test.jpg - used for generated the title
        # of the returned oembed resource
        image_filename = image_path.rsplit('/', 1)[1]
        
        data = {'type': 'photo', 'provider_name': '', 'version': '1.0',
                'width': w, 'height': h, 'title': image_filename,
                'url': new_url, 'author_name': '', 'author_url': ''}
        
        return OEmbedResource.create(data)
Example #13
0
 def thumbnail(self, obj, mapping):
     url, width, height = self.resize(
         self.get_image(obj), 
         *size_to_nearest(allowed_sizes=self._meta.thumbnail_sizes))
     mapping.update(thumbnail_url=url, thumbnail_width=width, 
                    thumbnail_height=height)
Example #14
0
 def resize_photo(self, obj, mapping, maxwidth=None, maxheight=None):
     url, width, height = self.resize(
         self.get_image(obj), 
         *size_to_nearest(maxwidth, maxheight, self._meta.valid_sizes))
     mapping.update(url=url, width=width, height=height)
Example #15
0
    def test_size_to_nearest(self):
        sizes = ((100, 100), (200, 200), (300, 300))

        self.assertEqual((300, 200), size_to_nearest(400, 200, sizes, False))
        self.assertEqual((100, 100), size_to_nearest(100, 100, sizes, False))
        self.assertEqual((200, 300), size_to_nearest(250, 500, sizes, False))
        
        # if force_fit is False then jump to the largest on None
        self.assertEqual((100, 300), size_to_nearest(150, None, sizes, False))
        self.assertEqual((300, 100), size_to_nearest(None, 150, sizes, False))

        # if force_fit is True then scale to the nearest size for the one
        # that is defined
        self.assertEqual((100, 100), size_to_nearest(150, None, sizes, True))
        self.assertEqual((100, 100), size_to_nearest(None, 150, sizes, True))
        self.assertEqual((200, 200), size_to_nearest(220, None, sizes, True))
        self.assertEqual((200, 200), size_to_nearest(None, 220, sizes, True))


        # if both dimensions are None use the largest possible
        self.assertEqual((300, 300), size_to_nearest(None, None, sizes, False))
        self.assertEqual((300, 300), size_to_nearest(None, None, sizes, True))

        # if a dimension is too small scale it up to the minimum
        self.assertEqual((100, 300), size_to_nearest(50, 300, sizes, False))
        self.assertEqual((100, 100), size_to_nearest(50, 300, sizes, True))
        self.assertEqual((100, 300), size_to_nearest(50, None, sizes, False))
        self.assertEqual((100, 100), size_to_nearest(50, None, sizes, True))

        # test when things are too large
        self.assertEqual((200, 200), size_to_nearest(400, 200, sizes, True))
        self.assertEqual((100, 100), size_to_nearest(100, 100, sizes, True))
        self.assertEqual((200, 200), size_to_nearest(250, 500, sizes, True))
        
        # test Nones around the edges
        self.assertEqual((100, 100), size_to_nearest(100, None, sizes, True))
        self.assertEqual((100, 100), size_to_nearest(None, 100, sizes, True))
 def resize_photo(self, obj, mapping, maxwidth=None, maxheight=None):
     url, width, height = self.resize(
         self.get_image(obj),
         *size_to_nearest(maxwidth, maxheight, self._meta.valid_sizes))
     mapping.update(url=url, width=width, height=height)
Example #17
0
    def test_size_to_nearest(self):
        sizes = ((100, 100), (200, 200), (300, 300))

        self.assertEqual((300, 200), size_to_nearest(400, 200, sizes, False))
        self.assertEqual((100, 100), size_to_nearest(100, 100, sizes, False))
        self.assertEqual((200, 300), size_to_nearest(250, 500, sizes, False))
        
        self.assertEqual((100, 300), size_to_nearest(150, None, sizes, False))
        self.assertEqual((300, 100), size_to_nearest(None, 150, sizes, False))

        self.assertEqual((200, 200), size_to_nearest(400, 200, sizes, True))
        self.assertEqual((100, 100), size_to_nearest(100, 100, sizes, True))
        self.assertEqual((200, 200), size_to_nearest(250, 500, sizes, True))
        
        self.assertEqual((100, 100), size_to_nearest(100, None, sizes, True))
        self.assertEqual((100, 100), size_to_nearest(None, 100, sizes, True))
        
        self.assertEqual((800, 600), size_to_nearest(800, 600))
        self.assertEqual((800, 600), size_to_nearest(850, 650))
        self.assertEqual((800, 300), size_to_nearest(None, 350))
        self.assertEqual((400, 800), size_to_nearest(450, None))
        
        self.assertEqual((200, 200), size_to_nearest(400, 250, force_fit=True))
Example #18
0
    def test_size_to_nearest(self):
        sizes = ((100, 100), (200, 200), (300, 300))

        self.assertEqual((300, 200), size_to_nearest(400, 200, sizes, False))
        self.assertEqual((100, 100), size_to_nearest(100, 100, sizes, False))
        self.assertEqual((200, 300), size_to_nearest(250, 500, sizes, False))
        
        # if force_fit is False then jump to the largest on None
        self.assertEqual((100, 300), size_to_nearest(150, None, sizes, False))
        self.assertEqual((300, 100), size_to_nearest(None, 150, sizes, False))

        # if force_fit is True then scale to the nearest size for the one
        # that is defined
        self.assertEqual((100, 100), size_to_nearest(150, None, sizes, True))
        self.assertEqual((100, 100), size_to_nearest(None, 150, sizes, True))

        # if both dimensions are None use the largest possible
        self.assertEqual((300, 300), size_to_nearest(None, None, sizes, False))
        self.assertEqual((300, 300), size_to_nearest(None, None, sizes, True))

        # if a dimension is too small scale it up to the minimum
        self.assertEqual((100, 300), size_to_nearest(50, 300, sizes, False))
        self.assertEqual((100, 100), size_to_nearest(50, 300, sizes, True))
        self.assertEqual((100, 300), size_to_nearest(50, None, sizes, False))
        self.assertEqual((100, 100), size_to_nearest(50, None, sizes, True))

        # test when things are too large
        self.assertEqual((200, 200), size_to_nearest(400, 200, sizes, True))
        self.assertEqual((100, 100), size_to_nearest(100, 100, sizes, True))
        self.assertEqual((200, 200), size_to_nearest(250, 500, sizes, True))
        
        # test Nones around the edges
        self.assertEqual((100, 100), size_to_nearest(100, None, sizes, True))
        self.assertEqual((100, 100), size_to_nearest(None, 100, sizes, True))