Created
May 11, 2012 11:58
-
-
Save esatterwhite/2659203 to your computer and use it in GitHub Desktop.
Returning data on POST request with Tastypie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
Generates a stupid api the illustrates returning POST data from tastypie | |
''' | |
import random | |
import shelve | |
from hashlib import md5 | |
from datetime import datetime | |
from django.utils import simplejson | |
from tastypie.resources import Resource | |
from tastypie.authorization import ReadOnlyAuthorization, DjangoAuthorization, Authorization | |
from tastypie import fields | |
#data store | |
db = shelve.open( 'datastore.db', writeback=True ) | |
class Data( object ): | |
def __init__( self, info=None ): | |
self.id = md5( str( datetime.now() ) ).hexdigest() | |
self.data = info | |
def save( self ): | |
""" Saves it self in the data store""" | |
db[self.id] = self | |
db.sync() | |
def __str__( self ): | |
return self.id | |
def __unicode__( self ): | |
return u'%s' % self.__str__() | |
def __repr__( self ): | |
return "<Data: %s>" % self.data | |
@classmethod | |
def get( self, idx ): | |
"""helper method to retrive an object at a given index in the data store """ | |
try: | |
return db.get( db.keys()[idx] ) | |
except: | |
return None | |
@property | |
def info( self ): | |
return self.data | |
class DataResource( Resource ): | |
id = fields.CharField() | |
info = fields.CharField() | |
class Meta: | |
resource_name = "data" | |
object_class = Data | |
authorization = Authorization() | |
### THIS IS THE MAGIC ### | |
always_return_data = True | |
def get_object_list( self, request ): | |
""" | |
populates the list endpoint | |
""" | |
return [ db.get( key ) for key in db.keys() ] | |
def obj_get_list( self, request=None, **kwargs): | |
""" | |
Don't know why you need this... but you do. | |
""" | |
return self.get_object_list( request ) | |
def get_resource_uri( self, bundle_or_obj ): | |
""" | |
generates the URI for each object | |
""" | |
kwargs= { | |
"resource_name":self._meta.resource_name, | |
"pk":bundle_or_obj.obj.id | |
} | |
if self._meta.api_name is not None: | |
kwargs['api_name'] = self._meta.api_name | |
return self._build_reverse_url("api_dispatch_detail", kwargs=kwargs) | |
def obj_get( self, request=None, **kwargs ): | |
""" | |
Gets and object by pk | |
""" | |
d = db.get(str(kwargs.get('pk')) ) | |
return d | |
def obj_update( self, bundle, request=None, **kwargs ): | |
''' Used for PUT requests ''' | |
return self.obj_create( bundle, request, **kwargs ) | |
def obj_create( self, bundle, request=None, **kwargs): | |
""" | |
For POST Requests | |
This method blows. You know it. I know it. | |
Let's never speak of this again. | |
""" | |
#eh, i didn't dig deep enough to find out | |
#translate data the right way | |
data = simplejson.loads( request.POST.keys()[0] ) | |
obj = Data(data["info"]) | |
obj.save() | |
bundle.obj = obj | |
bundle = self.full_hydrate( bundle ) | |
return bundle | |
def rollback( self, bundles ): | |
pass | |
def dehydrate( self, bundle ): | |
""" add some random crap to the response""" | |
random.seed() | |
bundle.data['silly']= random.random() | |
return bundle | |
def dehydrate_id( self, bundle ): | |
""" | |
Populates the id field in the response | |
""" | |
return bundle.obj.id | |
def dehydrate_info( self, bundle ): | |
""" | |
Populates the info field in the response | |
""" | |
return getattr( bundle.obj, 'data', None ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you very much for that
crying here after i saw the "THIS IS THE MAGIC" comment