Skip to content
This repository has been archived by the owner on Dec 15, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from miraculixx/fix-resource-uri-error
Browse files Browse the repository at this point in the history
fix regression due to resource_uri handling in tastypie 0.13.x
  • Loading branch information
miraculixx committed May 5, 2016
2 parents 2b5e0a0 + d1bcb0c commit 268765b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 15 deletions.
1 change: 0 additions & 1 deletion app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
'south',
'tpasync',
'tastypie',
)
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
install_requires=[
'Django>=1.6',
'django-tastypie',
'South==1.0',
'Celery>=3.1.16',
'pytz',
],
Expand Down
18 changes: 14 additions & 4 deletions tpasync/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
from tastypie import resources, http, utils

# use for dev/test only!
EAGER_RESULTS={}
EAGER_RESULTS = {}


class AsyncResourceMixin(object):

def async_get_detail(self, request, **kwargs):
raise NotImplementedError

Expand Down Expand Up @@ -126,6 +128,10 @@ def async_result(self, request, task_id, **kwargs):
to_be_serialized = self.alter_list_data_to_serialize(
request, to_be_serialized)
return self.create_response(request, to_be_serialized)
elif isinstance(result, dict):
bundle = self.build_bundle(obj=result, request=request)
self.full_dehydrate(bundle)
return self.create_response(request, bundle)
else:
bundle = self.build_bundle(obj=result, request=request)
bundle = self.full_dehydrate(bundle)
Expand All @@ -141,7 +147,8 @@ def process_result(self, result):
return result

def dehydrate(self, bundle):
bundle.data = bundle.obj
if bundle.obj:
bundle.data = bundle.obj
return bundle

def prepend_urls(self):
Expand All @@ -156,7 +163,10 @@ def prepend_urls(self):
self.wrap_view('async_result'), name="api_async_result")
]

def _async_method(method):
def detail_uri_kwargs(self, bundle_or_obj):
raise NotImplementedError()

def _async_method(method): # @NoSelf
"""
This is a wrapper that's used to generate methods like
VERB_KIND using hooks named async_VERB_KIND.
Expand Down Expand Up @@ -207,4 +217,4 @@ def inner(self, request, **kwargs):


class BaseAsyncResource(AsyncResourceMixin, resources.Resource):
pass
pass
12 changes: 7 additions & 5 deletions tpasync/tasks.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import time
from celery import task

from celery import shared_task

@task

@shared_task
def successful_task():
# We do extremely difficult and long computation here :-)
time.sleep(3)
return {'result': 'ok', 'id': 1}


@task
@shared_task
def list_task():
return [{'result': 'ok', 'id': 1}, {'result': 'not bad', 'id': 2}]


@task
@shared_task
def failing_task():
raise Exception('I failed miserably')
raise Exception('I failed miserably')

8 changes: 4 additions & 4 deletions tpasync/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from django import http
from tpasync.resources import BaseAsyncResource
from tastypie import fields
from tastypie.test import ResourceTestCase
from tastypie.test import ResourceTestCaseMixin
from . import tasks
from django.test.testcases import TestCase


class EmptyTestResource(BaseAsyncResource):
Expand All @@ -16,7 +17,6 @@ class TestResource(BaseAsyncResource):
result = fields.CharField()

class Meta:
include_resource_uri = False
resource_name = 'test'

def async_get_detail(self, request, **kwargs):
Expand All @@ -32,7 +32,7 @@ def async_post_detail(self, request, **kwargs):
return tasks.failing_task.apply_async()


class AsyncResourceTest(ResourceTestCase):
class AsyncResourceTest(ResourceTestCaseMixin, TestCase):
def setUp(self):
super(AsyncResourceTest, self).setUp()
self.empty_resource = EmptyTestResource()
Expand Down Expand Up @@ -134,7 +134,7 @@ def test_list_deserialization(self):
self.assertEqual(data['state'], 'SUCCESS')
self.assertIn('result_uri', data)
result_url = data['result_uri']

print result_url
# Get results
response = self.api_client.get(result_url)
self.assertHttpOK(response)
Expand Down

0 comments on commit 268765b

Please sign in to comment.