Skip to content

Simplify logical expression in Multiple Functions #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion fastapi_contrib/auth/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def authenticate(
if not authorization:
return False, None
scheme, credentials = get_authorization_scheme_param(authorization)
if not (authorization and scheme and credentials):
if not scheme or not credentials:
raise AuthenticationError("Not authenticated")
if scheme.lower() != "token":
raise AuthenticationError("Invalid authentication credentials")
Expand Down
3 changes: 1 addition & 2 deletions fastapi_contrib/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ def get_current_app() -> FastAPI:
:return: FastAPI app
"""
# TODO: cache this
app = resolve_dotted_path(settings.fastapi_app)
return app
return resolve_dotted_path(settings.fastapi_app)


def async_timing(func):
Expand Down
3 changes: 1 addition & 2 deletions fastapi_contrib/db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def get_db_client():
"""
from fastapi_contrib.db.client import MongoDBClient

client = MongoDBClient()
return client
return MongoDBClient()


def get_models() -> list:
Expand Down
17 changes: 8 additions & 9 deletions fastapi_contrib/exception_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ def parse_error(
"""

if isinstance(err.exc, EnumError):
permitted_values = ", ".join(
[f"'{val}'" for val in err.exc.enum_values]
)
permitted_values = ", ".join(f"'{val}'" for val in err.exc.enum_values)
message = f"Value is not a valid enumeration member; " \
f"permitted: {permitted_values}."
elif isinstance(err.exc, StrRegexError):
Expand All @@ -51,13 +49,14 @@ def parse_error(
name = str(err.loc_tuple()[0])
else:
name = "__all__"
elif (
len(err.loc_tuple()) == 2
or len(err.loc_tuple()) != 2
and len(err.loc_tuple()) == 1
):
name = str(err.loc_tuple()[0])
else:
if len(err.loc_tuple()) == 2:
name = str(err.loc_tuple()[0])
elif len(err.loc_tuple()) == 1:
name = str(err.loc_tuple()[0])
else:
name = "__all__"
name = "__all__"

if name in field_names:
return None
Expand Down
18 changes: 9 additions & 9 deletions fastapi_contrib/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ async def custom_route_handler(request: Request) -> Response:
return await original_route_handler(request)
except RequestValidationError as exc:
body = await request.body()
if not body:
status_code = 400
data = {
"code": status_code,
"detail": "Empty body for this request is not valid.",
"fields": [],
}
return UJSONResponse(data, status_code=status_code)
else:
if body:
raise exc

status_code = 400
data = {
"code": status_code,
"detail": "Empty body for this request is not valid.",
"fields": [],
}
return UJSONResponse(data, status_code=status_code)

return custom_route_handler
77 changes: 35 additions & 42 deletions fastapi_contrib/serializers/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,26 +137,23 @@ async def update_one(

:return: result of update operation
"""
if (
hasattr(self, "Meta")
and getattr(self.Meta, "model", None) is not None
):
data = {}
fields = self.dict(skip_defaults=skip_defaults)

if not array_fields:
array_fields = []

if array_fields:
tmp_data = {}
for i in array_fields:
tmp_data[i] = {"$each": fields.pop(i)}
data.update({"$push": tmp_data})
if fields:
data.update({"$set": fields})
return await self.Meta.model.update_one(
filter_kwargs=filter_kwargs, **data
)
if not hasattr(self, "Meta") or getattr(self.Meta, "model", None) is None:
return

data = {}
fields = self.dict(skip_defaults=skip_defaults)

if not array_fields:
array_fields = []

if array_fields:
tmp_data = {i: {"$each": fields.pop(i)} for i in array_fields}
data.update({"$push": tmp_data})
if fields:
data.update({"$set": fields})
return await self.Meta.model.update_one(
filter_kwargs=filter_kwargs, **data
)

async def update_many(
self,
Expand All @@ -170,26 +167,23 @@ async def update_many(

:return: result of update many operation
"""
if (
hasattr(self, "Meta")
and getattr(self.Meta, "model", None) is not None
):
data = {}
fields = self.dict(skip_defaults=skip_defaults)

if not array_fields:
array_fields = []

if array_fields:
tmp_data = {}
for i in array_fields:
tmp_data[i] = {"$each": fields.pop(i)}
data.update({"$push": tmp_data})
if fields:
data.update({"$set": fields})
return await self.Meta.model.update_many(
filter_kwargs=filter_kwargs, **data
)
if not hasattr(self, "Meta") or getattr(self.Meta, "model", None) is None:
return

data = {}
fields = self.dict(skip_defaults=skip_defaults)

if not array_fields:
array_fields = []

if array_fields:
tmp_data = {i: {"$each": fields.pop(i)} for i in array_fields}
data.update({"$push": tmp_data})
if fields:
data.update({"$set": fields})
return await self.Meta.model.update_many(
filter_kwargs=filter_kwargs, **data
)

def dict(self, *args, **kwargs) -> dict:
"""
Expand All @@ -212,8 +206,7 @@ def dict(self, *args, **kwargs) -> dict:
exclude.update(self.Meta.write_only_fields)

kwargs.update({"exclude": exclude})
original = super().dict(*args, **kwargs)
return original
return super().dict(*args, **kwargs)

class Meta(AbstractMeta):
...
Expand Down