arpakitlib 1.8.156__py3-none-any.whl → 1.8.158__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.
@@ -0,0 +1,11 @@
1
+ from project.core.util import setup_logging
2
+ from project.sqlalchemy_db_.sqlalchemy_db import get_cached_sqlalchemy_db
3
+
4
+
5
+ def __command():
6
+ setup_logging()
7
+ get_cached_sqlalchemy_db().remove_rows_from_tables()
8
+
9
+
10
+ if __name__ == '__main__':
11
+ __command()
@@ -2,6 +2,7 @@ import asyncio
2
2
  import datetime as dt
3
3
  import logging
4
4
  import smtplib
5
+ import ssl
5
6
  from email.message import EmailMessage
6
7
 
7
8
  import aiosmtplib
@@ -12,79 +13,112 @@ from project.core.util import setup_logging
12
13
  _logger = logging.getLogger(__name__)
13
14
 
14
15
 
16
+ def _build_message(to_email: str, subject: str, html_content: str) -> EmailMessage:
17
+ msg = EmailMessage()
18
+ msg["From"] = get_cached_settings().email_smtp_user
19
+ msg["To"] = to_email.strip()
20
+ msg["Subject"] = subject
21
+ msg.add_alternative(html_content, subtype="html")
22
+ return msg
23
+
24
+
15
25
  def sync_send_email(
16
26
  *,
17
27
  to_email: str,
18
- subject: str = "Gamer.Market",
28
+ subject: str = get_cached_settings().project_name,
19
29
  html_content: str,
20
- emulate: bool = False
30
+ emulate: bool = False,
21
31
  ):
22
- to_email = to_email.strip()
23
-
24
32
  if emulate:
25
- _logger.info(f"emulate email sending, {to_email=}, {subject=}, {html_content=}")
33
+ _logger.info(f"emulate email sending, to_email={to_email!r}, subject={subject!r}")
26
34
  return
27
35
 
28
- message = EmailMessage()
29
- message["From"] = get_cached_settings().email_smtp_user
30
- message["To"] = to_email
31
- message["Subject"] = subject
32
- message.add_alternative(html_content, subtype="html")
33
-
34
- with smtplib.SMTP_SSL(
35
- host=get_cached_settings().email_smtp_hostname,
36
- port=get_cached_settings().email_smtp_port,
37
- timeout=dt.timedelta(seconds=15).total_seconds()
38
-
39
- ) as server:
40
- server.login(
41
- get_cached_settings().email_smtp_user,
42
- get_cached_settings().email_smtp_password
43
- )
44
- server.send_message(message)
45
-
46
- _logger.info(f"email was send, {to_email=}")
36
+ if get_cached_settings().email_smtp_port == 465:
37
+ _logger.info("using port 465 (SSL)")
38
+ with smtplib.SMTP_SSL(
39
+ host=get_cached_settings().email_smtp_hostname,
40
+ port=465,
41
+ timeout=dt.timedelta(seconds=30).total_seconds(),
42
+ context=ssl.create_default_context(),
43
+ ) as server:
44
+ server.login(
45
+ get_cached_settings().email_smtp_user,
46
+ get_cached_settings().email_smtp_password,
47
+ )
48
+ server.send_message(_build_message(to_email, subject, html_content))
49
+
50
+ elif get_cached_settings().email_smtp_port == 587:
51
+ _logger.info("using port 587 (STARTTLS)")
52
+ with smtplib.SMTP(
53
+ host=get_cached_settings().email_smtp_hostname,
54
+ port=587,
55
+ timeout=dt.timedelta(seconds=30).total_seconds(),
56
+ ) as server:
57
+ server.ehlo()
58
+ server.starttls(context=ssl.create_default_context())
59
+ server.ehlo()
60
+ server.login(
61
+ get_cached_settings().email_smtp_user,
62
+ get_cached_settings().email_smtp_password,
63
+ )
64
+ server.send_message(_build_message(to_email, subject, html_content))
65
+ else:
66
+ raise ValueError("Unsupported SMTP port")
67
+
68
+ _logger.info(f"email was sent, to_email={to_email!r}")
47
69
 
