Skip to content

Commit

Permalink
No longer supporting queryset as a Meta option -- this should be done in
Browse files Browse the repository at this point in the history
an overridden get_queryset() method on the provider
  • Loading branch information
coleifer committed Apr 27, 2011
1 parent 6f4df53 commit 56dbbb2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
15 changes: 14 additions & 1 deletion doc/djangoembed/providing_resources.rst
Expand Up @@ -109,10 +109,13 @@ file in your blog app's directory::
resource_type = 'link' # this is required
class Meta:
queryset = Entry.objects.filter(published=True)
model = Entry
named_view = 'blog_detail'
fields_to_match = {'entry_slug': 'slug'} # map url field to model field
def get_queryset(self):
queryset = Entry.objects.filter(published=True)

def author_name(self, obj):
return obj.author.username
Expand Down Expand Up @@ -182,3 +185,13 @@ dimensions. These methods call a general-purpose resize() method which
hooks into the image processing backend (by default PIL, but you can write
your own!) and resizes the photo, returning the url of the resized image and
the new dimensions.

If you want to manually specify which field to use for an image, simply override
the ``get_image()`` method on your provider. This method returns an instance
of an ImageField::

class EntryProvider(DjangoProvider):
# ... meta, etc ...

def get_image(self, obj):
return obj.primary_photo
11 changes: 3 additions & 8 deletions oembed/providers.py
Expand Up @@ -131,7 +131,6 @@ def request_resource(self, url, **kwargs):

class DjangoProviderOptions(object):
model = None # required
queryset = None # model._default_manager.all() if not supplied
named_view = '' # required: the name of this models' detail view
fields_to_match = {} # mapping of regex_field -> model attr for lookups

Expand Down Expand Up @@ -167,12 +166,8 @@ def __init__(self, meta_options, provider_class):
)

def _validate(self):
if self.model is None and self.queryset is None:
raise OEmbedException('Provider %s requires a model or queryset' % self.provider_class.__name__)
if self.model:
self.queryset = self.model._default_manager.all()
elif self.queryset:
self.model = self.queryset.model
if self.model is None:
raise OEmbedException('Provider %s requires a model' % self.provider_class.__name__)
if not self.named_view:
raise OEmbedException('Provider %s requires a named_view' % self.provider_class.__name__)

Expand Down Expand Up @@ -376,7 +371,7 @@ def get_params(self, url):
raise OEmbedException('No regex matched the url %s' % (url))

def get_queryset(self):
return self._meta.queryset
return self._meta.model._default_manager.all()

def get_object(self, url):
"""
Expand Down
1 change: 0 additions & 1 deletion oembed/tests/tests/providers.py
Expand Up @@ -27,7 +27,6 @@ def test_resource_options(self):

ops = BlogProvider._meta
self.assertEqual(ops.model, Blog)
self.assertQuerysetEqual(ops.queryset, Blog.objects.all())
self.assertEqual(ops.date_field, 'pub_date')
self.assertEqual(ops.fields_to_match, {'entry_slug': 'slug'})
self.assertEqual(ops.named_view, 'test_blog_detail')
Expand Down

0 comments on commit 56dbbb2

Please sign in to comment.