holado 0.4.1__py3-none-any.whl → 0.5.2__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 holado might be problematic. Click here for more details.

Files changed (93) hide show
  1. holado/common/context/session_context.py +6 -0
  2. holado/common/handlers/object.py +6 -0
  3. {holado-0.4.1.dist-info → holado-0.5.2.dist-info}/METADATA +2 -1
  4. {holado-0.4.1.dist-info → holado-0.5.2.dist-info}/RECORD +93 -43
  5. holado_ais/ais/ais_messages.py +44 -15
  6. holado_ais/ais/patch_pyais.py +47 -176
  7. holado_ais/tests/behave/steps/ais/ais_messages_steps.py +2 -6
  8. holado_core/common/resource/persisted_data_manager.py +4 -5
  9. holado_core/common/resource/resource_manager.py +4 -18
  10. holado_core/common/tools/converters/converter.py +14 -3
  11. holado_core/common/tools/tools.py +9 -2
  12. holado_core/tools/abstracts/blocking_command_service.py +9 -1
  13. holado_data/data/generator/generator_manager.py +1 -13
  14. holado_db/tools/db/clients/base/db_audit.py +94 -0
  15. holado_db/tools/db/clients/base/db_client.py +145 -59
  16. holado_db/tools/db/clients/postgresql/postgresql_audit.py +75 -0
  17. holado_db/tools/db/clients/postgresql/postgresql_client.py +4 -0
  18. holado_db/tools/db/clients/sqlite/sqlite_audit.py +70 -0
  19. holado_db/tools/db/clients/sqlite/sqlite_client.py +4 -0
  20. holado_django/__init__.py +31 -0
  21. holado_django/server/HOWTO.txt +27 -0
  22. holado_django/server/django_projects/rest_api/db.sqlite3 +0 -0
  23. holado_django/server/django_projects/rest_api/manage.py +22 -0
  24. holado_django/server/django_projects/rest_api/rest_api/__init__.py +0 -0
  25. holado_django/server/django_projects/rest_api/rest_api/application/__init__.py +0 -0
  26. holado_django/server/django_projects/rest_api/rest_api/application/admin.py +3 -0
  27. holado_django/server/django_projects/rest_api/rest_api/application/apps.py +9 -0
  28. holado_django/server/django_projects/rest_api/rest_api/application/migrations/__init__.py +0 -0
  29. holado_django/server/django_projects/rest_api/rest_api/application/models.py +3 -0
  30. holado_django/server/django_projects/rest_api/rest_api/application/tests.py +3 -0
  31. holado_django/server/django_projects/rest_api/rest_api/application/views.py +6 -0
  32. holado_django/server/django_projects/rest_api/rest_api/asgi.py +16 -0
  33. holado_django/server/django_projects/rest_api/rest_api/settings.py +130 -0
  34. holado_django/server/django_projects/rest_api/rest_api/urls.py +35 -0
  35. holado_django/server/django_projects/rest_api/rest_api/wsgi.py +16 -0
  36. holado_django/server/django_server.py +110 -0
  37. holado_django/server/grpc_django_server.py +57 -0
  38. holado_django/server/patch_djangogrpcframework.py +46 -0
  39. holado_django/tests/behave/steps/__init__.py +16 -0
  40. holado_django/tests/behave/steps/django_server_steps.py +83 -0
  41. holado_grpc/tests/behave/steps/private/api/grpc_steps.py +9 -9
  42. holado_logging/common/logging/holado_logger.py +1 -6
  43. holado_multitask/multitasking/multitask_manager.py +36 -10
  44. holado_multitask/multithreading/thread.py +13 -7
  45. holado_multitask/multithreading/timer.py +3 -0
  46. holado_python/common/tools/datetime.py +31 -13
  47. holado_python/standard_library/ssl/resources/certificates/tcpbin.crt +16 -16
  48. holado_python/standard_library/ssl/resources/certificates/tcpbin.key +26 -26
  49. holado_rabbitmq/tools/rabbitmq/rabbitmq_blocking_client.py +24 -2
  50. holado_rabbitmq/tools/rabbitmq/rabbitmq_client.py +2 -2
  51. holado_report/report/builders/failure_report_builder.py +129 -0
  52. holado_report/report/report_manager.py +4 -0
  53. holado_rest/api/rest/TODO.txt +1 -1
  54. holado_rest/api/rest/rest_client.py +19 -7
  55. holado_rest/tests/behave/steps/api/rest_client_steps.py +1 -2
  56. holado_rest/tests/behave/steps/private/api/rest_steps.py +11 -13
  57. holado_system/system/command/command.py +31 -9
  58. holado_system/system/global_system.py +3 -3
  59. test_holado/environment.py +6 -4
  60. test_holado/features/NonReg/api/REST.feature +7 -2
  61. test_holado/features/NonReg/api/gRPC.feature +0 -6
  62. test_holado/features/NonReg/holado_ais/message_types/type-10.feature +38 -0
  63. test_holado/features/NonReg/holado_ais/message_types/type-12.feature +37 -0
  64. test_holado/features/NonReg/holado_ais/message_types/type-14.feature +36 -0
  65. test_holado/features/NonReg/holado_ais/message_types/type-15.feature +36 -0
  66. test_holado/features/NonReg/holado_ais/message_types/type-16.feature +38 -0
  67. test_holado/features/NonReg/holado_ais/message_types/type-17.feature +46 -0
  68. test_holado/features/NonReg/holado_ais/message_types/type-18.feature +37 -0
  69. test_holado/features/NonReg/holado_ais/message_types/type-19.feature +38 -0
  70. test_holado/features/NonReg/holado_ais/message_types/type-1_2_3.feature +42 -0
  71. test_holado/features/NonReg/holado_ais/message_types/type-20.feature +38 -0
  72. test_holado/features/NonReg/holado_ais/message_types/type-21.feature +37 -0
  73. test_holado/features/NonReg/holado_ais/message_types/type-22.feature +84 -0
  74. test_holado/features/NonReg/holado_ais/message_types/type-23.feature +49 -0
  75. test_holado/features/NonReg/holado_ais/message_types/type-24.feature +72 -0
  76. test_holado/features/NonReg/holado_ais/message_types/type-25.feature +143 -0
  77. test_holado/features/NonReg/holado_ais/message_types/type-26.feature +144 -0
  78. test_holado/features/NonReg/holado_ais/message_types/type-27.feature +36 -0
  79. test_holado/features/NonReg/holado_ais/message_types/type-4_11.feature +39 -0
  80. test_holado/features/NonReg/holado_ais/message_types/type-5.feature +33 -0
  81. test_holado/features/NonReg/holado_ais/message_types/type-6.feature +37 -0
  82. test_holado/features/NonReg/holado_ais/message_types/type-7_13.feature +43 -0
  83. test_holado/features/NonReg/holado_ais/message_types/type-8.feature +37 -0
  84. test_holado/features/NonReg/holado_ais/message_types/type-9.feature +37 -0
  85. test_holado/initialize_holado.py +62 -0
  86. test_holado/logging.conf +3 -0
  87. test_holado/tools/django/api_grpc/manage.py +2 -0
  88. test_holado/tools/django/api_grpc/patch_djangogrpcframework.py +42 -0
  89. test_holado/tools/django/api_rest/db.sqlite3 +0 -0
  90. {holado-0.4.1.dist-info → holado-0.5.2.dist-info}/WHEEL +0 -0
  91. {holado-0.4.1.dist-info → holado-0.5.2.dist-info}/licenses/LICENSE +0 -0
  92. /holado_helper/holado_module_template/{test → tests}/behave/steps/__init__.py +0 -0
  93. /holado_helper/holado_module_template/{test → tests}/behave/steps/private/__init__.py +0 -0
