Permalink
Browse files

Whitespace cleanup and comments.

  • Loading branch information...
1 parent 02988b0 commit 7b3707801d6c74b828bd19c02d2c5039ee8f10b4 @issackelly issackelly committed Aug 28, 2012
Showing with 20 additions and 5 deletions.
  1. +15 −2 tastypie/contrib/contenttypes/fields.py
  2. +5 −3 tastypie/contrib/contenttypes/resources.py
@@ -7,34 +7,47 @@
class GenericForeignKeyField(fields.ToOneField):
+ """
+ Provides access to GenericForeignKey objects from the django content_types
+ framework.
+ """
+
def __init__(self, to, attribute, **kwargs):
+
if not isinstance(to, dict):
raise ValueError('to field must be a dictionary in GenericForeignKeyField')
+
if len(to) <= 0:
raise ValueError('to field must have some values')
+
for k, v in to.iteritems():
if not issubclass(k, models.Model) or not issubclass(v, Resource):
raise ValueError('to field must map django models to tastypie resources')
+
super(GenericForeignKeyField, self).__init__(to, attribute, **kwargs)
def get_related_resource(self, related_instance):
self._to_class = self.to.get(type(related_instance), None)
+
if self._to_class is None:
raise TypeError('no resource for model %s' % type(related_instance))
+
return super(GenericForeignKeyField, self).get_related_resource(related_instance)
@property
def to_class(self):
+
if self._to_class and not issubclass(GenericResource, self._to_class):
return self._to_class
+
return GenericResource
def resource_from_uri(self, fk_resource, uri, request=None, related_obj=None, related_name=None):
+
try:
obj = fk_resource.get_via_uri(uri, request=request)
fk_resource = self.get_related_resource(obj)
- return super(GenericForeignKeyField, self).resource_from_uri(
- fk_resource, uri, request, related_obj, related_name)
+ return super(GenericForeignKeyField, self).resource_from_uri(fk_resource, uri, request, related_obj, related_name)
except ObjectDoesNotExist:
raise ApiFieldError("Could not find the provided object via resource URI '%s'." % uri)
@@ -4,6 +4,10 @@
class GenericResource(ModelResource):
+ """
+ Provides a stand-in resource for GFK relations.
+ """
+
def get_via_uri(self, uri, request=None):
"""
This pulls apart the salient bits of the URI and populates the
@@ -25,8 +29,6 @@ def get_via_uri(self, uri, request=None):
except Resolver404:
raise NotFound("The URL provided '%s' was not a link to a valid resource." % uri)
- # FIXME Hack to climb the prototype chain to get the parent resource
- # for this given uri.
- parent_resource = view.func_closure[0].cell_contents.func_closure[0].cell_contents.func_closure[0].cell_contents
+ parent_resource = view.func_closure[0].cell_contents.func_closure[0].cell_contents
return parent_resource.obj_get(**self.remove_api_resource_names(kwargs))

0 comments on commit 7b37078

Please sign in to comment.