ul-api-utils 9.0.0a2__py3-none-any.whl → 9.0.0a4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ul-api-utils might be problematic. Click here for more details.

@@ -1,6 +1,7 @@
1
1
  import json
2
2
  from typing import NamedTuple, Iterable, Any, List, Tuple, Dict, Optional
3
3
 
4
+ from flask_sqlalchemy.query import Query
4
5
  from pydantic import model_validator, BaseModel
5
6
 
6
7
  from ul_api_utils.errors import SimpleValidateApiError
@@ -14,11 +15,7 @@ class ApiRequestQueryPagination(NamedTuple):
14
15
  offset: int
15
16
  per_page: int
16
17
 
17
- def mk_sqlalchemy_pagination(self, items: Iterable[Any], total: int, query: Any = None) -> 'ApiPagination':
18
- if has_already_imported_db():
19
- from flask_sqlalchemy import Pagination
20
- return Pagination(total=total, query=query, per_page=self.per_page, page=self.page, items=items) # type: ignore
21
- assert query is None
18
+ def mk_item_pagination(self, items: Iterable[Any], total: int) -> ApiPagination:
22
19
  return ApiPagination(
23
20
  total=total,
24
21
  per_page=self.per_page,
@@ -26,6 +23,12 @@ class ApiRequestQueryPagination(NamedTuple):
26
23
  items=items,
27
24
  )
28
25
 
26
+ # TODO: Not sure that is worked. Check it if will be found usages
27
+ def mk_sqlalchemy_pagination(self, items: Iterable[Any], total: int, query: Query = None) -> 'ApiPagination':
28
+ if has_already_imported_db() and query:
29
+ return query.paginate(total=total, query=query, per_page=self.per_page, page=self.page, items=items) # type: ignore
30
+ return self.mk_item_pagination(items, total)
31
+
29
32
 
30
33
  class ApiRequestQuerySortBy(NamedTuple):
31
34
  params: List[Tuple[str, str]]
@@ -293,13 +293,10 @@ class ApiResource:
293
293
  mimetype: Optional[str], # it will be auto-detected by extension if mimetype==None
294
294
  as_attachment: bool = False,
295
295
  download_name: Optional[str] = None,
296
- attachment_filename: Optional[str] = None,
297
296
  conditional: bool = True,
298
297
  etag: Union[bool, str] = True,
299
- add_etags: Optional[bool] = None,
300
298
  last_modified: Optional[Union[datetime, int, float]] = None,
301
299
  max_age: Optional[Union[int, Callable[[Optional[str]], Optional[int]]]] = None,
302
- cache_timeout: Optional[int] = None,
303
300
  ) -> FileApiResponse:
304
301
  ApiResourceType.FILE.validate(self._type)
305
302
  if isinstance(path_or_file, str) and not os.path.exists(path_or_file):
@@ -311,13 +308,10 @@ class ApiResource:
311
308
  mimetype=mimetype,
312
309
  as_attachment=as_attachment,
313
310
  download_name=download_name,
314
- attachment_filename=attachment_filename,
315
311
  conditional=conditional,
316
312
  etag=etag,
317
- add_etags=add_etags,
318
313
  last_modified=last_modified,
319
314
  max_age=max_age,
320
- cache_timeout=cache_timeout,
321
315
  )
322
316
 
323
317
  def response_created_ok(self, payload: 'TPayloadInputUnion', total_count: Optional[int] = None) -> JsonApiResponse[Any]:
@@ -93,13 +93,10 @@ class FileApiResponse(ApiResponse):
93
93
  mimetype: Optional[str] = None # it will be auto-detected by extension if mimetype==None
94
94
  as_attachment: bool = False
95
95
  download_name: Optional[str] = None
96
- attachment_filename: Optional[str] = None
97
96
  conditional: bool = True
98
97
  etag: Union[bool, str] = True
99
- add_etags: Optional[bool] = None
100
98
  last_modified: Optional[Union[datetime, int, float]] = None
101
99
  max_age: Optional[Union[int, Callable[[Optional[str]], Optional[int]]]] = None
102
- cache_timeout: Optional[int] = None
103
100
 
104
101
  @classmethod
105
102
  def _internal_use__mk_schema(cls, inner_type: Optional[Type[BaseModel]]) -> Type[BaseModel]:
@@ -113,13 +110,10 @@ class FileApiResponse(ApiResponse):
113
110
  mimetype=self.mimetype,
114
111
  as_attachment=self.as_attachment,
115
112
  download_name=self.download_name,
116
- attachment_filename=self.attachment_filename,
117
113
  conditional=self.conditional,
118
114
  etag=self.etag,
119
- add_etags=self.add_etags,
120
115
  last_modified=self.last_modified,