@@ -41,6 +41,8 @@ class DBClient(object):
41
41
 
42
42
  self.__connection = None
43
43
  self.__query_builder = None
44
+ self.__audit_manager = None
45
+ self.__column_names_by_table = {}
44
46
 
45
47
  @property
46
48
  def name(self):
@@ -68,6 +70,19 @@ class DBClient(object):
68
70
  raise TechnicalException(f"DB client '{self.__name}' doesn't manage cursor")
69
71
  return self.__cursor
70
72
 
73
+ @property
74
+ def audit_manager(self):
75
+ if self.__audit_manager is None:
76
+ self.__audit_manager = self._new_audit_manager()
77
+ return self.__audit_manager
78
+
79
+ def _new_audit_manager(self):
80
+ """Create a new audit manager.
81
+ By default, use a audit manager with a single table of audit for whole DB and with triggers on audited tables.
82
+ Override this method in order to define which audit manager to use.
83
+ """
84
+ raise NotImplementedError()
85
+
71
86
  def connect(self):
72
87
  try:
73
88
  self.__connection = self._connect(**self.__connect_kwargs)
@@ -83,22 +98,45 @@ class DBClient(object):
83
98
  if not self.is_connected:
84
99
  raise FunctionalException(f"DB Client '{self.name}' is not connected")
