Skip to main content

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.

Source Distribution

perestroika-0.1.17.tar.gz (9.8 kB view details)

Uploaded Source

File details

Details for the file perestroika-0.1.17.tar.gz.

File metadata

  • Download URL: perestroika-0.1.17.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.7.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.7

File hashes

Hashes for perestroika-0.1.17.tar.gz
Algorithm Hash digest
SHA256 ab922a06fe6d553215fc10fc9a99cf58de9e5932e26162bb34bea83582732949
MD5 2ced8cb3a85b9c9ab63cbd8f73f34f1a
BLAKE2b-256 b9d0744836d7b523dbb475ba95ff2974644e19f8839fd95209f698e4ea4b7087

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page