121
116
  max_age=self.max_age,
122
- cache_timeout=self.cache_timeout,
123
117
  )
124
118
 
125
119
  resp.headers.update(self.headers)
@@ -45,7 +45,7 @@ from ul_api_utils.utils.cached_per_request import cached_per_request
45
45
  from ul_api_utils.utils.constants import TKwargs
46
46
  from ul_api_utils.utils.jinja.t_url_for import t_url_for
47
47
  from ul_api_utils.utils.jinja.to_pretty_json import to_pretty_json
48
- from ul_api_utils.utils.json_encoder import CustomJSONEncoder, SocketIOJsonWrapper
48
+ from ul_api_utils.utils.json_encoder import CustomJSONProvider, SocketIOJsonWrapper
49
49
  from ul_api_utils.utils.load_modules import load_modules_by_template
50
50
  from ul_api_utils.utils.uuid_converter import UUID4Converter
51
51
 
@@ -225,7 +225,7 @@ class ApiSdk:
225
225
  )
226
226
  self._flask_app_cache = flask_app
227
227
 
228
- flask_app.json_encoder = CustomJSONEncoder # type: ignore
228
+ flask_app.json = CustomJSONProvider(flask_app) # type: ignore
229
229
 
230
230
  flask_app.url_map.converters['uuid'] = UUID4Converter
231
231
 
@@ -7,9 +7,18 @@ from enum import Enum
7
7
  from json import JSONEncoder
8
8
  from typing import Dict, Any, Union, List, Optional, TYPE_CHECKING
9
9
  from uuid import UUID
10
+
11
+ from flask.json.provider import DefaultJSONProvider
10
12
  from frozendict import frozendict
11
13
  from pydantic import BaseModel
12
14
 
15
+ from flask_sqlalchemy.query import Query
16
+ from flask_sqlalchemy.model import Model, DefaultMeta
17
+
18
+ from sqlalchemy.orm import Query, registry
19
+
20
+ from ul_db_utils.modules.postgres_modules.db import DbModel
21
+ from ul_db_utils.model.base_model import BaseModel as DbBaseModel
13
22
  from ul_api_utils.utils.imports import has_already_imported_db
14
23
 
15
24
  if TYPE_CHECKING:
@@ -26,11 +35,6 @@ def to_dict(obj: 'TDictable') -> Optional[Dict[str, Any]]:
26
35
  if isinstance(obj, BaseModel):
27
36
  return obj.model_dump()
28
37
  if has_already_imported_db():
29
- from ul_db_utils.modules.postgres_modules.db import DbModel
30
- from flask_sqlalchemy import BaseQuery, Model
31
- from sqlalchemy.orm import registry
32
- from ul_db_utils.model.base_model import BaseModel as DbBaseModel
33
-
34
38
  if isinstance(obj, DbBaseModel) or isinstance(obj, DbModel):
35
39
  return obj.to_dict()
36
40
  if isinstance(obj, Model):
@@ -38,7 +42,7 @@ def to_dict(obj: 'TDictable') -> Optional[Dict[str, Any]]:
38
42
  for field in (x for x in dir(obj) if not x.startswith('_') and x != 'metadata'):
39
43
  val = obj.__getattribute__(field)
40
44
  # is this field method defination, or an SQLalchemy object
41
- if not hasattr(val, "__call__") and not isinstance(val, BaseQuery): # noqa: B004
45
+ if not hasattr(val, "__call__") and not isinstance(val, Query): # noqa: B004
42
46
  if isinstance(val, datetime):
43
47
  val = str(val.isoformat())
44
48
  if isinstance(val, UUID):
@@ -76,10 +80,6 @@ class CustomJSONEncoder(JSONEncoder):
76
80
  return str(obj.__html__())
77
81
 
78
82
  if has_already_imported_db():
79
- from ul_db_utils.model.base_model import BaseModel as DbBaseModel
80
- from flask_sqlalchemy import BaseQuery, Model, DefaultMeta
81
- from sqlalchemy.orm import Query, registry
82
-
83
83
  if isinstance(obj, DbBaseModel):
84
84
  return obj.to_dict()
85
85
  if isinstance(obj, Query) or isinstance(obj, DefaultMeta):
@@ -89,7 +89,7 @@ class CustomJSONEncoder(JSONEncoder):
89
89
  for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']:
90
90
  val = obj.__getattribute__(field)
91
91
  # is this field method defination, or an SQLalchemy object
92
- if not hasattr(val, "__call__") and not isinstance(val, BaseQuery): # noqa: B004
92
+ if not hasattr(val, "__call__") and not isinstance(val, Query): # noqa: B004
93
93
  if isinstance(val, datetime):