85
100
 
86
- def is_query_type(self, sql_or_query, query_type):
87
- if isinstance(sql_or_query, pypika.queries.QueryBuilder):
88
- sql = self.query_builder.to_sql(sql_or_query)
89
- else:
90
- sql = sql_or_query
91
- if not isinstance(sql, str):
92
- raise TechnicalException(f"Unexpected type '{Typing.get_object_class_fullname(sql)}' for parameter sql_or_query")
93
-
94
- if isinstance(query_type, str):
95
- query_type = QueryType[query_type.upper()]
96
- if not isinstance(query_type, QueryType):
97
- raise TechnicalException(f"Unmanage query_type of type '{Typing.get_object_class_fullname(query_type)}'")
98
-
99
- p = sql_metadata.Parser(sql)
100
- return p.query_type == query_type
101
+ # Manage queries
102
+
103
+ def insert(self, table_name, data: dict, do_commit=None):
104
+ """
105
+ Insert given data.
106
+ Parameter 'data' has to be a dictionary with keys equal to table column names.
107
+ """
108
+ query, values = self.query_builder.insert(table_name, data)
109
+ return self.execute_query(query, *values, do_commit=do_commit)
110
+
111
+ def update(self, table_name, data: dict, where_data: dict=None, where_compare_data: list=None, do_commit=None):
112
+ """
113
+ Update given data.
114
+ Parameters 'data' and 'where_data' have to be dictionaries with keys equal to table column names.
115
+ """
116
+ query, values = self.query_builder.update(table_name, data, where_data=where_data, where_compare_data=where_compare_data)
117
+ return self.execute_query(query, *values, do_commit=do_commit)
118
+
119
+ def select(self, table_name, where_data: dict=None, where_compare_data: list=None, sql_return="*", **kwargs):
120
+ """
121
+ Select by filtering on given where data.
122
+ @param where_data: dictionary of (field_name, value) for simple where clauses.
123
+ @param where_compare_data: list of tuples (field_name, operator, value) for where clauses comparing fields with values.
124
+ """
125
+ query, values = self.query_builder.select(table_name, where_data=where_data, where_compare_data=where_compare_data, sql_return=sql_return)
126
+ return self.execute_query(query, *values, do_commit=False, **kwargs)
127
+
128
+ def delete(self, table_name, where_data: dict=None, where_compare_data: list=None, do_commit=None):
129
+ """
130
+ Delete by filtering on given where data.
131
+ Parameter 'where_data' has to be a dictionary with keys equal to table column names.
132
+ """
133
+ query, values = self.query_builder.delete(table_name, where_data=where_data, where_compare_data=where_compare_data)
134
+ return self.execute_query(query, *values, do_commit=do_commit)
101
135
 
136
+ def count(self, table_name, where_data: dict=None, where_compare_data: list=None):
137
+ result = self.select(table_name, where_data=where_data, where_compare_data=where_compare_data, sql_return="count(*)")
138
+ return result[0][0].content
139
+
102
140
  def execute_query(self, query, *args, **kwargs):
103
141
  sql = self.query_builder.to_sql(query)
104
142
 
@@ -126,7 +164,7 @@ class DBClient(object):
126
164
 
127
165
  # Execute
128
166
  if Tools.do_log(logger, logging.DEBUG):