48
70
 
49
71
  async def async_send_email(
50
72
  *,
51
73
  to_email: str,
52
- subject: str = "Gamer.Market",
74
+ subject: str = get_cached_settings().project_name,
53
75
  html_content: str,
54
- emulate: bool = False
76
+ emulate: bool = False,
55
77
  ):
56
- to_email = to_email.strip()
57
-
58
78
  if emulate:
59
- _logger.info(f"emulate email sending, {to_email=}, {subject=}, {html_content=}")
79
+ _logger.info(f"emulate email sending, to_email={to_email!r}, subject={subject!r}")
60
80
  return
61
81
 
62
- message = EmailMessage()
63
- message["From"] = get_cached_settings().email_smtp_user
64
- message["To"] = to_email
65
- message["Subject"] = subject
66
- message.add_alternative(html_content, subtype="html")
67
-
68
- await aiosmtplib.send(
69
- message,
70
- hostname=get_cached_settings().email_smtp_hostname,
71
- port=get_cached_settings().email_smtp_port,
72
- username=get_cached_settings().email_smtp_user,
73
- password=get_cached_settings().email_smtp_password,
74
- use_tls=True,
75
- timeout=dt.timedelta(seconds=15).total_seconds()
76
- )
82
+ if get_cached_settings().email_smtp_port == 465:
83
+ _logger.info("using port 465 (SSL)")
84
+ await aiosmtplib.send(
85
+ _build_message(to_email, subject, html_content),
86
+ hostname=get_cached_settings().email_smtp_hostname,
87
+ port=get_cached_settings().email_smtp_port,
88
+ username=get_cached_settings().email_smtp_user,
89
+ password=get_cached_settings().email_smtp_password,
90
+ use_tls=True,
91
+ start_tls=False,
92
+ timeout=dt.timedelta(seconds=30).total_seconds(),
93
+ tls_context=ssl.create_default_context(),
94
+ )
95
+
96
+ elif get_cached_settings().email_smtp_port == 587:
97
+ _logger.info("using port 587 (STARTTLS)")
98
+ await aiosmtplib.send(
99
+ _build_message(to_email, subject, html_content),
100
+ hostname=get_cached_settings().email_smtp_hostname,
101
+ port=get_cached_settings().email_smtp_port,
102
+ username=get_cached_settings().email_smtp_user,
103
+ password=get_cached_settings().email_smtp_password,
104
+ use_tls=False,
105
+ start_tls=True,
106
+ timeout=dt.timedelta(seconds=30).total_seconds(),
107
+ tls_context=ssl.create_default_context(),
108
+ )
109
+ else:
110
+ raise ValueError("Unsupported SMTP port")
77
111
 
78
- _logger.info(f"email was send, {to_email=}")
112
+ _logger.info(f"email was sent, to_email={to_email!r}")
79
113
 
80
114
 
81
115
  async def __async_example():
82
116
  setup_logging()
83
117
  await async_send_email(
84
118
  to_email="arpakit@gmail.com",
85
- html_content="Hello world 2"
119
+ html_content="Hello world 2",
86
120
  )
87
121
 
88
122
 
89
- if __name__ == '__main__':
123
+ if __name__ == "__main__":
90
124
  asyncio.run(__async_example())
@@ -220,19 +220,27 @@ class SQLAlchemyDb:
220
220
  self.remove_celery_tables_data()
221
221
  self._logger.info("all reinited")
222
222
 
223
- def remove_rows_from_tables(self):
223
+ def remove_rows_from_tables(self, exclude_tables: list[str] | None = None):
224
+ if exclude_tables is None:
225
+ exclude_tables = []
224
226
  with self.new_session() as session:
225
227
  for table_name, table in BaseDBM.metadata.tables.items():
226
- session.execute(sqlalchemy.delete(table))
228
+ if table_name not in exclude_tables:
229
+ session.execute(sqlalchemy.delete(table))
227
230
  session.commit()
