How to use the restless.views.Endpoint 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 dobarkod / django-restless / testproject / testapp / views.py View on Github external
raise HttpError(403, 'with exception')
        else:
            # this is an illegal return value for this function
            return 42

    def get(self, request):
        return 'OK'


class WildcardHandler(Endpoint):
    def dispatch(self, request, *args, **kwargs):
        return Http404('no such resource: %s %s' % (
            request.method, request.path))


class EchoView(Endpoint):
    def post(self, request):
        return {
            'headers': dict((k, str(v)) for k, v in request.META.items()),
            'raw_data': base64.b64encode(request.raw_data).decode('ascii')
        }

    def get(self, request):
        return self.post(request)

    def put(self, request):
        return self.post(request)


class ErrorRaisingView(Endpoint):
    def get(self, request):
        raise HttpError(400, 'raised error', extra_data='foo')
github dobarkod / django-restless / testproject / testapp / views.py View on Github external
class AuthorList(Endpoint):
    def get(self, request):
        return serialize(Author.objects.all())

    def post(self, request):
        form = AuthorForm(request.data)
        if form.is_valid():
            author = form.save()
            return Http201(serialize(author))
        else:
            return Http400(reason='invalid author data',
                details=form.errors)


class AuthorDetail(Endpoint):
    def get(self, request, author_id=None):
        author_id = int(author_id)
        try:
            return serialize(Author.objects.get(id=author_id))
        except Author.DoesNotExist:
            return Http404(reason='no such author')

    def delete(self, request, author_id=None):
        author_id = int(author_id)
        Author.objects.get(id=author_id).delete()
        return 'ok'

    def put(self, request, author_id=None):
        author_id = int(author_id)
        try:
            author = Author.objects.get(id=author_id)
github dobarkod / django-restless / restless / views.py View on Github external
def dispatch(self, request, *args, **kwargs):
        if not hasattr(request, 'content_type'):
            request.content_type = request.META.get('CONTENT_TYPE', 'text/plain')
        request.params = dict((k, v) for (k, v) in request.GET.items())
        request.data = None
        request.raw_data = request.body

        try:
            self._parse_body(request)
            authentication_required = self._process_authenticate(request)
            if authentication_required:
                return authentication_required

            response = super(Endpoint, self).dispatch(request, *args, **kwargs)
        except HttpError as err:
            response = err.response
        except Exception as ex:
            if settings.DEBUG:
                response = Http500(str(ex), traceback=traceback.format_exc())
            else:
                raise

        if not isinstance(response, HttpResponse):
            response = Http200(response)
        return response
github dobarkod / django-restless / restless / modelviews.py View on Github external
def post(self, request, *args, **kwargs):
        """Create a new object."""

        if 'POST' not in self.methods:
            raise HttpError(405, 'Method Not Allowed')

        Form = _get_form(self.form, self.model)
        form = Form(request.data or None, request.FILES)
        if form.is_valid():
            obj = form.save()
            return Http201(self.serialize(obj))
            
        raise HttpError(400, 'Invalid Data', errors=form.errors)