129
- logger.debug(f"[{self.name}] Executing SQL [{sql}] with parameters [{args if args else ''}{kwargs if kwargs else ''}]...")
167
+ logger.debug(f"[{self.name}] Executing SQL [{Tools.truncate_text(sql, 1000)}] (args: {Tools.truncate_text(args, 1000)} ; kwargs: {Tools.truncate_text(kwargs, 1000)})...")
130
168
  try:
131
169
  if args:
132
170
  self.cursor.execute(sql, args)
@@ -172,7 +210,7 @@ class DBClient(object):
172
210
  else:
173
211
  res = None
174
212
 
175
- self.__log_sql_result(res, message=f"Executed SQL [{sql}] with parameters [{args if args else ''}{kwargs if kwargs else ''}]")
213
+ self.__log_sql_result(res, message=f"Executed SQL [{Tools.truncate_text(sql, 1000)}] (args: {Tools.truncate_text(args, 1000)} ; kwargs: {Tools.truncate_text(kwargs, 1000)})")
176
214
 
177
215
  # Manage commit
178
216
  if do_commit:
@@ -184,6 +222,52 @@ class DBClient(object):
184
222
 
185
223
  return res
186
224
 
225
+ def is_query_type(self, sql_or_query, query_type):
226
+ if isinstance(sql_or_query, pypika.queries.QueryBuilder):
227
+ sql = self.query_builder.to_sql(sql_or_query)
228
+ else:
229
+ sql = sql_or_query
230
+ if not isinstance(sql, str):
231
+ raise TechnicalException(f"Unexpected type '{Typing.get_object_class_fullname(sql)}' for parameter sql_or_query")
232
+ sql = sql.lower()
233
+
234
+ # Parsing query with sql_metadata can be costly, thus begin by fast and simple methods
235
+ query_type_str = query_type.lower() if isinstance(query_type, str) else query_type.name.lower()
236
+ for qt in ['select', 'insert', 'update', 'delete']:
237
+ if sql.startswith(qt):
238
+ return qt == query_type_str
239
+
240
+ # Parse more complex queries
241
+ if isinstance(query_type, str):
242
+ query_type = QueryType[query_type.upper()]
243
+ if not isinstance(query_type, QueryType):
244
+ raise TechnicalException(f"Unmanage query_type of type '{Typing.get_object_class_fullname(query_type)}'")
245
+
246
+ try:
247
+ p = sql_metadata.Parser(sql)
248
+ res = p.query_type == query_type
249
+ except ValueError as exc:
250
+ if "Not supported query type" in str(exc):
251
+ res = False
252
+ else:
253
+ raise TechnicalException(f"Failed to define query type") from exc
254
+ except Exception as exc:
255
+ raise TechnicalException(f"Failed to define query type") from exc
256
+
257
+ return res
258
+
259
+ def set_or_update_json_key_value(self, table_name, field_name, json_key, json_value, where_data: dict=None, where_compare_data: list=None):
260
+ """
261
+ Set or update a json field with key=value.
262
+ """
263
+ raise NotImplementedError()
264
+
265
+ def _get_sql_placeholder(self):
266
+ """
267
+ Return the character/string to use as placeholder in SQL requests.
268
+ """
269
+ raise NotImplementedError()
270
+
187
271
  def _get_base_exception_type(self):
188
272
  raise NotImplementedError()
189
273
 
@@ -203,57 +287,59 @@ class DBClient(object):
203
287
  else:
204
288
  return str(sql_result)
205
289
 
290
+
291
+ # Manage transactions
292
+
206
293
  def commit(self):
207
294
  self.connection.commit()
208
295
 
209
296
  def rollback(self):
210
297
  self.connection.rollback()
211
-
298
+
299
+
300
+ # Manage tables
301
+
212
302
  def exist_table(self, table_name):
213
303
  raise NotImplementedError()
214
304
 
