Skip to content

Commit

Permalink
Merge pull request #335 from phenobarbital/dev
Browse files Browse the repository at this point in the history
replacing own json_encoder with datamodel's encoders
  • Loading branch information
phenobarbital authored Jan 23, 2025
2 parents e694089 + 34fad30 commit a46770f
Show file tree
Hide file tree
Showing 12 changed files with 34 additions and 160 deletions.
4 changes: 2 additions & 2 deletions navigator/actions/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from bs4 import BeautifulSoup as bs
from lxml import html, etree
from proxylists.proxies import FreeProxy
from datamodel.parsers.json import JSONContent
from asyncdb.utils.functions import cPrint
from ..libs.json import JSONContent
from ..exceptions import ConfigError
from .abstract import AbstractAction

Expand Down Expand Up @@ -342,7 +342,7 @@ async def process_request(self, future, url: str):
result = filename
# getting the result, based on the Accept logic
elif self.file_buffer is True:
data = response.content
data = response.content
buffer = BytesIO(data)
buffer.seek(0)
result = buffer
Expand Down
2 changes: 1 addition & 1 deletion navigator/brokers/rabbitmq/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import aiormq
from aiormq.abc import AbstractConnection, AbstractChannel
from datamodel import BaseModel
from navigator.libs.json import json_encoder, json_decoder
from datamodel.parsers.json import json_encoder, json_decoder
from navigator.exceptions import ValidationError
from ...conf import rabbitmq_dsn
from ..wrapper import BaseWrapper
Expand Down
2 changes: 1 addition & 1 deletion navigator/brokers/redis/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from redis import asyncio as aioredis
from datamodel import Model, BaseModel
from navconfig.logging import logging
from navigator.libs.json import json_encoder, json_decoder
from datamodel.parsers.json import json_encoder, json_decoder
from navigator.exceptions import ValidationError
from ..connection import BaseConnection
from ..wrapper import BaseWrapper
Expand Down
2 changes: 1 addition & 1 deletion navigator/brokers/sqs/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from datamodel import Model, BaseModel
from navconfig import config
from navconfig.logging import logging
from navigator.libs.json import json_encoder, json_decoder
from datamodel.parsers.json import json_encoder, json_decoder
from navigator.exceptions import ValidationError
from ..connection import BaseConnection
from ..wrapper import BaseWrapper
Expand Down
6 changes: 6 additions & 0 deletions navigator/libs/json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from datamodel.parsers.json import (
json_encoder,
json_decoder,
BaseEncoder,
JSONContent
)
130 changes: 0 additions & 130 deletions navigator/libs/json.pyx

This file was deleted.

2 changes: 1 addition & 1 deletion navigator/template/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import inspect
from aiohttp import web
from aiohttp.abc import AbstractView
from datamodel.parsers.json import json_encoder, json_decoder
from .parser import TemplateParser
from ..libs.json import json_decoder


