Skip to content

Commit 04765ef

Browse files
committed
more refactoring
1 parent 4e6b8f6 commit 04765ef

2 files changed

Lines changed: 59 additions & 35 deletions

File tree

fastapi_jsonapi/views/view_base.py

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
from functools import partial
33
from typing import (
4+
Callable,
45
Dict,
56
Iterable,
67
List,
@@ -128,7 +129,56 @@ def update_known_included(
128129
included_objects[(included.id, included.type)] = included
129130

130131
@classmethod
131-
def process_db_items_and_create_prepare(
132+
def process_single_db_item_and_prepare_includes(
133+
cls,
134+
parent_db_item: TypeModel,
135+
previous_resource_type: str,
136+
related_field_name: str,
137+
included_objects: Dict[Tuple[str, str], TypeSchema],
138+
relationships_schema: Type[BaseModel],
139+
object_schema: Type[JSONAPIObjectSchema],
140+
process_db_item: Callable,
141+
):
142+
next_current_db_item = []
143+
cache_key = (str(parent_db_item.id), previous_resource_type)
144+
current_db_item = getattr(parent_db_item, related_field_name)
145+
current_is_single = False
146+
if not isinstance(current_db_item, Iterable):
147+
# hack to do less if/else
148+
current_db_item = [current_db_item]
149+
current_is_single = True
150+
relationship_data_items = []
151+
152+
for db_item in current_db_item:
153+
next_current_db_item.append(db_item)
154+
data_for_relationship, new_included = process_db_item(
155+
related_db_item=db_item,
156+
)
157+
158+
cls.update_known_included(
159+
included_objects=included_objects,
160+
new_included=new_included,
161+
)
162+
relationship_data_items.append(data_for_relationship)
163+
164+
if current_is_single:
165+
# if initially was single, get back one dict
166+
# hack to do less if/else
167+
relationship_data_items = relationship_data_items[0]
168+
169+
cls.update_related_object(
170+
relationship_data=relationship_data_items,
171+
relationships_schema=relationships_schema,
172+
object_schema=object_schema,
173+
included_objects=included_objects,
174+
cache_key=cache_key,
175+
related_field_name=related_field_name,
176+
)
177+
178+
return next_current_db_item
179+
180+
@classmethod
181+
def process_db_items_and_prepare_includes(
132182
cls,
133183
parent_db_items: List[TypeModel],
134184
previous_resource_type: str,
@@ -148,41 +198,16 @@ def process_db_items_and_create_prepare(
148198
next_current_db_item = []
149199

150200
for parent_db_item in parent_db_items:
151-
cache_key = (str(parent_db_item.id), previous_resource_type)
152-
current_db_item = getattr(parent_db_item, related_field_name)
153-
current_is_single = False
154-
if not isinstance(current_db_item, Iterable):
155-
# hack to do less if/else
156-
current_db_item = [current_db_item]
157-
current_is_single = True
158-
relationship_data_items = []
159-
160-
for db_item in current_db_item:
161-
next_current_db_item.append(db_item)
162-
data_for_relationship, new_included = process_db_item(
163-
related_db_item=db_item,
164-
)
165-
166-
cls.update_known_included(
167-
included_objects=included_objects,
168-
new_included=new_included,
169-
)
170-
relationship_data_items.append(data_for_relationship)
171-
172-
if current_is_single:
173-
# if initially was single, get back one dict
174-
# hack to do less if/else
175-
relationship_data_items = relationship_data_items[0]
176-
177-
cls.update_related_object(
178-
relationship_data=relationship_data_items,
201+
new_next_items = cls.process_single_db_item_and_prepare_includes(
202+
parent_db_item=parent_db_item,
203+
previous_resource_type=previous_resource_type,
204+
related_field_name=related_field_name,
205+
included_objects=included_objects,
179206
relationships_schema=relationships_schema,
180207
object_schema=object_schema,
181-
included_objects=included_objects,
182-
cache_key=cache_key,
183-
related_field_name=related_field_name,
208+
process_db_item=process_db_item,
184209
)
185-
210+
next_current_db_item.extend(new_next_items)
186211
return next_current_db_item
187212

188213
def process_include_with_nested(
@@ -222,7 +247,7 @@ def process_include_with_nested(
222247
# xxx: less if/else
223248
current_db_item = [current_db_item]
224249

225-
current_db_item = self.process_db_items_and_create_prepare(
250+
current_db_item = self.process_db_items_and_prepare_includes(
226251
parent_db_items=current_db_item,
227252
previous_resource_type=previous_resource_type,
228253
related_field_name=related_field_name,

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ extend-ignore = [
9494
"RUF001", # String contains ambiguous unicode character {confusable} (did you mean {representant}?)
9595
"RUF002", # Docstring contains ambiguous unicode character {confusable} (did you mean {representant}?)
9696
"RUF003", # Comment contains ambiguous unicode character {confusable} (did you mean {representant}?)
97-
"PLR0912", # too many branches (temp)
9897
]
9998

10099
[tool.ruff.per-file-ignores]

0 commit comments

Comments
 (0)