ecodev-core 0.0.18__py3-none-any.whl → 0.0.20__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 ecodev-core might be problematic. Click here for more details.

ecodev_core/__init__.py CHANGED
@@ -23,6 +23,7 @@ from ecodev_core.authentication import JwtAuth
23
23
  from ecodev_core.authentication import safe_get_user
24
24
  from ecodev_core.authentication import SCHEME
25
25
  from ecodev_core.authentication import Token
26
+ from ecodev_core.authentication import upsert_new_user
26
27
  from ecodev_core.backup import backup
27
28
  from ecodev_core.check_dependencies import check_dependencies
28
29
  from ecodev_core.check_dependencies import compute_dependencies
@@ -60,13 +61,13 @@ from ecodev_core.read_write import load_json_file
60
61
  from ecodev_core.read_write import make_dir
61
62
  from ecodev_core.read_write import write_json_file
62
63
  from ecodev_core.safe_utils import boolify
64
+ from ecodev_core.safe_utils import datify
63
65
  from ecodev_core.safe_utils import floatify
64
66
  from ecodev_core.safe_utils import intify
65
67
  from ecodev_core.safe_utils import safe_clt
66
68
  from ecodev_core.safe_utils import SafeTestCase
67
69
  from ecodev_core.safe_utils import SimpleReturn
68
70
  from ecodev_core.safe_utils import stringify
69
-
70
71
  __all__ = [
71
72
  'AUTH', 'Token', 'get_app_services', 'attempt_to_log', 'get_current_user', 'is_admin_user',
72
73
  'write_json_file', 'load_json_file', 'make_dir', 'check_dependencies', 'compute_dependencies',
@@ -78,4 +79,5 @@ __all__ = [
78
79
  'enum_converter', 'ServerSideFilter', 'get_rows', 'count_rows', 'ServerSideField', 'get_raw_df',
79
80
  'generic_insertion', 'custom_equal', 'is_authorized_user', 'get_method', 'AppActivity',
80
81
  'fastapi_monitor', 'dash_monitor', 'is_monitoring_user', 'get_recent_activities', 'select_user',
81
- 'get_access_token', 'safe_get_user', 'backup', 'group_by', 'get_excelfile']
82
+ 'get_access_token', 'safe_get_user', 'backup', 'group_by', 'get_excelfile', 'upsert_new_user',
83
+ 'datify']
ecodev_core/app_user.py CHANGED
@@ -2,7 +2,7 @@
2
2
  Module implementing the sqlmodel orm part of the user table
3
3
  """
4
4
  from pathlib import Path
5
- from typing import Dict
5
+ from typing import Any
6
6
  from typing import List
7
7
  from typing import Optional
8
8
  from typing import TYPE_CHECKING
@@ -39,7 +39,7 @@ class AppUser(SQLModel, table=True): # type: ignore
39
39
  rights: List['AppRight'] = Relationship(back_populates='user')
40
40
 
41
41
 
42
- def user_convertor(df: pd.DataFrame) -> List[Dict]:
42
+ def user_convertor(df: pd.DataFrame) -> List[Any]:
43
43
  """
44
44
  Dummy user convertor
45
45
  """
@@ -93,20 +93,23 @@ class JwtAuth(AuthenticationBackend):
93
93
  request.session.update(token)
94
94
  return True if token else False
95
95
 
96
- @staticmethod
97
- def authorized(form: Any):
96
+ def authorized(self, form: Any):
98
97
  """
99
98
  Check that the user information contained in the form corresponds to an admin user
100
99
  """
101
100
  with Session(engine) as session:
102
101
  try:
103
- token = attempt_to_log(form.get('username', ''), form.get('password', ''), session)
104
- if is_admin_user(token['access_token']):
105
- return token
106
- return None
102
+ return self.admin_token(form, session)
107
103
  except HTTPException:
108
104
  return None
109
105
 
106
+ def admin_token(self, form: Any, session: Session) -> Union[Dict[str, str], None]:
107
+ """
108
+ Unsafe attempt to retrieve the token, only return it if admin rights
109
+ """
110
+ token = attempt_to_log(form.get('username', ''), form.get('password', ''), session)
111
+ return token if is_admin_user(token['access_token']) else None
112
+
110
113
  async def logout(self, request: Request) -> bool:
111
114
  """
112
115
  Logout procedure: clears the cache
@@ -218,6 +221,20 @@ def is_monitoring_user(token: str = Depends(SCHEME)) -> AppUser:
218
221
  detail=MONITORING_ERROR, headers={'WWW-Authenticate': 'Bearer'})
219
222
 
220
223
 
224
+ def upsert_new_user(token: str, user: str, password: str = '') -> None:
225
+ """
226
+ Upsert a new user if not already present in db.
227
+
228
+ NB: this method RAISES a http error if he token is invalid
229
+ """
230
+ user_id = _verify_access_token(token).id
231
+ with Session(engine) as session:
232
+ if not session.exec(select(AppUser).where(col(AppUser.id) == user_id)).first():
233
+ session.add(AppUser(user=user, password=password, permission=Permission.Consultant,
234
+ id=user_id))
235
+ session.commit()
236
+
237
+
221
238
  def _create_access_token(data: Dict, tfa_value: Optional[str] = None) -> str:
222
239
  """
223
240
  Create an access token out of the passed data. Only called if credentials are valid
@@ -262,4 +279,3 @@ def _check_password(plain_password: Optional[str], hashed_password: str) -> bool
262
279
  Check the passed password (compare it to the passed encoded one).
263
280
  """