215
- def insert(self, table_name, data: dict, do_commit=None):
216
- """
217
- Insert given data.
218
- Parameter 'data' has to be a dictionary with keys equal to table column names.
219
- """
220
- query, values = self.query_builder.insert(table_name, data)
221
- return self.execute_query(query, *values, do_commit=do_commit)
222
-
223
- def update(self, table_name, data: dict, where_data: dict=None, where_compare_data: list=None, do_commit=None):
224
- """
225
- Update given data.
226
- Parameters 'data' and 'where_data' have to be dictionaries with keys equal to table column names.
227
- """
228
- query, values = self.query_builder.update(table_name, data, where_data=where_data, where_compare_data=where_compare_data)
229
- return self.execute_query(query, *values, do_commit=do_commit)
305
+ def create_table(self, table_name, create_sql, raise_if_exist=False, do_commit=True, do_audit=False):
306
+ if not self.exist_table(table_name):
307
+ self.execute(create_sql, do_commit=do_commit)
308
+ if not self.exist_table(table_name):
309
+ raise TechnicalException(f"Failed to create table '{table_name}' with SQL request [{create_sql}]")
310
+
311
+ # Audit table
312
+ if do_audit:
313
+ self.audit_manager.audit_table(table_name)
314
+ elif raise_if_exist:
315
+ raise FunctionalException(f"Table '{table_name}' already exists")
230
316
 
231
- def select(self, table_name, where_data: dict=None, where_compare_data: list=None, sql_return="*", **kwargs):
232
- """
233
- Select by filtering on given where data.
234
- @param where_data: dictionary of (field_name, value) for simple where clauses.
235
- @param where_compare_data: list of tuples (field_name, operator, value) for where clauses comparing fields with values.
236
- """
237
- query, values = self.query_builder.select(table_name, where_data=where_data, where_compare_data=where_compare_data, sql_return=sql_return)
238
- return self.execute_query(query, *values, do_commit=False, **kwargs)
317
+ def drop_table(self, table_name, raise_if_not_exist=False, do_commit=True):
318
+ if self.exist_table(table_name):
319
+ sql = f"drop table {table_name};"
320
+ self.execute(sql, do_commit=do_commit)
321
+ if self.exist_table(table_name):
322
+ raise TechnicalException(f"Failed to drop table '{table_name}' with SQL request [{sql}]")
323
+ elif raise_if_not_exist:
324
+ raise FunctionalException(f"Table '{table_name}' doesn't exist")
239
325
 
240
- def delete(self, table_name, where_data: dict=None, where_compare_data: list=None, do_commit=None):
241
- """
242
- Delete by filtering on given where data.
243
- Parameter 'where_data' has to be a dictionary with keys equal to table column names.
244
- """
245
- query, values = self.query_builder.delete(table_name, where_data=where_data, where_compare_data=where_compare_data)
246
- return self.execute_query(query, *values, do_commit=do_commit)
326
+ def get_table_column_names(self, table_name):
327
+ if table_name not in self.__column_names_by_table:
328
+ self.__column_names_by_table[table_name] = self._get_table_column_names(table_name)
329
+ return self.__column_names_by_table[table_name]
247
330
 
248
- def set_or_update_json_key_value(self, table_name, field_name, json_key, json_value, where_data: dict=None, where_compare_data: list=None):
249
- """
250
- Set or update a json field with key=value.
251
- """
252
- raise NotImplementedError()
331
+ def _get_table_column_names(self, table_name):
332
+ sql = f"select * from {table_name} limit 1"
333
+ try:
334
+ self.cursor.execute(sql)
335
+ except self._get_base_exception_type() as exc:
336
+ self.rollback()
337
+ raise TechnicalException(f"[{self.name}] Error while executing SQL [{sql}]") from exc
338
+
339
+ if self.cursor.description:
340
+ res = [field[0] for field in self.cursor.description]
341
+ else:
342
+ raise TechnicalException(f"Failed to get column names of table {table_name}")
343
+
344
+ return res
253
345
 
