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:
Validation only before main logic.
Reusable logic for many views.
Reusable input and output data formats.
More clear views.
Dataflow:
Decorators (d).
Parser (p). Parse request body.
PreValidator (prev). Validate and clear request.
PreRenderer (prer). Render and return PreValidation errors.
Controller (c). Main logic: do some things.
PostValidator (postv). Validate and clear response.
PostRenderer (postr). Render and return PostValidation errors.
Renderer (r). Render successfull response.
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:
Parsers (djburger.p).
Validators (djburger.v). Can be used as prev and postv.
Bases (djburger.v.b).
Constructors (djburger.v.c).
Wrappers (djburger.v.w)
Controllers (djburger.c). Can be used as c.
Renderers (djburger.r). Can be used as prer, postr and r.
Some additional components:
djburger.exceptions – useful exceptions.
Interfaces
Decorator. Any decorator which can wrap Django view
Parser. Any callable object which get request object and return parsed data.
Validator. Have same interfaces as Django Forms, but get request by initialization:
.__init__()
request – Request object
data – data from user (prev) or controller (postv)
**kwargs – any keyword arguments for validator
.is_valid() – return True if data is valid False otherwise.
.errors – errors if data is invalid.
.cleaned_data – cleaned data if input data is valid.
Controller. Any callable object. Kwargs:
request – Request object.
data – validated request data. 3 **kwargs – kwargs from url.
Renderer. Any callable object. Kwargs:
request – Request object.
data – validated controller data (only for r).
validator – validator which not be passed (only for prer and postr).
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
-
djburger.p.BSON
djburger.r.BSON
-
djburger.v.b.RESTFramework
djburger.v.w.RESTFramework
djburger.r.RESTFramework
-
djburger.v.b.Marshmallow
djburger.v.w.Marshmallow
-
djburger.v.c.PySchemes
djburger.v.w.PySchemes
-
djburger.r.YAML
-
djburger.r.Tablib
What’s next?
Read documentation, source code docstrings or just inspect djburger from python console (for example, help('djburger.views')).
See example project.
If you have some questions then view issues or create new.
If you found some mistakes then fix it and create Pull Request. Contributors are welcome.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file djburger-0.8.1.tar.gz
.
File metadata
- Download URL: djburger-0.8.1.tar.gz
- Upload date:
- Size: 18.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82fb6b17463aa089478936d2fd8e95af5a83a3cfded8f3906006e4857510a573 |
|
MD5 | 86dfc88334affd187329ada7a65c63d5 |
|
BLAKE2b-256 | aa061df64e788fa31f1f3e2579356f904a89fedb4e947761514164830a94d488 |