Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter support for polymorphic resources #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions tastypie_mongoengine/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from django.core import exceptions, urlresolvers
from django.db.models import base as models_base
from django.utils import datastructures
from bson import ObjectId
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't import classes directly, only modules.


try:
# Django 1.5+
Expand Down Expand Up @@ -67,14 +68,32 @@ def filter(self, **kwargs):

for field, value in kwargs.iteritems():
value = self._process_filter_value(value)
if constants.LOOKUP_SEP in field:
try:
if constants.LOOKUP_SEP in field:
filter_bits = field.split(constants.LOOKUP_SEP)
field_name = filter_bits.pop(0)
filter_type = 'exact'
except AttributeError, e:
raise tastypie_exceptions.InvalidFilterError("Unsupported filter: (%s, %s)" % (field, value))

try:
result = ListQuerySet([(unicode(obj.pk), obj) for obj in result.itervalues() if getattr(obj, field) == value])
if isinstance(getattr(result[0], field_name), ObjectId):
result = ListQuerySet([(unicode(obj.pk), obj) for obj in result.itervalues() if getattr(obj, field_name).__str__() == value])
else:
result = ListQuerySet([(unicode(obj.pk), obj) for obj in result.itervalues() if getattr(obj, field_name) == value] )
except AttributeError, e:
raise tastypie_exceptions.InvalidFilterError(e)


# for field, value in kwargs.iteritems():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to have commented out code in final code.

# value = self._process_filter_value(value)
# if constants.LOOKUP_SEP in field:
# raise tastypie_exceptions.InvalidFilterError("Unsupported filter: (%s, %s)" % (field, value))

# try:
# result = ListQuerySet([(unicode(obj.pk), obj) for obj in result.itervalues() if getattr(obj, field) == value])
# except AttributeError, e:
# raise tastypie_exceptions.InvalidFilterError(e)

return result

def attrgetter(self, attr):
Expand Down