Skip to content
This repository has been archived by the owner on Oct 3, 2019. It is now read-only.

Commit

Permalink
[add] optional view verbose_logging (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
FP committed Aug 27, 2019
1 parent e3d5635 commit 0865be7
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mbq/api_tools/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging
from functools import wraps
from typing import Any, Dict, List, Optional, Tuple, Union

Expand All @@ -19,6 +20,7 @@
from .utils import first


logger = logging.getLogger(__name__)
PermissionsCollection = Union[List[Any], Tuple[Any, ...]]
HttpMethodNames = Literal["GET", "POST", "PATCH", "PUT", "DELETE"]
OnUnknownField = Literal["raise", "exclude"]
Expand All @@ -34,6 +36,7 @@ def __init__(
paginated: bool = False,
page_size: Optional[int] = None,
on_unknown_field: Optional[OnUnknownField] = None,
verbose_logging: bool = False,
_method: Optional[bool] = None,
):
if not permissions:
Expand All @@ -54,6 +57,7 @@ def __init__(

self.paginated = paginated
self.page_size = page_size
self.verbose_logging = verbose_logging

if self.page_size and not self.paginated:
raise ValueError(
Expand All @@ -70,6 +74,7 @@ def __call__(self, func):
func.paginated = self.paginated
func.page_size = self.page_size
func.on_unknown_field = self.on_unknown_field
func.verbose_logging = self.verbose_logging

if self._method:
return func
Expand Down Expand Up @@ -137,6 +142,7 @@ def __init__(self, view_func, request, view_class=None):
self.paginated = view_func.paginated
self.page_size = view_func.page_size
self.on_unknown_field = view_func.on_unknown_field
self.verbose_logging = view_func.verbose_logging

# Add the pagination fields to the params
if self.paginated:
Expand Down Expand Up @@ -182,6 +188,7 @@ def __call__(self, *args, **kwargs):
# Backwards compat thing for DRF permissions
self.args = args
self.kwargs = kwargs
logging_level = logging.INFO if self.verbose_logging else logging.DEBUG

try:
self._enrich_request()
Expand All @@ -196,8 +203,10 @@ def __call__(self, *args, **kwargs):
return response

except exceptions.ValidationError as e:
logger.log(logging_level, f"ValidationError: {str(e)}")
return ClientErrorResponse("validation_error", str(e))
except exceptions.ImmediateResponseError as e:
logger.log(logging_level, f"ImmediateResponseError: {str(e)}")
return e.response

def _enrich_request(self):
Expand Down

0 comments on commit 0865be7

Please sign in to comment.