index/stable/: alto-django-utils-0.1.0 metadata and description

Homepage Simple index

common django utilities for alto JSON APIs

author Jakub Kaláb
author_email jakubkalab@gmail.com
classifiers
  • License :: Other/Proprietary License
  • Programming Language :: Python :: 3
  • Programming Language :: Python :: 3.6
  • Programming Language :: Python :: 3.7
  • Programming Language :: Python :: 3.8
  • Programming Language :: Python :: 3.9
description_content_type text/markdown
keywords json,api,django,utils
license Proprietary
maintainer Jakub Kaláb
maintainer_email jakubkalab@gmail.com
project_urls
  • Repository, http://gitlab.apps.asrv.cz/python/alto-django-utils
  • Documentation, http://gitlab.apps.asrv.cz/python/alto-django-utils
  • Bug Tracker, https://gitlab.apps.asrv.cz/python/alto-django-utils/-/issues
requires_dist
  • django (>=1.10.0,<2.1)
  • djangorestframework (>=3.6.2,<3.11)
  • dynamic-rest (>=2.0.0,<2.1.0)
  • inflection (>=0.3.0,<0.4)
  • pydash (>=4.1.0,<4.9)
  • psycopg2-binary (>=2.8.2,<2.9); extra == "test"
  • pyyaml (>=3.12,<5.4); extra == "test"
  • pytest (>=6.1,<7.0); extra == "test"
  • pytest-django (>=4.1.0,<5.0.0); extra == "test"
  • pytest-cov (>=2.10,<3.0); extra == "test"
  • pytest-watch (>=4.2.0,<5.0.0); extra == "test-watch"
  • pytest-testmon (>=1.0.3,<2.0.0); extra == "test-watch"
requires_python >=3.6,<4.0
File Tox results History
alto-django-utils-0.1.0.tar.gz
Size
13 KB
Type
Source
alto_django_utils-0.1.0-py3-none-any.whl
Size
15 KB
Type
Python Wheel
Python
3

alto-django-utils

alto-django-utils is a set of convenient shortcuts for quickly setting up JSON HTTP APIs based on django, DRF and dynamic-rest. The most important utility functions are:

Both of these take the model class as the first argument (in addition to some optional ones) and derive from it the serializer and viewset classes, needed to build up the API, with sensible defaults. These can then be registered using dynamic_rest.routers.DynamicRouter. The resulting API supports all the advanced features of dynamic-rest (https://github.com/AltSchool/dynamic-rest) - complex filtering, ordering and sideloading of records and their relations.

Prerequisities

install python

apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
  libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
  xz-utils # tk-dev

curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash

echo 'export PATH="/root/.pyenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"'               >> ~/.bash_profile
echo 'eval "$(pyenv virtualenv-init -)"'    >> ~/.bash_profile

source ~/.bash_profile

pyenv install 3.6.5

create and activate virtualenv

pyenv virtualenv 3.6.5 tutor3.6.5
pyenv activate tutor3.6.5

install django

pip install django

create and enter project

django-admin startproject tutor
cd tutor

add basic dependencies

echo django              >> requirements.in
echo django-extensions   >> requirements.in
echo djangorestframework >> requirements.in
echo dynamic-rest        >> requirements.in
echo '-e git+ssh://git@gitlab.apps.asrv.cz/python/alto-django-utils.git#egg=alto_django_utils' \
  >> requirements.in

compile and install them

pip install pip-compile
pip-compile
pip install -r requirements.txt

create app

django-admin startapp testapp

register app in project

# tutor/settings.py

INSTALLED_APPS = [
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'django_extensions',
  'rest_framework',
  'dynamic_rest',
  'testapp'
]

Usage

create first model

# testapp/models.py

from django.db.models import Model, CharField, DateTimeField

class Client(Model):
  email      = CharField('Email',    max_length = 64)
  firstname  = CharField('Jméno',    max_length = 32)
  surname    = CharField('Příjmení', max_length = 32)
  created_at = DateTimeField(auto_now_add = True)
  updated_at = DateTimeField(auto_now = True)

make and apply migration

./manage.py makemigrations -n create_client
./manage.py migrate

create serializer

# testapp/serializers.py

from alto_django_utils.serializers import serializer_for
from .models import Client

ClientSerializer = serializer_for(Client)

create viewset

# testapp/viewsets.py

from alto_django_utils.viewsets import viewset_for
from .models      import Client
from .serializers import ClientSerializer

ClientViewSet = viewset_for(Client, ClientSerializer)

register viewset in app urls

# testapp/urls.py

from django.conf.urls import url, include
from dynamic_rest.routers import DynamicRouter
from .viewsets import ClientViewSet

crud_router = DynamicRouter()

crud_router.register_resource(ClientViewSet)

app_name    = 'testapp'
urlpatterns = [
  url(r'^crud/', include(crud_router.urls)),
]

include app urls in project urls

# tutor/urls.py

from django.conf.urls import url, include

urlpatterns = [
  url(r'^test/', include('testapp.urls')),
]

list created urls

./manage.py show_urls
# /test/crud/clients/        views.ClientViewSet   testapp:clients-list
# /test/crud/clients/<pk>/   views.ClientViewSet   testapp:clients-detail

test created urls

./manage.py runserver # in separate shell

pip install httpie

http post :8000/test/crud/clients firstname=Testy surname=Testson email=test@test.cz
http get  :8000/test/crud/clients?filter{firstname.icontains}=test
# {
#     "count": 1,
#     "next": null,
#     "previous": null,
#     "results": {
#         "clients": [
#             {
#                 "id": 1,
#                 "email": "test@test.cz",
#                 "firstname": "Testy",
#                 "surname": "Testson",
#                 "created_at": "2018-04-19T03:02:16.850640Z",
#                 "updated_at": "2018-04-19T03:02:16.850786Z"
#             }
#         ]
#     }
# }

get more info at https://github.com/AltSchool/dynamic-rest