Skip to content

Commit

Permalink
Fix: assign join statement when user not select foreign table
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisLuii committed Sep 5, 2021
1 parent f6a33ec commit dd258c4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
19 changes: 7 additions & 12 deletions src/fastapi_quickcrud/misc/abstract_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from starlette.responses import Response, RedirectResponse

from .exceptions import FindOneApiNotRegister


# class ResultParserBase(ABC):
#
# @abstractmethod
Expand Down Expand Up @@ -135,11 +133,10 @@ def find_one_sub_func(sql_execute_result, response_model, fastapi_response, **kw
result[key][foreign_column] = value_
else:
result[key_] = value_
if 'join_mode' in kwargs:
join = kwargs['join_mode']
if join:
result = group_find_many_join([result])[0]
# result = parse_obj_as(response_model, data_dict)

join = kwargs.get('join_mode', None)
if join:
result = group_find_many_join([result])[0]
fastapi_response.headers["x-total-count"] = str(1)
return result

Expand All @@ -160,7 +157,7 @@ def find_many_sub_func(response_model, sql_execute_result, fastapi_response, **k
# print(dir(table))
# print(table._asdict())
# FIXME handle NO_CONTENT
join = kwargs['join_mode']
join = kwargs.get('join_mode', None)
result = sql_execute_result.fetchall()
if not result:
return Response(status_code=HTTPStatus.NO_CONTENT)
Expand Down Expand Up @@ -284,7 +281,7 @@ def delete_many_sub_func(self, response_model, sql_execute_result, fastapi_respo
if not sql_execute_result.rowcount:
return Response(status_code=HTTPStatus.NO_CONTENT)

deleted_rows= sql_execute_result.fetchall()
deleted_rows = sql_execute_result.fetchall()
deleted_rows_dict = [dict(deleted_row) for deleted_row in deleted_rows]
result = parse_obj_as(response_model, deleted_rows_dict)
fastapi_response.headers["x-total-count"] = str(len(deleted_rows_dict))
Expand Down Expand Up @@ -594,14 +591,12 @@ def post_redirect_get_sub_func(self, response_model, sql_execute_result, fastapi
redirect_url = fastapi_request.url.path + "/" + str(primary_key_field)
return redirect_url


def get_post_redirect_get_url(self, response_model, sql_execute_result, fastapi_request):
redirect_url = self.post_redirect_get_sub_func(response_model, sql_execute_result, fastapi_request)
header_dict = {i[0].decode("utf-8"): i[1].decode("utf-8") for i in fastapi_request.headers.__dict__['_list']}
redirect_url += f'?{urlencode(header_dict)}'
return redirect_url


async def async_post_redirect_get(self, *, response_model, sql_execute_result, fastapi_request, **kwargs):
session = kwargs['session']
if not self.has_end_point(fastapi_request):
Expand All @@ -626,4 +621,4 @@ def post_redirect_get(self, *, response_model, sql_execute_result, fastapi_reque
self.commit(session)
return RedirectResponse(redirect_url,
status_code=HTTPStatus.SEE_OTHER
)
)
7 changes: 4 additions & 3 deletions src/fastapi_quickcrud/misc/abstract_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ def get_join_select_fields(self, join_mode):
return join_table_instance_list


def get_join_by_excpression(self,stmt: BinaryExpression, join_mode) -> BinaryExpression:
def get_join_by_excpression(self,stmt: BinaryExpression, join_mode = None) -> BinaryExpression:
if not join_mode:
return stmt
for join_table, data in join_mode.items():
for local_reference in data['local_reference_pairs_set']:
local = local_reference['local']['local_column']
Expand Down Expand Up @@ -129,8 +131,7 @@ def get_many(self, *,
if order_by_query_list:
stmt = stmt.order_by(*order_by_query_list)
stmt = stmt.limit(limit).offset(offset)
if join_mode:
stmt = self.get_join_by_excpression(stmt,join_mode=join_mode)
stmt = self.get_join_by_excpression(stmt,join_mode=join_mode)

return stmt

Expand Down

0 comments on commit dd258c4

Please sign in to comment.