Skip to main content

Framework for views in big projects on Django.

Project description

Documentation on other languages:

DjBurger – patched views format for big projects on Django.

Key principles:

  1. Validation only before main logic.

  2. Reusable logic for many views.

  3. Reusable input and output data formats.

  4. More clear views.

Dataflow:

  1. Decorators (d).

  2. Parser (p). Parse request body.

  3. PreValidator (prev). Validate and clear request.

  4. PreRenderer (prer). Render and return PreValidation errors.

  5. Controller (c). Main logic: do some things.

  6. PostValidator (postv). Validate and clear response.

  7. PostRenderer (postr). Render and return PostValidation errors.

  8. Renderer (r). Render successfull response.

Scheme

Scheme

Required only Controller and Renderer.

Installation

STABLE

pip install djburger

DEV

Using pip:

sudo pip install -e git+https://github.com/orsinium/djburger.git#egg=djburger

In requirements.txt:

-e git+https://github.com/orsinium/djburger.git#egg=djburger

Components

Main components:

  1. Parsers (djburger.p).

  2. Validators (djburger.v). Can be used as prev and postv.

    1. Bases (djburger.v.b).

    2. Constructors (djburger.v.c).

    3. Wrappers (djburger.v.w)

  3. Controllers (djburger.c). Can be used as c.

  4. Renderers (djburger.r). Can be used as prer, postr and r.

Some additional components:

  1. djburger.exceptions – useful exceptions.

Interfaces

  1. Decorator. Any decorator which can wrap Django view

  2. Parser. Any callable object which get request object and return parsed data.

  3. Validator. Have same interfaces as Django Forms, but get request by initialization:

    1. .__init__()

      • request – Request object

      • data – data from user (prev) or controller (postv)

      • **kwargs – any keyword arguments for validator

    2. .is_valid() – return True if data is valid False otherwise.

    3. .errors – errors if data is invalid.

    4. .cleaned_data – cleaned data if input data is valid.

  4. Controller. Any callable object. Kwargs:

    1. request – Request object.

    2. data – validated request data. 3 **kwargs – kwargs from url.

  5. Renderer. Any callable object. Kwargs:

    1. request – Request object.

    2. data – validated controller data (only for r).

    3. validator – validator which not be passed (only for prer and postr).

    4. status_code – HTTP status code if validator raise djburger.e.StatusCodeError.

Usage example

View definition:

import djburger

class ExampleView(djburger.ViewBase):
    rules = {
        'get': djburger.rule(
            c=lambda request, data, **kwargs: 'Hello, World!',
            postv=djburger.v.c.IsStr,
            postr=djburger.r.Exception(),
            r=djburger.r.Template(template_name='index.html'),
        ),
    }

Minimum info:

class ExampleView(djburger.ViewBase):
    default_rule = djburger.rule(
        c=lambda request, data, **kwargs: 'Hello, World!',
        r=djburger.r.Template(template_name='index.html'),
    ),

Rule from default_rule will be used as rule for all requests, which method not definited in rules.

Big example:

class UsersView(djburger.ViewBase):
    rules = {
        'get': djburger.rule(
            d=[login_required, csrf_exempt],
            prev=SomeValidator,
            c=djburger.c.List(model=User),
            postv=djburger.v.c.QuerySet,
            postr=djburger.r.Exception(),
            r=djburger.r.JSON(),
        ),
        'put': djburger.rule(
            d=[csrf_exempt],
            p=djburger.p.JSON(),
            prev=SomeOtherValidator,
            c=djburger.c.Add(model=User),
            r=djburger.r.JSON(),
        ),
    }

External libraries support

  • BSON

    • djburger.p.BSON

    • djburger.r.BSON

  • Django REST Framework

    • djburger.v.b.RESTFramework

    • djburger.v.w.RESTFramework

    • djburger.r.RESTFramework

  • Marshmallow

    • djburger.v.b.Marshmallow

    • djburger.v.w.Marshmallow

  • PySchemes

    • djburger.v.c.PySchemes

    • djburger.v.w.PySchemes

  • PyYAML

    • djburger.r.YAML

  • Tablib

    • djburger.r.Tablib

What’s next?

  1. Read documentation, source code docstrings or just inspect djburger from python console (for example, help('djburger.views')).

  2. See example project.

  3. If you have some questions then view issues or create new.

  4. If you found some mistakes then fix it and create Pull Request. Contributors are welcome.

  5. Star this project on github :)

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

djburger-0.8.2.tar.gz (17.7 kB view hashes)

Uploaded Source

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