Skip to main content

Implementation of Google JSON Style Guide for Django

Project description

pre-commit autoflake: on Imports: isort Code style: black PyPI PyPI - Python Version


Django Google JSON Style API

Implementation of Google JSON Style Guide for Django

Install

pip install django-google-json-style-api

Example

# models.py

from django.db import models


class City(models.Model):
    city_name = models.TextField()

# schemas.py

from typing import List

from pydantic import BaseModel

from django_google_json_style_api.base import CamelModel
from django_google_json_style_api.responses import BaseResponseData, BaseSuccessResponse


class AddCityRequest(CamelModel):
    city_name: str


class AddCitiesRequest(BaseModel):
    cities: List[AddCityRequest]


class CityDataItem(CamelModel):
    id: int
    city_name: str


class CityResponseData(BaseResponseData[CityDataItem]):
    ...


class CityResponse(BaseSuccessResponse[CityResponseData]):
    __kind__ = "City"

# urls.py

from django.urls import path
from django.views.decorators import csrf

from . import views

urlpatterns = [
    path(
        "add/",
        csrf.csrf_exempt(views.AddCitiesView.as_view()),
        name="add-cities",
    ),
]


# views.py

from django_google_json_style_api.decorators import process_json_response

from django.utils.decorators import method_decorator
from django.views import View

from .models import City
from .schemas import AddCitiesRequest, CityResponse, CityDataItem


@method_decorator(process_json_response(api_version='1.1'), name="dispatch")
class AddCitiesView(View):
    def post(self, request):
        cities = AddCitiesRequest.parse_raw(request.body).cities
        response_items = []
        for add_city_request in cities:
            city = City.objects.create(**add_city_request.dict())
            city_data_item = CityDataItem(
                id=city.id,
                city_name=city.city_name
            )
            response_items.append(city_data_item)
        return CityResponse.make_from(
            request,
            total_items=City.objects.count(),
            items=response_items,
        )

# tests.py

from django.test import TestCase
from django.urls import reverse


class TestCities(TestCase):

    def test_add_cities(self):
        url = reverse('add-cities')
        data = {
            "cities": [
                {"cityName": "Tyumen"},
                {"cityName": "Moscow"},
            ]
        }
        response = self.client.post(url, data, content_type="application/json")
        response_json = response.json()
        self.assertDictEqual(
            response_json,
            {
                'apiVersion': '1.1',
                "data": {
                    'currentItemCount': 2,
                    "items": [
                        {
                            "id": 1,
                            "cityName": "Tyumen",
                        },
                        {
                            "id": 2,
                            "cityName": "Moscow",
                        },
                    ],
                    'itemsPerPage': 100,
                    'kind': 'City',
                    'startIndex': 0,
                    'totalItems': 2,
                },
            }
        )

TODO:

Docs, tests

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

django-google-json-style-api-0.3.3.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_google_json_style_api-0.3.3-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file django-google-json-style-api-0.3.3.tar.gz.

File metadata

  • Download URL: django-google-json-style-api-0.3.3.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.6 CPython/3.9.0 Linux/5.11.0-37-generic

File hashes

Hashes for django-google-json-style-api-0.3.3.tar.gz
Algorithm Hash digest
SHA256 b7508c2da7919f9b5ac0870f330d1d8e24998cae01a5801e98a4fe2fbe832791
MD5 86c7805698ca3a9914dd632d37a25c75
BLAKE2b-256 721a79b15b49eb381a69c58020d9d24425f444b23843673cf6c86e549b4ec583

See more details on using hashes here.

File details

Details for the file django_google_json_style_api-0.3.3-py3-none-any.whl.

File metadata

File hashes

Hashes for django_google_json_style_api-0.3.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1c2dfe3cca802be1b270b24ce45b15756f71a3a04b4b622f026fd34ec31472f0
MD5 72b480a450ba633cfb3b6077216a61fe
BLAKE2b-256 897eca7063a67822a3cab6e7473db82037f3bfb2f02060b82ee900291e6353d3

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