How to use the restless.resources.Resource 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
import six
import unittest

from restless.exceptions import HttpError, NotFound, MethodNotImplemented
from restless.preparers import Preparer, FieldsPreparer
from restless.resources import Resource
from restless.utils import json

from .fakes import FakeHttpRequest, FakeHttpResponse


class GenericResource(Resource):
    def build_response(self, data, status=200):
        resp = FakeHttpResponse(data, content_type='application/json')
        resp.status_code = status
        return resp

    # This should Fake some endpoint Authentication
    def is_authenticated(self):
        if self.endpoint == 'list':
            return False
        return super(GenericResource, self).is_authenticated()


class ResourceTestCase(unittest.TestCase):
    resource_class = GenericResource

    def setUp(self):
github toastdriven / restless / restless / dj.py View on Github external
import six

from django.conf import settings
from django.conf.urls import url
from django.core.exceptions import ObjectDoesNotExist
from django.core.paginator import Paginator
from django.http import HttpResponse, Http404
from django.views.decorators.csrf import csrf_exempt

from .constants import OK, NO_CONTENT
from .exceptions import NotFound, BadRequest
from .resources import Resource


class DjangoResource(Resource):
    """
    A Django-specific ``Resource`` subclass.

    Doesn't require any special configuration, but helps when working in a
    Django environment.
    """

    def serialize_list(self, data):
        if data is None:
            return super(DjangoResource, self).serialize_list(data)

        if getattr(self, 'paginate', False):
            page_size = getattr(self, 'page_size', getattr(settings, 'RESTLESS_PAGE_SIZE', 10))
            paginator = Paginator(data, page_size)

            page_number = self.request.GET.get('p', 1)
github toastdriven / restless / restless / tnd.py View on Github external
and helps to init a resource instance
    """
    def __init__(self, *args, **kwargs):
        super(_BridgeMixin, self).__init__(*args, **kwargs)
        # create a resource instance based on the registered class
        # and init-parameters
        self.resource_handler = self.__class__.__resource_cls__(
            *self.__resource_args__, **self.__resource_kwargs__
        )

        self.resource_handler.request = self.request
        self.resource_handler.application = self.application
        self.resource_handler.ref_rh = weakref.proxy(self) # avoid circular reference between


class TornadoResource(Resource):
    """
    A Tornado-specific ``Resource`` subclass.
    """

    _request_handler_base_ = web.RequestHandler
    """
    To override ``tornado.web.RequestHandler`` we used,
    please assign your RequestHandler via this attribute.
    """

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

        self.request = None
        """
        a reference to ``tornado.httpclient.HTTPRequest``
github toastdriven / restless / restless / fl.py View on Github external
from flask import make_response
from flask import request

from .constants import OK, NO_CONTENT
from .resources import Resource


class FlaskResource(Resource):
    """
    A Flask-specific ``Resource`` subclass.

    Doesn't require any special configuration, but helps when working in a
    Flask environment.
    """
    @classmethod
    def as_list(cls, *init_args, **init_kwargs):
        # Overridden here, because Flask uses a global ``request`` object
        # rather than passing it to each view.
        def _wrapper(*args, **kwargs):
            # Make a new instance so that no state potentially leaks between
            # instances.
            inst = cls(*init_args, **init_kwargs)
            inst.request = request
            return inst.handle('list', *args, **kwargs)
github toastdriven / restless / restless / tnd.py View on Github external
cls.__name__ + '_' + _BridgeMixin.__name__ + '_restless',
            (_BridgeMixin, cls._request_handler_base_,),
            dict(
                __resource_cls__=cls,
                __resource_args__=init_args,
                __resource_kwargs__=init_kwargs,
                __resource_view_type__=view_type)
        )

        """
        Add required http-methods to the newly created class
        We need to scan through MRO to find what functions users declared,
        and then add corresponding http-methods used by Tornado.
        """
        bases = inspect.getmro(cls)
        bases = bases[0:bases.index(Resource)-1]
        for k, v in cls.http_methods[view_type].items():
            if any(v in base_cls.__dict__ for base_cls in bases):
                setattr(new_cls, k.lower(), _method)

        return new_cls
github toastdriven / restless / restless / pyr.py View on Github external
from pyramid.response import Response

from .constants import OK, NO_CONTENT
from .resources import Resource


class PyramidResource(Resource):
    """
    A Pyramid-specific ``Resource`` subclass.

    Doesn't require any special configuration, but helps when working in a
    Pyramid environment.
    """

    @classmethod
    def as_list(cls, *args, **kwargs):
        return super(PyramidResource, cls).as_list(*args, **kwargs)

    @classmethod
    def as_detail(cls, *init_args, **init_kwargs):
        def _wrapper(request):
            # Make a new instance so that no state potentially leaks between
            # instances.
github toastdriven / restless / restless / it.py View on Github external
import re

import itty
from restless.resources import Resource


class IttyResource(Resource):
    """
    A Itty-specific ``Resource`` subclass.

    Doesn't require any special configuration, but helps when working in a
    Itty environment.
    """
    debug = False

    def is_debug(self):
        return self.debug

    def build_response(self, data, status=200):
        return itty.Response(data, status=status, content_type='application/json')

    @classmethod
    def setup_urls(cls, rule_prefix):