F = TypeVar("F", bound=Callable[..., Any])
Expand Down
2 changes: 1 addition & 1 deletion navigator/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
__description__ = (
"Navigator Web Framework based on aiohttp, " "with batteries included."
)
__version__ = "2.12.11"
__version__ = "2.12.12"
__copyright__ = "Copyright (c) 2020-2024 Jesus Lara"
__author__ = "Jesus Lara"
__author_email__ = "[email protected]"
Expand Down
2 changes: 1 addition & 1 deletion navigator/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
NAV aiohttp Class-Based Views.
"""
from ..libs.json import json_encoder, json_decoder
from datamodel.parsers.json import json_encoder, json_decoder
from .base import BaseHandler, BaseView
from .data import DataView
from .model import ModelView, load_models
Expand Down
17 changes: 11 additions & 6 deletions navigator/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pathlib import Path
from dataclasses import dataclass
from urllib import parse
import aiohttp
from aiohttp import web
from aiohttp.web_exceptions import (
HTTPMethodNotAllowed,
Expand All @@ -16,19 +17,26 @@
import orjson
from orjson import JSONDecodeError
import aiohttp_cors
from datamodel.parsers.json import (
JSONContent,
json_encoder,
json_decoder
)
from datamodel.exceptions import ValidationError
from navconfig.logging import logging, loglevel
from navigator_session import get_session
from ..exceptions import NavException, InvalidArgument
from ..libs.json import JSONContent, json_encoder, json_decoder
from ..responses import JSONResponse
from ..applications.base import BaseApplication
from ..types import WebApp
from ..conf import CORS_MAX_AGE


# Monkey-patching
DEFAULT_JSON_ENCODER = json_encoder
DEFAULT_JSON_DECODER = json_decoder
# monkey-patch the JSON encoder
aiohttp.typedefs.DEFAULT_JSON_ENCODER = json_encoder
aiohttp.typedefs.DEFAULT_JSON_DECODER = json_decoder


class BaseHandler(ABC):
Expand Down Expand Up @@ -72,10 +80,7 @@ async def get_userid(self, session, idx: str = 'user_id') -> int:
status=403
)
try:
if 'session' in session:
return session['session'][idx]
else:
return session[idx]
return session['session'][idx] if 'session' in session else session[idx]
except KeyError:
self.error(reason="Unauthorized", status=403)

Expand Down
19 changes: 9 additions & 10 deletions navigator/views/model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Iterable
from typing import Optional, Union, Any, Awaitable, Callable
from typing import Optional, Union, Any, Awaitable, Callable, Hashable
import importlib
import asyncio
from aiohttp import web
Expand Down Expand Up @@ -73,7 +73,7 @@ class ModelView(AbstractModel):
get_model: BaseModel = None
model_name: str = None # Override the current model with other.
path: str = None
pk: Optional[Iterable] = None
pk: Union[Hashable, str] = None
_required: list = []
_primaries: list = []
_hidden: list = []
Expand Down Expand Up @@ -204,7 +204,7 @@ async def _model_response(
try:
# Run the coroutine in a new thread
asyncio.run_coroutine_threadsafe(
self._get_callback(response, result),
self._get_callback(response, result), # pylint: disable=E1102
loop
)
except Exception as ex:
Expand Down Expand Up @@ -512,7 +512,7 @@ async def set_column_value(value):
for name, column in self.model.get_columns().items():
### if a function with name _get_{column name} exists
### then that function is called for getting the field value
fn = getattr(self, f'_set_{name}', None)
fn = getattr(self, f'_post_{name}', None) or getattr(self, f'_set_{name}', None)
if not fn:
fn = getattr(self, f'_get_{name}', None)
if fn:
Expand Down Expand Up @@ -559,7 +559,7 @@ async def _patch_response(self, result, status: int = 202) -> web.Response:
try:
# Run the coroutine in a new thread
asyncio.run_coroutine_threadsafe(
self._patch_callback(response, result),
self._patch_callback(response, result), # pylint: disable=E1102
loop
)
except Exception as ex:
Expand Down Expand Up @@ -626,6 +626,7 @@ async def _calculate_column(
return

async def _set_update(self, model, data):
""" Set the value for a field in the model """
for key, val in data.items():
if key in model.get_fields():
col = model.column(key)
Expand Down Expand Up @@ -654,9 +655,7 @@ async def set_column_value(value):
for name, column in self.model.get_columns().items():
### if a function with name _get_{column name} exists
### then that function is called for getting the field value
fn = getattr(self, f'_set_{name}', None)
if not fn:
fn = getattr(self, f'_patch_{name}', None)
fn = getattr(self, f'_patch_{name}', None) or getattr(self, f'_set_{name}', None)
if fn:
try:
val = value.get(name, None)
Expand Down Expand Up @@ -864,7 +863,7 @@ async def _post_response(self, result: BaseModel, status: int = 200, fields: lis
try:
# Run the coroutine in a new thread
asyncio.run_coroutine_threadsafe(
self._post_callback(response, result),
self._post_callback(response, result), # pylint: disable=E1102
loop
)
except Exception as ex:
Expand All @@ -885,7 +884,7 @@ async def _put_response(self, result: BaseModel, status: int = 200, fields: list
try:
# Run the coroutine in a new thread
asyncio.run_coroutine_threadsafe(
self._put_callback(response, result),
self._put_callback(response, result), # pylint: disable=E1102
loop
)
except Exception as ex:
Expand Down
6 changes: 0 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@
extra_compile_args=COMPILE_ARGS,
language="c"
),
Extension(
name='navigator.libs.json',
sources=['navigator/libs/json.pyx'],
extra_compile_args=COMPILE_ARGS,
language="c++"
),
Extension(
name='navigator.exceptions.exceptions',
sources=['navigator/exceptions/exceptions.pyx'],
Expand Down

0 comments on commit a46770f

Please sign in to comment.