94
94
  val = str(val.isoformat())
95
95
  if isinstance(val, UUID):
@@ -113,3 +113,12 @@ class SocketIOJsonWrapper:
113
113
  @staticmethod
114
114
  def loads(*args: Any, **kwargs: Any) -> Any:
115
115
  return json.loads(*args, **kwargs)
116
+
117
+
118
+ class CustomJSONProvider(DefaultJSONProvider):
119
+ def __init__(self, app):
120
+ super().__init__(app)
121
+ self.encoder = CustomJSONEncoder()
122
+
123
+ def default(self, obj) -> Union[str, Dict[str, Any], List[Any], None]:
124
+ return self.encoder.default(obj)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ul-api-utils
3
- Version: 9.0.0a2
3
+ Version: 9.0.0a4
4
4
  Summary: Python api utils
5
5
  Author: Unic-lab
6
6
  Author-email:
@@ -25,13 +25,13 @@ ul_api_utils/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
25
25
  ul_api_utils/sentry.py,sha256=UH_SwZCAoKH-Nw5B9CVQMoF-b1BJOp-ZTzwqUZ3Oq84,1801
26
26
  ul_api_utils/access/__init__.py,sha256=NUyRNvCVwfePrfdn5ATFVfHeSO3iq4-Syeup4IAZGzs,4526
27
27
  ul_api_utils/api_resource/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
28
- ul_api_utils/api_resource/api_request.py,sha256=6Ag2trKIkenhYYPU2--hnfNJC5lLgBxVQ7jXj5D1eXM,3303
29
- ul_api_utils/api_resource/api_resource.py,sha256=j-E8KJiXWS1L0oVIerJZlbGpDL2ijlQrck4GrJPWPyE,17840
28
+ ul_api_utils/api_resource/api_request.py,sha256=gB7imtE1uTI8eSTFql3Zk92kkQKD47YN8KQfshY-DQc,3488
29
+ ul_api_utils/api_resource/api_resource.py,sha256=WMGYtBKoobpRJVNLLWdbuVpq2IcIAI9kK5_Hgd4TqqM,17575
30
30
  ul_api_utils/api_resource/api_resource_config.py,sha256=l9OYJy75UZLshOkEQDO5jlhXeb5H4HDPu-nLOjuoexw,769
31
31
  ul_api_utils/api_resource/api_resource_error_handling.py,sha256=E0SWpjFSIP-4SumbgzrHtFuFiGe9q38WsvLROt0YcPE,1168
32
32
  ul_api_utils/api_resource/api_resource_fn_typing.py,sha256=n5bux_azlV8aRtRQdoIP553tXWHCi7P-brKUAgj502E,18970
33
33
  ul_api_utils/api_resource/api_resource_type.py,sha256=mgjSQI3swGpgpLI6y35LYtFrdN-kXyV5cQorwGW7h6g,462
34
- ul_api_utils/api_resource/api_response.py,sha256=bfnWo5GZUCzwJm8rDfzcpuYL4l63pBedJ0tTguP--qw,10069
34
+ ul_api_utils/api_resource/api_response.py,sha256=OnIQepPuJoTm57SGmwTbdosKSLzSZnyEquZp9mjO6vE,9804
35
35
  ul_api_utils/api_resource/api_response_db.py,sha256=ucY6ANPlHZml7JAbvq-PL85z0bvERTjEJKvz-REPyok,888
36
36
  ul_api_utils/api_resource/api_response_payload_alias.py,sha256=FoD0LhQGZ2T8A5-VKRX5ADyzSgm7_dd3qxU2BgCVXkA,587
37
37
  ul_api_utils/api_resource/db_types.py,sha256=NxHBYetUogWZow7Vhd3e00Y3L62-dxjwRzJlXywYlV4,439
@@ -65,7 +65,7 @@ ul_api_utils/internal_api/__tests__/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQ
65
65
  ul_api_utils/internal_api/__tests__/internal_api.py,sha256=X2iopeso6vryszeeA__lcqXQVtz3Nwt3ngH7M4OuN1U,1116
66
66
  ul_api_utils/internal_api/__tests__/internal_api_content_type.py,sha256=mfiYPkzKtfZKFpi4RSnWAoCd6mRijr6sFsa2TF-s5t8,749
67
67
  ul_api_utils/modules/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
68
- ul_api_utils/modules/api_sdk.py,sha256=0fA26r7165BN4kYtLoCLcBZC7xyt2gvoDKVcf7OQ-ho,26425
68
+ ul_api_utils/modules/api_sdk.py,sha256=BOQdHXzajmRW2tnGym-4PjLi_ypTn54KDJt3DaaUcss,26430
69
69
  ul_api_utils/modules/api_sdk_config.py,sha256=ZUR48tIJeFlPJTSjyXzKfXaCKPtfqeaA0mlLX42SSFY,2137