254
- def _get_sql_placeholder(self):
255
- """
256
- Return the character/string to use as placeholder in SQL requests.
257
- """
258
- raise NotImplementedError()
259
-
@@ -0,0 +1,75 @@
1
+
2
+ #################################################
3
+ # HolAdo (Holistic Automation do)
4
+ #
5
+ # (C) Copyright 2023 by Eric Klumpp
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
+
11
+ # The Software is provided “as is”, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the Software.
12
+ #################################################
13
+
14
+ import logging
15
+ from holado.common.handlers.undefined import default_value
16
+ from holado_db.tools.db.clients.base.db_audit import TriggerAuditManager
17
+
18
+ logger = logging.getLogger(__name__)
19
+
20
+
21
+ class PostgreSQLTriggerAuditManager(TriggerAuditManager):
22
+ """Manage audit of tables by triggers.
23
+ WARNING: This case is not tested yet
24
+ """
25
+ def __init__(self, name, db_client):
26
+ super().__init__(name, db_client)
27
+
28
+ def _get_audit_table_sql_create(self, audit_table_name=default_value):
29
+ audit_table_name = self._get_audit_table_name(audit_table_name)
30
+ return f"""CREATE TABLE {audit_table_name} (
31
+ id SERIAL PRIMARY KEY,
32
+ table_name TEXT,
33
+ record_id INTEGER,
34
+ operation_type TEXT,
35
+ changed_at TIMESTAMP DEFAULT now(),
36
+ changed_by TEXT,
37
+ previous_values JSONB,
38
+ new_values JSONB
39
+ )"""
40
+
41
+ def _get_drop_trigger_sql_of_audit_table_operation(self, table_name, operation_type, audit_table_name):
42
+ audit_table_name = self._get_audit_table_name(audit_table_name)
43
+ trigger_name = self.get_trigger_name(table_name, operation_type)
44
+ return f"DROP TRIGGER IF EXISTS {trigger_name} on {table_name}"
45
+
46
+ def _get_create_trigger_sql_to_audit_table_operation(self, table_name, operation_type, audit_table_name):
47
+ audit_table_name = self._get_audit_table_name(audit_table_name)
48
+ trigger_name = self.get_trigger_name(table_name, operation_type)
49
+ trigger_insert_sql = self.__get_trigger_insert_sql(table_name, operation_type, audit_table_name)
50
+ return f"""CREATE TRIGGER {trigger_name} AFTER {operation_type.upper()} ON {table_name}
51
+ BEGIN
52
+ {trigger_insert_sql}
53
+ END;
54
+ """
55
+
56
+ def __get_trigger_insert_sql(self, table_name, operation_type, audit_table_name):
57
+ rowid_txt = "NEW.id" if operation_type in ['insert', 'update'] else "OLD.id"
58
+ op_type = operation_type[0]
59
+ prev_vals_txt = "to_jsonb(OLD)" if operation_type in ['update', 'delete'] else "NULL"
60
+ new_vals_txt = "to_jsonb(NEW)" if operation_type in ['insert', 'update'] else "NULL"
61
+
62
+ audit_info = [f"'{table_name}'",
63
+ rowid_txt,
64
+ f"'{op_type}'",
65
+ "current_user",
66
+ prev_vals_txt,
67
+ new_vals_txt,
68
+ ]
69
+ audit_info = ','.join(audit_info)
70
+
71
+ return f"""INSERT INTO {audit_table_name} (table_name, record_id, operation_type, changed_by, previous_values, new_values)
72
+ VALUES ({audit_info});"""
73
+
74
+
75
+
@@ -17,6 +17,7 @@ from holado_core.common.tables.table_with_header import TableWithHeader
17
17
  from holado_core.common.tools.tools import Tools
18
18
  import json
19
19
  from pypika.terms import Function, LiteralValue
20
+ from holado_db.tools.db.clients.postgresql.postgresql_audit import PostgreSQLTriggerAuditManager
20
21
 
21
22
  logger = logging.getLogger(__name__)
22
23
 
@@ -43,6 +44,9 @@ class PostgreSQLClient(DBClient):
43
44
  def __init__(self, name, connect_kwargs):
44
45
  super().__init__(name if name else 'PostgreSQL', connect_kwargs)
45
46
 
47
+ def _new_audit_manager(self):
48
+ return PostgreSQLTriggerAuditManager("Audit with triggers", self)
49
+
46
50
  def _get_base_exception_type(self):
47
51
  return psycopg.Error
48
52
 