228
- self._logger.info(f"rows from tables ({BaseDBM.metadata.tables.keys()}) were removed")
231
+ removed_tables = [t for t in BaseDBM.metadata.tables.keys() if t not in exclude_tables]
232
+ self._logger.info(f"rows from tables ({removed_tables}) were removed")
229
233
 
230
- async def async_remove_rows_from_tables(self):
234
+ async def async_remove_rows_from_tables(self, exclude_tables: list[str] | None = None):
235
+ if exclude_tables is None:
236
+ exclude_tables = []
231
237
  async with self.new_async_session() as async_session:
232
238
  for table_name, table in BaseDBM.metadata.tables.items():
233
- await async_session.execute(sqlalchemy.delete(table))
239
+ if table_name not in exclude_tables:
240
+ await async_session.execute(sqlalchemy.delete(table))
234
241
  await async_session.commit()
235
- self._logger.info(f"rows from tables ({BaseDBM.metadata.tables.keys()}) were removed")
242
+ removed_tables = [t for t in BaseDBM.metadata.tables.keys() if t not in exclude_tables]
243
+ self._logger.info(f"rows from tables ({removed_tables}) were removed")
236
244
 
237
245
  def check_conn(self):
238
246
  self.engine.connect()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: arpakitlib
3
- Version: 1.8.156
3
+ Version: 1.8.158
4
4
  Summary: arpakitlib
5
5
  License: Apache-2.0
6
6
  Keywords: arpakitlib,arpakit,arpakit-company,arpakitcompany,arpakit_company
@@ -53,6 +53,7 @@ arpakitlib/_arpakit_project_template_v_5/command/reinit_all_sqlalchemy_db.py,sha
53
53
  arpakitlib/_arpakit_project_template_v_5/command/reinit_json_db.py,sha256=NYY1dPVbJ_mO9aCu8VTiDT7kfloiGvG624hJ_5g2tBM,318
54
54
  arpakitlib/_arpakit_project_template_v_5/command/reinit_sqlalchemy_db.py,sha256=aCoLtUvB_dubMHMGlOFgVzxkhhPAL4lHeMU0LM4CBCs,343
55
55
  arpakitlib/_arpakit_project_template_v_5/command/remove_operations.py,sha256=l1BMAT9kQMy_aZ2ZNc20vTi1xXktmDrJvKDvo85b8xA,326
56
+ arpakitlib/_arpakit_project_template_v_5/command/remove_rows_from_tables_sqlalchemy_db.py,sha256=9XDeYhjdELLq1T7ArskaMjzqpGerYuP38PjOwUFDnqQ,259
56
57
  arpakitlib/_arpakit_project_template_v_5/command/remove_story_logs.py,sha256=l-whsYahKY6ssk-c24ufiybF-U24iFqMdkNlnlASm2U,498
57
58
  arpakitlib/_arpakit_project_template_v_5/command/rm_all_records_in_json_db.py,sha256=qF5c_Aso7XpNcBZOInz7MWXX9Wm8L73rcOZBKGXlFfg,326
58
59
  arpakitlib/_arpakit_project_template_v_5/command/set_tg_bot_commands.py,sha256=6HbuL6acw-U8vkUdqLVIEffe_FcBsDWRKZ90AakZbv8,456
@@ -358,7 +359,7 @@ arpakitlib/_arpakit_project_template_v_5/project/tg_bot_notifier/tg_bot_notifier
358
359
  arpakitlib/_arpakit_project_template_v_5/project/util/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
359
360
  arpakitlib/_arpakit_project_template_v_5/project/util/arpakitlib_project_template_util.py,sha256=syA_IuszHVub0zm0sVdB4_7rPJXwXRW4JmQ4qHbjXPk,396
360
361
  arpakitlib/_arpakit_project_template_v_5/project/util/etc.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
