Пример #1
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
Пример #2
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
Пример #3
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
Пример #4
0
    def store_providers(self, provider_data):
        """
        Iterate over the returned json and try to sort out any new providers
        """
        if not hasattr(provider_data, '__iter__'):
            raise OEmbedException('Autodiscovered response not iterable')

        provider_pks = []

        for provider in provider_data:
            if 'endpoint' not in provider or \
               'matches' not in provider:
                continue

            resource_type = provider.get('type')
            if resource_type not in RESOURCE_TYPES:
                continue

            stored_provider, created = StoredProvider.objects.get_or_create(
                wildcard_regex=provider['matches']
            )

            if created:
                stored_provider.endpoint_url = relative_to_full(
                    provider['endpoint'],
                    provider['matches']
                )
                stored_provider.resource_type = resource_type
                stored_provider.save()

            provider_pks.append(stored_provider.pk)

        return StoredProvider.objects.filter(pk__in=provider_pks)
Пример #5
0
    def store_providers(self, provider_data):
        """
        Iterate over the returned json and try to sort out any new providers
        """
        if not hasattr(provider_data, '__iter__'):
            raise OEmbedException('Autodiscovered response not iterable')

        provider_pks = []

        for provider in provider_data:
            if 'endpoint' not in provider or \
               'matches' not in provider:
                continue

            resource_type = provider.get('type')
            if resource_type not in RESOURCE_TYPES:
                continue

            stored_provider, created = StoredProvider.objects.get_or_create(
                wildcard_regex=provider['matches'])

            if created:
                stored_provider.endpoint_url = relative_to_full(
                    provider['endpoint'], provider['matches'])
                stored_provider.resource_type = resource_type
                stored_provider.save()

            provider_pks.append(stored_provider.pk)

        return StoredProvider.objects.filter(pk__in=provider_pks)
Пример #6
0
 def test_relative_to_full(self):
     self.assertEqual('http://test.com/a/b/', relative_to_full('/a/b/', 'http://test.com'))
     self.assertEqual('http://test.com/a/b/', relative_to_full('/a/b/', 'http://test.com/c/d/?cruft'))
     self.assertEqual('http://test.com/a/b/', relative_to_full('http://test.com/a/b/', 'http://test.com'))
     self.assertEqual('http://blah.com/a/b/', relative_to_full('http://blah.com/a/b/', 'http://test.com'))
     self.assertEqual('/a/b/', relative_to_full('/a/b/', ''))
Пример #7
0
 def test_relative_to_full(self):
     self.assertEqual('http://test.com/a/b/', relative_to_full('/a/b/', 'http://test.com'))
     self.assertEqual('http://test.com/a/b/', relative_to_full('/a/b/', 'http://test.com/c/d/?cruft'))
     self.assertEqual('http://test.com/a/b/', relative_to_full('http://test.com/a/b/', 'http://test.com'))
     self.assertEqual('http://blah.com/a/b/', relative_to_full('http://blah.com/a/b/', 'http://test.com'))
     self.assertEqual('/a/b/', relative_to_full('/a/b/', ''))