70
70
  ul_api_utils/modules/api_sdk_jwt.py,sha256=2XRfb0LxHUnldSL67S60v1uyoDpVPNaq4zofUtkeg88,15112
71
71
  ul_api_utils/modules/intermediate_state.py,sha256=7ZZ3Sypbb8LaSfrVhaXaWRDnj8oyy26NUbmFK7vr-y4,1270
@@ -111,7 +111,7 @@ ul_api_utils/utils/deprecated.py,sha256=xR3ELgoDj7vJEY4CAYeEhdbtSJTfkukbjxcULtpM
111
111
  ul_api_utils/utils/flags.py,sha256=AYN5nKWp4-uu6PSlPptL7ZiLqr3Pu-x5dffF6SBsqfg,957
112
112
  ul_api_utils/utils/imports.py,sha256=i8PhoD0c_jnWTeXt_VxW_FihynwXSL_dHRT7jQiFyXE,376
113
113
  ul_api_utils/utils/instance_checks.py,sha256=9punTfY5uabuJhmSGLfTgBqRderoFysCXBSI8rvbPco,467
114
- ul_api_utils/utils/json_encoder.py,sha256=ra43WhkAadIvVpUY3uik_Nzg0LVXUXTGYvx4Gx7fD_g,4578
114
+ ul_api_utils/utils/json_encoder.py,sha256=tLcO8da4iytj9sCzuD8hP7XO3qPujBmGbuhAOOX9vsI,4711
115
115
  ul_api_utils/utils/load_modules.py,sha256=_CPmQuB6o_33FE6zFl_GyO5xS5gmjfNffB6k-cglKAA,685
116
116
  ul_api_utils/utils/token_check.py,sha256=-Quuh8gOs9fNE1shYhdiMpQedafsLN7MB2ilSxG_F8E,489
117
117
  ul_api_utils/utils/token_check_through_request.py,sha256=OyyObu6Btk9br7auIYvWcMULhNznNSD5T0mWOwZX7Uk,663
@@ -148,9 +148,9 @@ ul_api_utils/validators/validate_empty_object.py,sha256=3Ck_iwyJE_M5e7l6s1i88aqb
148
148
  ul_api_utils/validators/validate_uuid.py,sha256=EfvlRirv2EW0Z6w3s8E8rUa9GaI8qXZkBWhnPs8NFrA,257
149
149
  ul_api_utils/validators/__tests__/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
150
150
  ul_api_utils/validators/__tests__/test_custom_fields.py,sha256=20gLlnm1Ithsbbz3NIUXVAd92lW6YwVRSg_nETZhfaI,1442
151
- ul_api_utils-9.0.0a2.dist-info/LICENSE,sha256=6Qo8OdcqI8aGrswJKJYhST-bYqxVQBQ3ujKdTSdq-80,1062
152
- ul_api_utils-9.0.0a2.dist-info/METADATA,sha256=0dKlK__iEBPXqpkoGQI3TMWA4DLK_vCWs8P13qCwalc,14714
153
- ul_api_utils-9.0.0a2.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
154
- ul_api_utils-9.0.0a2.dist-info/entry_points.txt,sha256=8tL3ySHWTyJMuV1hx1fHfN8zumDVOCOm63w3StphkXg,53
155
- ul_api_utils-9.0.0a2.dist-info/top_level.txt,sha256=1XsW8iOSFaH4LOzDcnNyxHpHrbKU3fSn-aIAxe04jmw,21
156
- ul_api_utils-9.0.0a2.dist-info/RECORD,,
151
+ ul_api_utils-9.0.0a4.dist-info/LICENSE,sha256=6Qo8OdcqI8aGrswJKJYhST-bYqxVQBQ3ujKdTSdq-80,1062
152
+ ul_api_utils-9.0.0a4.dist-info/METADATA,sha256=vJjbnlXj9JFJI9AhlxvB_Z1rVKR7I5Ih1EY8RhfCo7E,14714
153
+ ul_api_utils-9.0.0a4.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
154
+ ul_api_utils-9.0.0a4.dist-info/entry_points.txt,sha256=8tL3ySHWTyJMuV1hx1fHfN8zumDVOCOm63w3StphkXg,53
155
+ ul_api_utils-9.0.0a4.dist-info/top_level.txt,sha256=1XsW8iOSFaH4LOzDcnNyxHpHrbKU3fSn-aIAxe04jmw,21
156
+ ul_api_utils-9.0.0a4.dist-info/RECORD,,