Skip to content
This repository has been archived by the owner on Mar 30, 2020. It is now read-only.

Commit

Permalink
Merge pull request #5 from orsinium/improve-kwargs
Browse files Browse the repository at this point in the history
Improve kwargs
  • Loading branch information
orsinium authored May 12, 2018
2 parents 01e4be6 + 67cf60e commit a4e54f7
Show file tree
Hide file tree
Showing 33 changed files with 487 additions and 362 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

[![Build Status](https://travis-ci.org/orsinium/djburger.svg?branch=master)](https://travis-ci.org/orsinium/djburger) [![Documentation](https://readthedocs.org/projects/djburger/badge/)](https://djburger.readthedocs.io/en/latest/) [![PyPI version](https://img.shields.io/pypi/v/djburger.svg)](https://pypi.python.org/pypi/djburger) [![Status](https://img.shields.io/pypi/status/djburger.svg)](https://pypi.python.org/pypi/djburger) [![Code size](https://img.shields.io/github/languages/code-size/orsinium/djburger.svg)](https://github.com/orsinium/djburger) [![License](https://img.shields.io/pypi/l/djburger.svg)](LICENSE)

**DjBurger** -- framework for big Django projects.
**DjBurger** -- framework for safe and maintainable web-projects.

What DjBurger do?

* Split Django views into steps for secure and clean code.
* Split Django views into [steps](https://djburger.readthedocs.io/en/latest/philosophy.html#dataflow) for secure and clean code.
* Provide built-in objects for all steps.
* Integrates this many side libraries like Django REST Framework and Marshmallow.
* Integrates this [many side libraries](https://djburger.readthedocs.io/en/latest/external.html) like Django REST Framework and Marshmallow.

DjBurger doesn't depend on Django. You can use it in any projects if you want.

Expand All @@ -23,17 +23,16 @@ Read more into [documentation](https://djburger.readthedocs.io/en/latest/).
3. Reusable input and output data formats.
4. More clean views.


## Dataflow

1. **Decorators** (`d`). Feel free to use any side Django decorators like `csrf_exempt`.
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 successful response.
1. **Decorators**. Feel free to use any side Django decorators like `csrf_exempt`.
2. **Parser**. Parse request body.
3. **PreValidator**. Validate and clear request.
4. **PreRenderer**. Render and return PreValidation errors response.
5. **Controller**. Main logic: do some things.
6. **PostValidator**. Validate and clear response.
7. **PostRenderer**. Render and return PostValidation errors response.
8. **Renderer**. Render successful response.

![Scheme](wiki/source/imgs/scheme.png)

Expand All @@ -47,3 +46,4 @@ Required only Controller and Renderer.
1. If you have some questions then [view issues](https://github.com/orsinium/djburger/issues) or [create new](https://github.com/orsinium/djburger/issues/new).
1. If you found some mistakes then fix it and [create Pull Request](https://github.com/orsinium/djburger/compare). Contributors are welcome.
1. [Star this project on github](https://github.com/orsinium/djburger) :)

28 changes: 14 additions & 14 deletions djburger/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,10 +207,10 @@ def __call__(self, request, data, **kwargs):
class pre(object): # noQA
"""Decorator for input data validation before subcontroller calling
:param djburger.v.b.IValidator validator: validator for pre-validation.
:param djburger.validators.bases.IValidator validator: validator for pre-validation.
:param \**kwargs: kwargs for validator.
:raises djburger.e.SubValidationError: if pre-validation not passed
:raises djburger.exceptions.SubValidationError: if pre-validation not passed
"""
def __init__(self, validator, **kwargs):
self.validator = validator
Expand Down Expand Up @@ -238,10 +238,10 @@ def _wrapper(self, data, request=None, **kwargs):
class post(pre): # noQA
"""Decorator for output data validation before subcontroller calling
:param djburger.v.b.IValidator validator: validator for post-validation.
:param djburger.validators.bases.IValidator validator: validator for post-validation.
:param \**kwargs: kwargs for validator.
:raises djburger.e.SubValidationError: if post-validation not passed
:raises djburger.exceptions.SubValidationError: if post-validation not passed
"""
def _wrapper(self, data, request=None, **kwargs):
result = self.controller(
Expand All @@ -259,18 +259,18 @@ def _wrapper(self, data, request=None, **kwargs):
return validator.cleaned_data


def subcontroller(c, prev=None, postv=None):
def subcontroller(controller, prevalidator=None, postvalidator=None):
"""Constructor for subcontrollers
If any validation failed, immediately raise SubValidationError.
:param djburger.v.b.IValidator prev: validator for pre-validation.
:param callable c: controller.
:param djburger.v.b.IValidator postv: validator for post-validation.
:param djburger.validators.bases.IValidator prevalidator:
:param callable controller:
:param djburger.validators.bases.IValidator postvalidator:
:raises djburger.e.SubValidationError: if any validation not passed
:raises djburger.exceptions.SubValidationError: if any validation not passed
"""
if prev:
c = pre(prev)(c)
if postv:
c = post(postv)(c)
return c
if prevalidator:
controller = pre(prevalidator)(controller)
if postvalidator:
controller = post(postvalidator)(controller)
return controller
11 changes: 6 additions & 5 deletions djburger/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,23 @@ class StatusCodeError(ValidationError):
"""Validation error with specified status code
Raise it from validator for return validation error with some status code.
View catch this error and call `prer` or `postr` with specified status code.
View catch this error and call `prerenderer` or `postrenderer`
with specified status code.
:param int status_code: Model for deleting object.
:param \**kwargs: kwargs for ValidationError.
"""

def __init__(self, status_code, msg, **kwargs):
def __init__(self, status_code, *args, **kwargs):
self.status_code = status_code
self.msg = msg
super(StatusCodeError, self).__init__(msg, **kwargs)
super(StatusCodeError, self).__init__(*args, **kwargs)


class SubValidationError(ValidationError):
"""ValidationError for validators in subcontrollers.
If calidation in subcontroller not passed, subcontroller raise this error
for stoping execution and returning `postr` with failed subcontroller's validator.
for stoping execution and returning `postrenderer` with failed
subcontroller's validator.
"""
pass
2 changes: 1 addition & 1 deletion djburger/mocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,5 @@ def __call__(self, *args, **kwargs):
return self.dispatch(*args, **kwargs)


class QuerySet:
class QuerySet(object):
pass
2 changes: 1 addition & 1 deletion djburger/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from functools import partial
from json import loads as _json
# project
from djburger.utils import is_django_installed
from .utils import is_django_installed


# Django
Expand Down
18 changes: 11 additions & 7 deletions djburger/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,15 @@


__all__ = [
'Base',
'Template', 'HTTP',
'JSON', 'YAML', 'Tablib',
'Redirect', 'Exception',
]
'BSON', 'Base', 'BaseWithHTTP',
'Exception',
'HTTP',
'JSON',
'RESTFramework',
'Redirect',
'Tablib', 'Template',
'YAML',
]


class Base(object):
Expand Down Expand Up @@ -105,7 +109,7 @@ def __call__(self, request=None, data=None, validator=None, status_code=None):
class BaseWithHTTP(Base):
"""Base class wrapped by HttpResponse
:param \**kwargs: all kwargs of djburger.r.Base.
:param \**kwargs: all kwargs of djburger.renderers.Base.
"""

def set_http_kwargs(self, **kwargs):
Expand Down Expand Up @@ -199,7 +203,7 @@ def __call__(self, data=None, **kwargs):
class Exception(object): # noQA
"""Raise Exception
I'm recommend use this renderer as `postr`.
We are recommend use this renderer as `postrenderer`.
Raised exception can be handled by decorators or loggers.
:param exception: exception for raising.
Expand Down
Loading

0 comments on commit a4e54f7

Please sign in to comment.