264
281
  return CONTEXT.verify(plain_password, hashed_password)
265
-
ecodev_core/safe_utils.py CHANGED
@@ -4,6 +4,7 @@ This boilerplate code is not to be touched under any circumstances.
4
4
  """
5
5
  import contextlib
6
6
  import shutil
7
+ from datetime import datetime
7
8
  from pathlib import Path
8
9
  from typing import Any
9
10
  from typing import Callable
@@ -170,6 +171,13 @@ def floatify(x: Union[str, float]) -> Union[float, None]:
170
171
  return _transformify(x, float)
171
172
 
172
173
 
174
+ def datify(date: str, date_format: str) -> Union[datetime, None]:
175
+ """
176
+ Safe conversion to a date format
177
+ """
178
+ return _transformify(date, lambda x: datetime.strptime(x, date_format))
179
+
180
+
173
181
  def _transformify(x: Union[Any, float], transformation: Callable) -> Union[Any, None]:
174
182
  """
175
183
  Safe conversion of a (Any, np.nan) value into a (Any,None) one thanks to transformation
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ecodev-core
3
- Version: 0.0.18
3
+ Version: 0.0.20
4
4
  Summary: Low level sqlmodel/fastapi/pydantic building blocks
5
5
  License: MIT
6
6
  Author: Thomas Epelbaum
@@ -1,9 +1,9 @@
1
- ecodev_core/__init__.py,sha256=BT9IRxEoI6GcvRx9Ldjo2JFLAshQ7OdSwHX8zDqMK5w,4368
1
+ ecodev_core/__init__.py,sha256=LssQSv0CD5SyuHK07j-0i_Dgawj-6tZghtzsnHTwpX0,4497
2
2
  ecodev_core/app_activity.py,sha256=_rU5uPfttHxXX5IaCuTA7K9We5w2qluJ3Xpf6i12HhY,3763
3
3
  ecodev_core/app_rights.py,sha256=RZPdDtydFqc_nFj96huKAc56BS0qS6ScKv4Kghqd6lc,726
4
- ecodev_core/app_user.py,sha256=qXvu0GOm2fAY_KxjkPEUIw7Rb9uxm2kvyFvcTTBQvEg,2949
4
+ ecodev_core/app_user.py,sha256=r1bqA4H08x53XmxmjwyGKl_PFjYQazzBbVErdkztqeE,2947
5
5
  ecodev_core/auth_configuration.py,sha256=ZjEB-N5HTo2jaEljwrh6q64Lh5qZ1NuKk8bbpIEcdYc,433
6
- ecodev_core/authentication.py,sha256=GLAYKuu-im7iGTI-pOrC-M-qSQBqz8JV0ayS_0yrAeE,9556
6
+ ecodev_core/authentication.py,sha256=XL_hPRw_jd1jFxASFmxB1GJqCeg8zY8S8GFt2Y1XoGE,10278
7
7
  ecodev_core/backup.py,sha256=9YZ79LkbLMSSoBJFmTr8u9_OPBbDmwasrmCJpqb7lkg,3055
8
8
  ecodev_core/check_dependencies.py,sha256=aFn8GI4eBbuJT8RxsfhSSnlpNYYj_LPOH-tZF0EqfKQ,6917
9
9
  ecodev_core/custom_equal.py,sha256=2gRn0qpyJ8-Kw9GQSueu0nLngLrRrwyMPlP6zqPac0U,899
@@ -18,8 +18,8 @@ ecodev_core/pandas_utils.py,sha256=bkobhqj_tWrFCgBGQwPJLwN59HJunNDeBKhnctWCUBE,1
18
18
  ecodev_core/permissions.py,sha256=dMaRQyjrF8Y51gkbkFvFsGVdzQGLZtA72IQ7REYamxg,320
19
19
  ecodev_core/pydantic_utils.py,sha256=e3GH50JmcpTmd2UgrB94QSwWOlOCW3WIlVdyX9C4T-U,741
20
20
  ecodev_core/read_write.py,sha256=auJ5bBJTVGkLRkiP_vZxVCX64B0Y-9qpsaDhovHmbas,996
21
- ecodev_core/safe_utils.py,sha256=uTHzLnKBoV_MiFsI65X-WYmgzLpIBH5Cio80KSLd6wg,5933
22
- ecodev_core-0.0.18.dist-info/LICENSE.md,sha256=jebQDe1ib9LAODuNvcSoo2CoqS6P0_q8--mMTICh_kI,1074
23
- ecodev_core-0.0.18.dist-info/METADATA,sha256=9KQl9pnlTiCgaAh4jLWvoH1kzzILU8dJ-Jm4_BG_lLA,3291
24
- ecodev_core-0.0.18.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
25
- ecodev_core-0.0.18.dist-info/RECORD,,
21
+ ecodev_core/safe_utils.py,sha256=JCfxo6fcznjsL-XHNJ1TKo1UvfJB83WT5jpTFmtJwsE,6160
22
+ ecodev_core-0.0.20.dist-info/LICENSE.md,sha256=jebQDe1ib9LAODuNvcSoo2CoqS6P0_q8--mMTICh_kI,1074
23
+ ecodev_core-0.0.20.dist-info/METADATA,sha256=RR6vZTi-k2VBUXggrp2_aXNDZ5j73oGsn3LiVwuv6AA,3291
24
+ ecodev_core-0.0.20.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
25
+ ecodev_core-0.0.20.dist-info/RECORD,,