class DetailEndpoint(Endpoint):
    """
    Detail :py:class:`restless.views.Endpoint` supports getting a single
    object from the database (HTTP GET), updating it (HTTP PUT) and deleting
    it (HTTP DELETE).

    The only required configuration for the endpoint is the `model`
    class attribute, which should be set to the model you want to have the
    detail endpoints for.

    You can also provide a `form` class attribute, which should be the
    model form that's used for updating the model. If not provided, the
    default model class for the model will be created automatically.

    You can restrict the HTTP methods available by specifying the `methods`
    class variable.
github openstack / openstack-health / stackviz / views / tempest / api.py View on Github external
_cached_details[provider_name, run_id] = dest
        except (KeyError, IndexError):
            raise RunNotFoundException("Requested test run could not be found")

    details_map = _cached_details[provider_name, run_id]
    if test_name is None:
        return details_map
    else:
        if test_name in details_map:
            return details_map[test_name]
        else:
            raise TestNotFoundException(
                "Requested test could not be found in run")


class TempestRunRawEndpoint(Endpoint):
    def get(self, request, provider_name, run_id):
        return _load_run(provider_name, int(run_id))


class TempestRunTreeEndpoint(Endpoint):
    def get(self, request, provider_name, run_id):
        return _load_tree(provider_name, int(run_id))


class TempestRunDetailsEndpoint(Endpoint):
    def get(self, request, run_id, provider_name, test_name=None):
        return _load_details(provider_name, int(run_id), test_name)
github openstack / openstack-health / stackviz / views / tempest / api.py View on Github external
if test_name is None:
        return details_map
    else:
        if test_name in details_map:
            return details_map[test_name]
        else:
            raise TestNotFoundException(
                "Requested test could not be found in run")


class TempestRunRawEndpoint(Endpoint):
    def get(self, request, provider_name, run_id):
        return _load_run(provider_name, int(run_id))


class TempestRunTreeEndpoint(Endpoint):
    def get(self, request, provider_name, run_id):
        return _load_tree(provider_name, int(run_id))


class TempestRunDetailsEndpoint(Endpoint):
    def get(self, request, run_id, provider_name, test_name=None):
        return _load_details(provider_name, int(run_id), test_name)
github dobarkod / django-restless / restless / modelviews.py View on Github external
from django import VERSION

    if VERSION[:2] >= (1,8):
        mf = lambda m: modelform_factory(m, fields='__all__')
    else:
        mf = modelform_factory

    if form:
        return form
    elif model:
        return mf(model)
    else:
        raise NotImplementedError('Form or Model class not specified')


class ListEndpoint(Endpoint):
    """
    List :py:class:`restless.views.Endpoint` supporting getting a list of
    objects and creating a new one. The endpoint exports two view methods by
    default: get (for getting the list of objects) and post (for creating a
    new object).

    The only required configuration for the endpoint is the `model`
    class attribute, which should be set to the model you want to have a list
    (and/or create) endpoints for.

    You can also provide a `form` class attribute, which should be the
    model form that's used for creating the model. If not provided, the
    default model class for the model will be created automatically.

    You can restrict the HTTP methods available by specifying the `methods`
    class variable.
github openstack / openstack-health / stackviz / views / upstream / api.py View on Github external
metadata = api.get_run_metadata(run_id,session=session)
    ret_list = []

    for meta in metadata:
        ret_list.append(meta.to_dict())

    return ret_list


class GerritURLEndpoint(Endpoint):

    def get(self, request, change_id):
        return _get_runs(change_id)

class RunMetadataEndpoint(Endpoint):

    def get(self, request, run_id):
        return _get_metadata(run_id)
github openstack / openstack-health / stackviz / views / tempest / api.py View on Github external
else:
            raise TestNotFoundException(
                "Requested test could not be found in run")


class TempestRunRawEndpoint(Endpoint):
    def get(self, request, provider_name, run_id):
        return _load_run(provider_name, int(run_id))


class TempestRunTreeEndpoint(Endpoint):
    def get(self, request, provider_name, run_id):
        return _load_tree(provider_name, int(run_id))


class TempestRunDetailsEndpoint(Endpoint):
    def get(self, request, run_id, provider_name, test_name=None):
        return _load_details(provider_name, int(run_id), test_name)
github openstack / openstack-health / stackviz / views / upstream / api.py View on Github external
engine = create_engine('mysql://query:query@logstash.openstack.org' +
                           ':3306/subunit2sql')
    Session = sessionmaker(bind=engine)
    session = Session()

    metadata = api.get_run_metadata(run_id,session=session)
    ret_list = []

    for meta in metadata:
        ret_list.append(meta.to_dict())

    return ret_list


class GerritURLEndpoint(Endpoint):

    def get(self, request, change_id):
        return _get_runs(change_id)

class RunMetadataEndpoint(Endpoint):

    def get(self, request, run_id):
        return _get_metadata(run_id)