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.10.tar.gz (8.6 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: perestroika-0.1.10.tar.gz
  • Upload date:
  • Size: 8.6 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.10.tar.gz
Algorithm Hash digest
SHA256 4e9731475ab221ea2382e0be6e73f1d4ce299e907a90ebd4a45e4d2f3718a695
MD5 3d907dec5d332e86e1a6c82366600c68
BLAKE2b-256 258d901479dcdab3e007c1d1ace6803a98676dcb4dfb557ed7c954bfdbd188dc

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