Django Json Rest
=================
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
.. code:: python
INSTALLED_APPS = (
'djangojr.token',
'djangojr.ratelimit',
...
)
Token Authentication
--------------------
``Authorization : asdqwe5423asdqwe23asasd`` like this header is required for token authentication.
Example
--------
.. code:: python
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()
Raw data
{
"_id": null,
"home_page": "https://github.com/esrefyigitbasi-dev/django_json_rest",
"name": "djangojr",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "django restframework djangojr json parse response request api wrapper lazyresponse ratelimit pagination permissions token authentication",
"author": "Esref Yigitbasi",
"author_email": "esrefyigitbasi.dev@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/40/08/c8450f5285adbd46633c48d73154aabbe17fb62c3ff96e7c291f0588d208/djangojr-1.0.5.tar.gz",
"platform": null,
"description": "Django Json Rest\n=================\n\nDjango Json Rest is a Django app that provides a simple way to json rest api. It is a simple and easy to use.\nPython 3.0+ is required. Django 1.8+ is required.\n\nFeatures\n--------\n* Api view\n* Response\n* Serializer\n* Lazy response\n* Rate limit\n* Blacklist\n* Token Authentication\n* Permission\n* Pagination\n\nInstallation\n------------\n\n::\n\n pip install djangojr\n\n\nAdd ``djangojr.token`` and ``djangojr.ratelimit`` to your ``INSTALLED_APPS`` setting\n\n.. code:: python\n\n INSTALLED_APPS = (\n 'djangojr.token',\n 'djangojr.ratelimit',\n ...\n )\n\nToken Authentication\n--------------------\n``Authorization : asdqwe5423asdqwe23asasd`` like this header is required for token authentication.\n\nExample\n--------\n\n.. code:: python\n\n from djangojr.authentication import token_authentication\n from djangojr.permissions import has_permission, has_group\n from djangojr.pagenation import pagenation\n from djangojr.ratelimit.utils import ForIp, ForUser, RateTime, Block\n from djangojr.serializer import JsonSerializer, api_view\n from djangojr.rate_limit import rate_limit\n from django.contrib.auth.models import User\n from itertools import chain\n\n\n def model_to_dict(instance, fields=None, exclude=None):\n opts = instance._meta\n data = dict()\n for f in chain(opts.concrete_fields, opts.private_fields, opts.many_to_many):\n if fields is not None and f.name not in fields:\n continue\n if exclude and f.name in exclude:\n continue\n else:\n data[f.name] = f.value_from_object(instance)\n return data\n\n\n class ExampleSerializer(JsonSerializer):\n\n model = User\n\n def get_fields(self):\n fields = self.model.objects.all()\n # self.DATA is validated data\n data, page_info = pagenation(\n fields, self.DATA[\"page\"], page_size=10, start=1)\n data = list(map(lambda x: model_to_dict(x), data))\n data = dict(data=data, page_info=page_info)\n return self.http_200_ok(data=data)\n\n\n @api_view(['POST']) # GET, POST, PUT, DELETE, PATCH\n @token_authentication(authentication=True)\n @has_permission(permissions=['add_user']) # write permission codename\n @has_group(groups=['admin']) # write group name\n # RateTime and Block support day, hour, minute, second\n @rate_limit(model=ForUser, rate_time=RateTime(count=10, hour=1), block=Block(active=True, minute=5))\n def get_fields_api(request):\n serializer = ExampleSerializer(data=request.body)\n # serializer.MODEL support str int float bool list dict set tuple and \"ANY\"\n serializer.MODEL = {'name': str, \"number\": int, \"page\": int} # define api fields and type\n serializer.OPTIONS = {\"number\": [1, 2, 3, 4]} # set options for field\n serializer.MAX_LENGTH = {\"name\": 10} # set max length for field\n serializer.UNDEFINED_FIELD = False # if True, undefined field will be ignored\n if serializer.is_valid():\n return serializer.get_fields()\n return serializer.http_404_not_found()\n\n\n def example(value, arg):\n ...\n # do something\n\n\n @api_view(['POST'])\n @token_authentication(authentication=True)\n @rate_limit(model=ForIp, rate_time=RateTime(count=5, hour=2, minute=15), block=Block(active=True, day=1, hour=2, second=30))\n def get_fields_api(request):\n serializer = ExampleSerializer(data=request.body)\n serializer.MODEL = {\"foo\": str, \"numbers\": list}\n serializer.UNDEFINED_FIELD = True\n if serializer.is_valid():\n serializer.lazy_response(function=example, parametes=(\n serializer.DATA[\"foo\"], serializer.DATA[\"numbers\"]), data={})\n return serializer.http_404_not_found()\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Django app to json rest api provide api views, serializer, rate limit, pagination, authentication, permission,lazy response,blacklist etc.",
"version": "1.0.5",
"split_keywords": [
"django",
"restframework",
"djangojr",
"json",
"parse",
"response",
"request",
"api",
"wrapper",
"lazyresponse",
"ratelimit",
"pagination",
"permissions",
"token",
"authentication"
],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "30f64a16da6dac36a72abf57012d4bac",
"sha256": "98b8e05299b76608c5ad72d332ec76928b114eaaa10641725c690734c7f49304"
},
"downloads": -1,
"filename": "djangojr-1.0.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "30f64a16da6dac36a72abf57012d4bac",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 12693,
"upload_time": "2022-12-06T00:31:10",
"upload_time_iso_8601": "2022-12-06T00:31:10.687845Z",
"url": "https://files.pythonhosted.org/packages/a4/db/2456f9bcc74ab87d656964200a58dbac865d4c2ebc7335c6856d57e69bf6/djangojr-1.0.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "0b7adba9349752976311e94ac7b8f10e",
"sha256": "edc4bf0d1bf13464805d888d43482b8de634758b0cd84c8e94ca876e01daf334"
},
"downloads": -1,
"filename": "djangojr-1.0.5.tar.gz",
"has_sig": false,
"md5_digest": "0b7adba9349752976311e94ac7b8f10e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 11088,
"upload_time": "2022-12-06T00:31:13",
"upload_time_iso_8601": "2022-12-06T00:31:13.347604Z",
"url": "https://files.pythonhosted.org/packages/40/08/c8450f5285adbd46633c48d73154aabbe17fb62c3ff96e7c291f0588d208/djangojr-1.0.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-06 00:31:13",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "esrefyigitbasi-dev",
"github_project": "django_json_rest",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "djangojr"
}