Skip to content

Commit

Permalink
According to wlanslovenija#38:
Browse files Browse the repository at this point in the history
Use an additional mixin for get_related_resource
Changed meta property name to prefer_polymorphic_resource_uri
  • Loading branch information
leo-naeka committed Jan 11, 2013
1 parent e2fd62b commit dd82774
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
14 changes: 9 additions & 5 deletions tastypie_mongoengine/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ def get_api_name(self):
return self._resource._meta.api_name
return None

class GetRelatedMixin(object):
def get_related_resource(self, related_instance):
related_resource = super(ApiNameMixin, self).get_related_resource(related_instance)
related_resource = super(GetRelatedMixin, self).get_related_resource(related_instance)
type_map = getattr(related_resource._meta, 'polymorphic', {})
if type_map and getattr(related_resource._meta, 'prefer_related_resource_name', False):
if type_map and getattr(related_resource._meta, 'prefer_polymorphic_resource_uri', False):
resource = related_resource._get_resource_from_class(type_map, related_instance.__class__)
related_resource._meta.resource_name = resource._meta.resource_name
return related_resource

class BuildRelatedMixin(ApiNameMixin):
class TastypieMongoengineMixin(ApiNameMixin, GetRelatedMixin):
pass

class BuildRelatedMixin(TastypieMongoengineMixin):
def build_related_resource(self, value, **kwargs):
# A version of build_related_resource which allows only dictionary-like data
if hasattr(value, 'items'):
Expand All @@ -46,7 +50,7 @@ def build_related_resource(self, value, **kwargs):
else:
raise exceptions.ApiFieldError("The '%s' field was not given a dictionary-alike data: %s." % (self.instance_name, value))

class ReferenceField(ApiNameMixin, fields.ToOneField):
class ReferenceField(TastypieMongoengineMixin, fields.ToOneField):
"""
References another MongoEngine document.
"""
Expand Down Expand Up @@ -212,7 +216,7 @@ def to_class(self):
return self._to_class_with_listresource


class ReferencedListField(ApiNameMixin, fields.ToManyField):
class ReferencedListField(TastypieMongoengineMixin, fields.ToManyField):
"""
Represents a list of referenced objects. It must be used in conjunction
with ReferenceField.
Expand Down
2 changes: 1 addition & 1 deletion tastypie_mongoengine/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ def _wrap_polymorphic(self, resource, fun):
self._meta.queryset = resource._meta.queryset
self.base_fields = resource.base_fields.copy()
self.fields = resource.fields.copy()
if getattr(self._meta, 'prefer_related_resource_name', False):
if getattr(self._meta, 'prefer_polymorphic_resource_uri', False):
self._meta.resource_name = resource._meta.resource_name
if getattr(self._meta, 'include_resource_type', True):
self.base_fields['resource_type'] = base_fields['resource_type']
Expand Down
2 changes: 1 addition & 1 deletion tests/test_project/test_app/api/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class Meta:
allowed_methods = ('get', 'post', 'put', 'patch', 'delete')
authorization = tastypie_authorization.Authorization()

prefer_related_resource_name = True
prefer_polymorphic_resource_uri = True
polymorphic = {
'individual': IndividualResource,
'company': CompanyResource,
Expand Down

0 comments on commit dd82774

Please sign in to comment.