Skip to main content

Django app to json rest api provide api views, serializer, rate limit, pagination, authentication, permission,lazy response,blacklist etc.

Project description

Django Json Rest is a Django app that provides a simple way to json rest api. It is a simple and easy to use. Python 3.0+ is required. Django 1.8+ is required.

Features

  • Api view

  • Response

  • Serializer

  • Lazy response

  • Rate limit

  • Blacklist

  • Token Authentication

  • Permission

  • Pagination

Installation

pip install djangojr

Add djangojr.token and djangojr.ratelimit to your INSTALLED_APPS setting

INSTALLED_APPS = (
    'djangojr.token',
    'djangojr.ratelimit',
    ...
)

Token Authentication

Authorization : asdqwe5423asdqwe23asasd like this header is required for token authentication.

Example

from djangojr.authentication import token_authentication
from djangojr.permissions import has_permission, has_group
from djangojr.pagenation import pagenation
from djangojr.ratelimit.utils import ForIp, ForUser, RateTime, Block
from djangojr.serializer import JsonSerializer, api_view
from djangojr.rate_limit import rate_limit
from django.contrib.auth.models import User
from itertools import chain


def model_to_dict(instance, fields=None, exclude=None):
    opts = instance._meta
    data = dict()
    for f in chain(opts.concrete_fields, opts.private_fields, opts.many_to_many):
        if fields is not None and f.name not in fields:
            continue
        if exclude and f.name in exclude:
            continue
        else:
            data[f.name] = f.value_from_object(instance)
    return data


class ExampleSerializer(JsonSerializer):

    model = User

    def get_fields(self):
        fields = self.model.objects.all()
        # self.DATA is validated data
        data, page_info = pagenation(
            fields, self.DATA["page"], page_size=10, start=1)
        data = list(map(lambda x: model_to_dict(x), data))
        data = dict(data=data, page_info=page_info)
        return self.http_200_ok(data=data)


@api_view(['POST'])  # GET, POST, PUT, DELETE, PATCH
@token_authentication(authentication=True)
@has_permission(permissions=['add_user'])  # write permission codename
@has_group(groups=['admin'])  # write group name
# RateTime and Block support day, hour, minute, second
@rate_limit(model=ForUser, rate_time=RateTime(count=10, hour=1), block=Block(active=True, minute=5))
def get_fields_api(request):
    serializer = ExampleSerializer(data=request.body)
    # serializer.MODEL support str int float bool list dict set tuple and "ANY"
    serializer.MODEL = {'name': str, "number": int, "page": int}  # define api fields and type
    serializer.OPTIONS = {"number": [1, 2, 3, 4]}  # set options for field
    serializer.MAX_LENGTH = {"name": 10}  # set max length for field
    serializer.UNDEFINED_FIELD = False  # if True, undefined field will be ignored
    if serializer.is_valid():
        return serializer.get_fields()
    return serializer.http_404_not_found()


def example(value, arg):
    ...
    # do something


@api_view(['POST'])
@token_authentication(authentication=True)
@rate_limit(model=ForIp, rate_time=RateTime(count=5, hour=2, minute=15), block=Block(active=True, day=1, hour=2, second=30))
def get_fields_api(request):
    serializer = ExampleSerializer(data=request.body)
    serializer.MODEL = {"foo": str, "numbers": list}
    serializer.UNDEFINED_FIELD = True
    if serializer.is_valid():
        serializer.lazy_response(function=example, parametes=(
            serializer.DATA["foo"], serializer.DATA["numbers"]), data={})
    return serializer.http_404_not_found()

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

djangojr-1.0.5.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

djangojr-1.0.5-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file djangojr-1.0.5.tar.gz.

File metadata

  • Download URL: djangojr-1.0.5.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for djangojr-1.0.5.tar.gz
Algorithm Hash digest
SHA256 edc4bf0d1bf13464805d888d43482b8de634758b0cd84c8e94ca876e01daf334
MD5 0b7adba9349752976311e94ac7b8f10e
BLAKE2b-256 4008c8450f5285adbd46633c48d73154aabbe17fb62c3ff96e7c291f0588d208

See more details on using hashes here.

File details

Details for the file djangojr-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: djangojr-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for djangojr-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 98b8e05299b76608c5ad72d332ec76928b114eaaa10641725c690734c7f49304
MD5 30f64a16da6dac36a72abf57012d4bac
BLAKE2b-256 a4db2456f9bcc74ab87d656964200a58dbac865d4c2ebc7335c6856d57e69bf6

See more details on using hashes here.

Supported by

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