Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Rest lib

Project description

☭ perestroika

Build Status Coverage Status PyPI version

About

Simple REST framework for django

Installation

With pip:

pip install perestroika

Usage

from django.contrib.auth.models import User
from perestroika.resource import DjangoResource
from perestroika.methods import Get, Post
from perestroika.exceptions import RestException



class Validator:
    def __call__(self, item: dict) -> dict:
        return {'username': item['username']}


def reject_not_superuser(request, bundle):
    if not request.user.is_superuser:
        raise RestException(message="Unauthorized", status_code=401)

    
def add_is_superuser_flag(request, bundle):
    for user in bundle['items']:
        user['is_superuser'] = True


class SuperUserResource(DjangoResource):
    # use django @cache_control kwargs
    cache_control = dict(max_age=0, no_cache=True, no_store=True, must_revalidate=True)

    # allowed method GET
    get = Get(
        # base queryset
        queryset=User.objects.filter(is_superuser=True),
        
        # restrict access
        request_hooks = [
            reject_not_superuser
        ],
        
        # any callable
        output_validator=Validator,
    )
    
    # allowed method POST
    post = Post(
        # restrict access
        request_hooks = [
            reject_not_superuser
        ],
    
        # process incoming data
        pre_query_hooks=[
            add_is_superuser_flag
        ],

        # any callable
        input_validator=Validator,
    )

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for perestroika, version 0.1.18
Filename, size File type Python version Upload date Hashes
Filename, size perestroika-0.1.18.tar.gz (9.7 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page