Metadata-Version: 2.1
Name: django-dataclasses
Version: 0.1.2
Summary: Implement a JSON API using dataclasses
Home-page: https://gitlab.com/roivant/oss/django-dataclasses
Author: Roivant Sciences
Author-email: vant.tech.eng@roivant.com
License: UNKNOWN
Platform: UNKNOWN
Classifier: Topic :: Internet :: WWW/HTTP :: Dynamic Content
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Development Status :: 4 - Beta
Classifier: Operating System :: OS Independent
Classifier: License :: OSI Approved :: Apache Software License
Requires-Python: >=3.7
License-File: LICENSE
Requires-Dist: apispec
Requires-Dist: django
Requires-Dist: dataclasses-jsonschema

django-dataclasses
================================

Documentation
----------------

This library provides a wrapper around Django views to enforce schema using the ``dataclasses-jsonschema`` library.

To use it, decorate your view functions with the appropriate HTTP method provided by `django-dataclasses`
(``post``, ``get``, ``patch``, or ``put``). Annotate the return values of your functions with the appropriate
dataclass type. Views that take input require an additional
``body`` parameter with the appropriate dataclass type annotation::

    from django import http
    import django_dataclasses


    @django_dataclasses.dataclass
    class StringLengthRequest:
        input: str


    @django_dataclasses.dataclass
    class StringLengthResponse:
        length: int


    @django_dataclasses.login_required
    @django_dataclasses.post
    def string_length(request: http.HttpRequest, body: StringLengthRequest) -> StringLengthResponse:
        """Calculate the length of a string"""
        return StringLengthResponse(length=len(body.input))


Export your schemas in JSON schema format or your API in the OpenAPI v3 format. You need to add
``django_dataclasses`` to your ``INSTALLED_APPS`` for the management command to be available::

    ./manage.py openapi_export
    ./manage.py schema_export

License
-------

Contributions are made under the terms of the Apache License, Version 2.0.
See `LICENSE <LICENSE>`_.

Contributing
----------------

This project uses `pyenv <https://github.com/yyuu/pyenv>`_, please install it first.
When running on a Mac, the ``Makefile`` uses Homebrew to install dependencies. On other
platforms please install them manually.

Clone the repo and setup your development environment by running::

    git clone https://gitlab.com/roivant/oss/django-dataclasses.git
    make setup

Run the tests and code validations::

    make test
    make validate

You can test exporting the openapi schema and compiling it to JavaScript using ``openapi-typescript-codegen``
by running this command. The output files are put in a folder ``api/``::

    make compile_js

Contributors must adhere to the Contributor Covenant `Code of Conduct
<https://www.contributor-covenant.org/version/2/0/code_of_conduct/>`_.
Please report suspected violations to vant.tech.eng@roivant.com.

About the template
-------------------

This repository was created from the `temple-python <https://gitlab.com/roivant/vant-tech/temple-python>`_
template. If you have access to that repository, apply updates from the template by running::

    temple update

What tools are included?
~~~~~~~~~~~~~~~~~~~~~~~~

- A Makefile for convenience; use ``make setup`` to setup your dev environment
- A build configuration in ``.gitlab-ci.yml``
- A test framework using pytest and requiring 100% coverage; use ``make test`` to run
- Python auto-formatting using ``black``, ``flake8``, and ``isort`` via a git pre-commit hook
- Automatic versioning and ChangeLog using `PBR <https://docs.openstack.org/developer/pbr/>`_

Versioning using PBR
~~~~~~~~~~~~~~~~~~~~

The `PBR <https://docs.openstack.org/developer/pbr/>`_ library will automatically
increment the version when you commit to the main branch by merging your pull request.
The commit message that you enter on GitHub when merging will determine what version
number is assigned, using `Semantic Versioning <http://semver.org/>`_.

- Messages starting with ``Sem-Ver: bugfix,`` or with no matching message will bump the ``PATCH`` number
- Messages starting with ``Sem-Ver: feature,`` or ``Sem-Ver: deprecation,`` will bump the ``MINOR`` number.
- Messages starting with ``Sem-Ver: api-break,`` will bump the ``MAJOR`` number.



