How to use the restless.preparers.FieldsPreparer function in restless

To help you get started, we’ve selected a few restless examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github toastdriven / restless / tests / test_resources.py View on Github external
def test_prepare(self):
        # Without fields.
        data = {
            'title': 'Cosmos',
            'author': 'Carl Sagan',
            'short_desc': 'A journey through the stars by an emminent astrophysist.',
            'pub_date': '1980'
        }

        # Should be unmodified.
        self.assertIsInstance(self.res.preparer, Preparer)
        self.assertEqual(self.res.prepare(data), data)

        self.res.preparer = FieldsPreparer(fields={
            'title': 'title',
            'author': 'author',
            'synopsis': 'short_desc',
        })
        self.assertEqual(self.res.prepare(data), {
            'author': 'Carl Sagan',
            'synopsis': 'A journey through the stars by an emminent astrophysist.',
            'title': 'Cosmos'
        })
github toastdriven / restless / tests / test_preparers.py View on Github external
def test_prepare_collection_subpreparer(self):
        subpreparer = FieldsPreparer(fields={
            'name': 'name',
        })
        preparer = FieldsPreparer(fields={
            'who': CollectionSubPreparer('who', subpreparer),
        })
        preped = preparer.prepare(self.dict_data)
        self.assertEqual(preped, {'who': [
            {'name': 'Ford'},
            {'name': 'Arthur'},
            {'name': 'Beeblebrox'},
        ]})
github toastdriven / restless / tests / test_preparers.py View on Github external
def test_prepare_subpreparer(self):
        subpreparer = FieldsPreparer(fields={
            'id': 'id',
            'data': 'data',
        })
        preparer = FieldsPreparer(fields={
            'flying': 'say',
            'wale': SubPreparer('moof.buried', subpreparer),
        })
        preped = preparer.prepare(self.obj_data)
github toastdriven / restless / tests / test_preparers.py View on Github external
def test_prepare_subsubpreparer(self):
        subsubpreparer = FieldsPreparer(fields={
            'really': 'yes',
        })
        subpreparer = FieldsPreparer(fields={
            'data': SubPreparer('data', subsubpreparer),
        })
        preparer = FieldsPreparer(fields={
            'wale': SubPreparer('moof.buried', subpreparer),
        })
        preped = preparer.prepare(self.obj_data)
        self.assertEqual(preped, {'wale': {'data': {'really': 'no'}}})
github toastdriven / restless / tests / test_preparers.py View on Github external
def test_prepare_subsubpreparer(self):
        subsubpreparer = FieldsPreparer(fields={
            'really': 'yes',
        })
        subpreparer = FieldsPreparer(fields={
            'data': SubPreparer('data', subsubpreparer),
        })
        preparer = FieldsPreparer(fields={
            'wale': SubPreparer('moof.buried', subpreparer),
        })
        preped = preparer.prepare(self.obj_data)
        self.assertEqual(preped, {'wale': {'data': {'really': 'no'}}})
github toastdriven / restless / tests / test_dj.py View on Github external
# Ugh. Settings for Django.
from django.conf import settings
settings.configure(DEBUG=True)

from restless.dj import DjangoResource
from restless.exceptions import Unauthorized
from restless.preparers import FieldsPreparer
from restless.resources import skip_prepare
from restless.utils import json

from .fakes import FakeHttpRequest, FakeModel


class DjTestResource(DjangoResource):
    preparer = FieldsPreparer(fields={
        'id': 'id',
        'title': 'title',
        'author': 'username',
        'body': 'content'
    })
    fake_db = []

    def __init__(self, *args, **kwargs):
        super(DjTestResource, self).__init__(*args, **kwargs)

        self.http_methods.update({
            'schema': {
                'GET': 'schema',
            }
        })
github ghing / tablesplitter / tablesplitter / api.py View on Github external
class ModelResource(FlaskResource):
    def list(self):
        objs = self.model.select()
        limit = self.request.args.get('limit')
        if limit:
            objs = objs.limit(limit)

        return objs

    def detail(self, pk):
        return self.model.get(self.model.id == pk)


class CellResource(ModelResource):
    preparer = FieldsPreparer(fields = {
        'id': 'id',
        'source': 'source.id',
        'image_url': 'image_url',
        'box': 'box',
        'text_count': 'count',
    })

    model = SplitFile

    def list(self):
        limit = self.request.args.get('limit', 20)
        text_lt = self.request.args.get('text_lt')
        no_accepted = 'no_accepted' in self.request.args
        random = 'random' in self.request.args

        objects = SplitFile.select(SplitFile,
github dbca-wa / oim-cms / oim_cms / api.py View on Github external
"""
    http_methods = {
        'list': {
            'GET': 'detail',
            'POST': 'detail',
            'PUT': 'detail',
            'DELETE': 'detail',
        },
        'detail': {
            'GET': 'detail',
            'POST': 'detail',
            'PUT': 'detail',
            'DELETE': 'detail',
        }
    }
    preparer = FieldsPreparer(fields={
        'email': 'user.email',
        'username': 'user.username',
        'first_name': 'user.first_name',
        'last_name': 'user.last_name',
        'shared_id': 'shared_id',
        'session_key': 'session.session_key',
        'client_logon_ip': 'ip'
    })

    def request_body(self):
        return None

    def is_authenticated(self):
        return self.request.user.is_authenticated()

    def detail(self):
github vmesel / PyJobs / pyjobs / api / views.py View on Github external
response_dict["meta"] = {
            "page": self.page.number,
            "limit": self.page.paginator.per_page,
            "total_pages": self.page.paginator.num_pages,
            "total_count": self.page.paginator.count,
            "next": next_page,
            "previous": previous_page,
        }
        return response_dict


class JobResource(DjangoPaginatedResource):
    page_size = 20
    serializer = PyJobsSerializer()
    preparer = FieldsPreparer(
        fields={
            field.name: field.name
            for field in Job._meta.fields
            if field.name
            not in {"premium", "public", "ad_interested", "company_email", "cellphone"}
        }
    )

    def list(self):
        return Job.objects.all()

    def detail(self, pk):
        return Job.objects.get(id=pk)
github dbca-wa / oim-cms / registers / api.py View on Github external
'role': data.get_role_display(),
            'it_systems': [i.name for i in it_systems],
        }

    def list(self):
        return ITSystemHardware.objects.all()


class ITSystemEventResource(DjangoResource):
    def __init__(self, *args, **kwargs):
        super(ITSystemEventResource, self).__init__(*args, **kwargs)
        self.http_methods.update({
            'current': {'GET': 'current'}
        })

    preparer = FieldsPreparer(fields={
        'id': 'id',
        'description': 'description',
        'planned': 'planned',
        'current': 'current',
    })

    def prepare(self, data):
        prepped = super(ITSystemEventResource, self).prepare(data)
        prepped['event_type'] = data.get_event_type_display()
        # Output times as the local timezone.
        tz = pytz.timezone(settings.TIME_ZONE)
        prepped['start'] = data.start.astimezone(tz)
        if data.end:
            prepped['end'] = data.end.astimezone(tz)
        else:
            prepped['end'] = None