361
- arpakitlib/_arpakit_project_template_v_5/project/util/send_email_.py,sha256=ehpBiIt6vpUXeuIY_tvr-80LGM7VBCUhgASGZpWy-Ok,2387
362
+ arpakitlib/_arpakit_project_template_v_5/project/util/send_email_.py,sha256=J1E9ee7s5gd3s0i_GseeND4kyNXkJVwLUFHR3s8JWF4,4140
362
363
  arpakitlib/_arpakit_project_template_v_5/todo.txt,sha256=q132Jbx229ThY77S3YiN-Cj5AVm7k1VlJcMYIbZUHUY,3
363
364
  arpakitlib/ar_additional_model_util.py,sha256=GFg-glLCxH9X95R2bhTJsscVwv37FgE1qbaAAyXrnIE,917
364
365
  arpakitlib/ar_aiogram_util.py,sha256=4bizX5Xg-E2-r2TXXGQGanJozsIWPVf5luO3vKUN8p8,8471
@@ -402,13 +403,13 @@ arpakitlib/ar_schedule_uust_api_client_util.py,sha256=rXI2_3OTaIBgR-GixM1Ti-Ue1f
402
403
  arpakitlib/ar_settings_util.py,sha256=Y5wi_cmsjDjfJpM0VJHjbo0NoVPKfypKaD1USowwDtQ,1327
403
404
  arpakitlib/ar_sleep_util.py,sha256=ggaj7ML6QK_ADsHMcyu6GUmUpQ_9B9n-SKYH17h-9lM,1045
404
405
  arpakitlib/ar_sqladmin_util.py,sha256=Jd33EX_fAso4bvrcDhNEYOUlUHkudbadNGOJDg7rqhQ,301
405
- arpakitlib/ar_sqlalchemy_util.py,sha256=J8AAPT0BgrrRYYkJJn_ezEla6YyF3OL34Q-DNpadziU,11285
406
+ arpakitlib/ar_sqlalchemy_util.py,sha256=w_tGPTWIMVjHkTEYo9tVe1sfK_4vvfd3zGATLiYC2J4,11775
406
407
  arpakitlib/ar_ssh_runner_util.py,sha256=yvAwza480MkHKvLkDEsR7JNh2bYNs6P9rCVo4NA85NE,6865
407
408
  arpakitlib/ar_str_util.py,sha256=2lGpnXDf2h1cBZpVf5i1tX_HCv5iBd6IGnrCw4QWWlY,4350
408
409
  arpakitlib/ar_type_util.py,sha256=Cs_tef-Fc5xeyAF54KgISCsP11NHyzIsglm4S3Xx7iM,4049
409
410
  arpakitlib/ar_yookassa_api_client_util.py,sha256=VozuZeCJjmLd1zj2BdC9WfiAQ3XYOrIMsdpNK-AUlm0,5347
410
- arpakitlib-1.8.156.dist-info/LICENSE,sha256=GPEDQMam2r7FSTYqM1mm7aKnxLaWcBotH7UvQtea-ec,11355
411
- arpakitlib-1.8.156.dist-info/METADATA,sha256=w7-HsemfipWlnRI-TBdYbQY3yqWzPO7mHUBd3iuBZYI,3706
412
- arpakitlib-1.8.156.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
413
- arpakitlib-1.8.156.dist-info/entry_points.txt,sha256=36xqR3PJFT2kuwjkM_EqoIy0qFUDPKSm_mJaI7emewE,87
414
- arpakitlib-1.8.156.dist-info/RECORD,,
411
+ arpakitlib-1.8.158.dist-info/LICENSE,sha256=GPEDQMam2r7FSTYqM1mm7aKnxLaWcBotH7UvQtea-ec,11355
412
+ arpakitlib-1.8.158.dist-info/METADATA,sha256=Owzc2sjXbylqDiJbrOZHuUsxlWpq7wHOwWNIu_YCvWE,3706
413
+ arpakitlib-1.8.158.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
414
+ arpakitlib-1.8.158.dist-info/entry_points.txt,sha256=36xqR3PJFT2kuwjkM_EqoIy0qFUDPKSm_mJaI7emewE,87
415
+ arpakitlib-1.8.158.dist-info/RECORD,,