date: | 2014-05-23 |
---|
This release improves the way data preparation & serialization are handled. It introduces these as separate, composable objects (Preparer & Serializer) that are assigned onto a Resource.
Porting is relatively straightforward in both the preparation & serialization cases.
If you were supplying fields on your Resource, such as:
# posts/api.py
from restless.dj import DjangoResource
from posts.models import Post
class PostResource(DjangoResource):
fields = {
'id': 'id',
'title': 'title',
'author': 'user.username',
'body': 'content',
'posted_on': 'posted_on',
}
Porting is simply 1.adding an import & 2. changing the assignment.:
# posts/api.py
from restless.dj import DjangoResource
# 1. ADDED IMPORT
from restless.preparers import FieldsPreparer
from posts.models import Post
class PostResource(DjangoResource):
# 2. CHANGED ASSIGNMENT
preparer = FieldsPreparer{
'id': 'id',
'title': 'title',
'author': 'user.username',
'body': 'content',
'posted_on': 'posted_on',
}
Serialization is even easier. If you performed no overridding, there’s nothing to update. You simply get the new JSONSerializer object automatically.
If you were overriding either raw_deserialize or raw_serialize, you should create a new Serializer subclass & move the methods over to it, changing their signatures as you go. Then assign an instance of your new Serializer subclass onto your ``Resource``(s).
Unported YAML serialization:
import yaml
from restless import Resource
class MyResource(Resource):
def raw_deserialize(self, body):
return yaml.safe_load(body)
def raw_serialize(self, data):
return yaml.dump(data)
Ported serialization:
import yaml
from restless import Resource
from restless.serializers import Serializer
class YAMLSerializer(Serializer):
def deserialize(self, body):
return yaml.safe_load(body)
def serialize(self, data):
return yaml.dump(data)
class MyResource(Resource):
serializer = YAMLSerializer()