@@ -0,0 +1,70 @@
1
+
2
+ #################################################
3
+ # HolAdo (Holistic Automation do)
4
+ #
5
+ # (C) Copyright 2023 by Eric Klumpp
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
+
11
+ # The Software is provided “as is”, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the Software.
12
+ #################################################
13
+
14
+ import logging
15
+ from holado.common.handlers.undefined import default_value
16
+ from holado_db.tools.db.clients.base.db_audit import TriggerAuditManager
17
+
18
+ logger = logging.getLogger(__name__)
19
+
20
+
21
+ class SQLite3TriggerAuditManager(TriggerAuditManager):
22
+ def __init__(self, name, db_client):
23
+ super().__init__(name, db_client)
24
+
25
+ def _get_audit_table_sql_create(self, audit_table_name=default_value):
26
+ audit_table_name = self._get_audit_table_name(audit_table_name)
27
+ return f"""CREATE TABLE {audit_table_name} (
28
+ id INTEGER PRIMARY KEY,
29
+ table_name TEXT,
30
+ record_id INTEGER,
31
+ operation_type TEXT,
32
+ changed_at TEXT,
33
+ previous_values TEXT,
34
+ new_values TEXT
35
+ )"""
36
+
37
+ def _get_drop_trigger_sql_of_audit_table_operation(self, table_name, operation_type, audit_table_name):
38
+ trigger_name = self.get_trigger_name(table_name, operation_type)
39
+ return f"DROP TRIGGER IF EXISTS {trigger_name}"
40
+
41
+ def _get_create_trigger_sql_to_audit_table_operation(self, table_name, operation_type, audit_table_name):
42
+ audit_table_name = self._get_audit_table_name(audit_table_name)
43
+ trigger_name = self.get_trigger_name(table_name, operation_type)
44
+ trigger_insert_sql = self.__get_trigger_insert_sql(table_name, operation_type, audit_table_name)
45
+ return f"""CREATE TRIGGER {trigger_name} AFTER {operation_type.upper()} ON {table_name}
46
+ BEGIN
47
+ {trigger_insert_sql}
48
+ END;
49
+ """
50
+
51
+ def __get_trigger_insert_sql(self, table_name, operation_type, audit_table_name):
52
+ values = [f"'{table_name}'",
53
+ "NEW.rowid" if operation_type in ['insert', 'update'] else "OLD.rowid",
54
+ f"'{operation_type[0]}'",
55
+ "DATETIME('now')",
56
+ self.__get_row_version_json_object_sql(table_name, 'OLD') if operation_type in ['update', 'delete'] else "NULL",
57
+ self.__get_row_version_json_object_sql(table_name, 'NEW') if operation_type in ['insert', 'update'] else "NULL",
58
+ ]
59
+
60
+ return f"""INSERT INTO {audit_table_name} (table_name, record_id, operation_type, changed_at, previous_values, new_values)
61
+ VALUES ({','.join(values)});"""
62
+
63
+ def __get_row_version_json_object_sql(self, table_name, row_version):
64
+ col_names = self._db_client.get_table_column_names(table_name)
65
+ json_object_args = []
66
+ for cn in col_names:
67
+ json_object_args.append(f"'{cn}'")
68
+ json_object_args.append(f"{row_version}.{cn}")
69
+ return f"json_object({','.join(json_object_args)})"
70
+
@@ -15,6 +15,7 @@ import logging
15
15
  from holado_db.tools.db.clients.base.db_client import DBClient
16
16
  from holado_core.common.exceptions.functional_exception import FunctionalException
17
17
  from holado_core.common.tools.tools import Tools
18
+ from holado_db.tools.db.clients.sqlite.sqlite_audit import SQLite3TriggerAuditManager
18
19
 
19
20
  logger = logging.getLogger(__name__)
20
21
 
@@ -35,6 +36,9 @@ class SQLite3Client(DBClient):
35
36
  def __init__(self, name, connect_kwargs):
36
37
  super().__init__(name if name else 'SQLite3', connect_kwargs)
37
38
 
39
+ def _new_audit_manager(self):
40
+ return SQLite3TriggerAuditManager("Audit with triggers", self)
41
+
38
42
  def _get_base_exception_type(self):
39
43
  return sqlite3.Error
40
44
 
@@ -0,0 +1,31 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ #################################################
4
+ # HolAdo (Holistic Automation do)
5
+ #
6
+ # (C) Copyright 2021-2025 by Eric Klumpp
7
+ #
8
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
11
+
12
+ # The Software is provided “as is”, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the Software.
13
+ #################################################
14
+
15
+
16
+ def dependencies():
17
+ """
18
+ Return None or the list of names of modules it depend on
19
+ """
20
+ return None
21
+
22
+ def register():
23
+ """
24
+ Register the services of this module
25
+ """
26
+ # from holado.common.context.session_context import SessionContext
27
+
28
+ # from holado_xxx.xxx import XXXManager
29
+ # SessionContext.instance().services.register_service_type("xxx_manager", XXXManager)
30
+ pass
31
+
@@ -0,0 +1,27 @@
1
+
2
+ #################################################
3
+ # HolAdo (Holistic Automation do)
4
+ #
5
+ # (C) Copyright 2021-2025 by Eric Klumpp
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8
+ #
9
+ # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
10
+
11
+ # The Software is provided “as is”, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the Software.
12
+ #################################################
13
+
14
+
15
+ HOWTO create a REST server easier with HolAdo:
16
+
17
+ 1. Create a working directory (WORK_DIR) where all server files will be stored
18
+ 2. Copy in WORK_DIR the appropriate django project. For this case, it is holado_django/server/django_projects/rest_api
19
+ 3. Update application with wanted REST APIs. For this case, update/add files in rest_api/rest_api/application: views.py, models.py,...
20
+ 4. Make migrations if needed (cf django documentation)
21
+ 5. Run server manually with a command like "python manage.py runserver", or use module holado_django/server/django_server.py
22
+
23
+ Example: A simple example is in holado_examples/projects/server_rest/server_rest_example
24
+
25
+ Note: These steps are simple and doesn't give help to developpers knowing Django and Django REST framework.
26
+ They should be a first step towards future more powerful tools.
27
+
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env python
2
+ """Django's command-line utility for administrative tasks."""
3
+ import os
4
+ import sys
5
+
6
+
7
+ def main():
8
+ """Run administrative tasks."""
9
+ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rest_api.settings')
10
+ try:
11
+ from django.core.management import execute_from_command_line
12
+ except ImportError as exc:
13
+ raise ImportError(
14
+ "Couldn't import Django. Are you sure it's installed and "
15
+ "available on your PYTHONPATH environment variable? Did you "
16
+ "forget to activate a virtual environment?"
17
+ ) from exc
18
+ execute_from_command_line(sys.argv)
19
+
20
+
21
+ if __name__ == '__main__':
22
+ main()
@@ -0,0 +1,3 @@
1
+ # from django.contrib import admin
2
+
3
+ # Register your models here.
@@ -0,0 +1,9 @@
1
+ from django.apps import AppConfig
2
+
3
+
4
+ django_application_inst = None
5
+
6
+
7
+ class ApplicationConfig(AppConfig):
8
+ default_auto_field = 'django.db.models.BigAutoField'
9
+ name = 'application'
@@ -0,0 +1,3 @@
1
+ # from django.db import models
2
+
3
+ # Create your models here.
@@ -0,0 +1,3 @@
1
+ # from django.test import TestCase
2
+
3
+ # Create your tests here.
@@ -0,0 +1,6 @@
1
+ # from django.shortcuts import render
2
+ # from rest_framework import viewsets
3
+ # from rest_framework import permissions
4
+
5
+
6
+
@@ -0,0 +1,16 @@
1
+ """
2
+ ASGI config for server_rest_example project.
3
+
4
+ It exposes the ASGI callable as a module-level variable named ``application``.
5
+
6
+ For more information on this file, see
7
+ https://docs.djangoproject.com/en/5.2/howto/deployment/asgi/
8
+ """
9
+
10
+ import os
11
+
12
+ from django.core.asgi import get_asgi_application
13
+
14
+ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'rest_api.settings')
15
+
16
+ application = get_asgi_application()