aws-python-helper 1.5.0__tar.gz → 1.6.0__tar.gz

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.
Files changed (59) hide show
  1. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/PKG-INFO +1 -1
  2. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/__init__.py +12 -1
  3. aws_python_helper-1.6.0/aws_python_helper/repository/__init__.py +19 -0
  4. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/repository/audit.py +77 -20
  5. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/repository/base.py +39 -3
  6. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper.egg-info/PKG-INFO +1 -1
  7. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/pyproject.toml +1 -1
  8. aws_python_helper-1.5.0/aws_python_helper/repository/__init__.py +0 -4
  9. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/README.md +0 -0
  10. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/api/__init__.py +0 -0
  11. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/api/auth_middleware.py +0 -0
  12. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/api/auth_validators.py +0 -0
  13. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/api/base.py +0 -0
  14. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/api/dispatcher.py +0 -0
  15. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/api/exceptions.py +0 -0
  16. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/api/fetcher.py +0 -0
  17. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/api/handler.py +0 -0
  18. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/context/__init__.py +0 -0
  19. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/context/session.py +0 -0
  20. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/context/state_validator.py +0 -0
  21. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/database/__init__.py +0 -0
  22. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/database/database_proxy.py +0 -0
  23. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/database/external_database_proxy.py +0 -0
  24. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/database/external_mongo_manager.py +0 -0
  25. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/database/mongo_manager.py +0 -0
  26. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/fargate/__init__.py +0 -0
  27. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/fargate/executor.py +0 -0
  28. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/fargate/fetcher.py +0 -0
  29. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/fargate/handler.py +0 -0
  30. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/fargate/task_base.py +0 -0
  31. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/invoker/__init__.py +0 -0
  32. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/invoker/api_client.py +0 -0
  33. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/invoker/exceptions.py +0 -0
  34. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/invoker/lambda_invoker.py +0 -0
  35. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/lambda_standalone/__init__.py +0 -0
  36. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/lambda_standalone/base.py +0 -0
  37. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/lambda_standalone/fetcher.py +0 -0
  38. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/lambda_standalone/handler.py +0 -0
  39. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/model_index_sync/__init__.py +0 -0
  40. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/model_index_sync/base.py +0 -0
  41. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/model_query/__init__.py +0 -0
  42. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/model_query/base.py +0 -0
  43. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/permissions/__init__.py +0 -0
  44. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/permissions/catalog.py +0 -0
  45. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/sns/__init__.py +0 -0
  46. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/sns/publisher.py +0 -0
  47. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/sqs/__init__.py +0 -0
  48. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/sqs/consumer_base.py +0 -0
  49. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/sqs/fetcher.py +0 -0
  50. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/sqs/handler.py +0 -0
  51. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/utils/__init__.py +0 -0
  52. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/utils/json_encoder.py +0 -0
  53. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/utils/response.py +0 -0
  54. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper/utils/serializer.py +0 -0
  55. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper.egg-info/SOURCES.txt +0 -0
  56. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper.egg-info/dependency_links.txt +0 -0
  57. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper.egg-info/requires.txt +0 -0
  58. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/aws_python_helper.egg-info/top_level.txt +0 -0
  59. {aws_python_helper-1.5.0 → aws_python_helper-1.6.0}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aws-python-helper
3
- Version: 1.5.0
3
+ Version: 1.6.0
4
4
  Summary: AWS Python Helper Framework
5
5
  Author-email: Fabian Claros <neufabiae@gmail.com>
6
6
  License-Expression: MIT
@@ -26,7 +26,14 @@ from .utils.serializer import serialize_mongo_types
26
26
 
27
27
  # Repository
28
28
  from .repository.base import Repository
29
- from .repository.audit import current_actor, public_audit
29
+ from .repository.audit import (
30
+ ACTIVE,
31
+ DEFAULT_STATUSES,
32
+ INACTIVE,
33
+ InvalidStatusError,
34
+ current_actor,
35
+ public_audit,
36
+ )
30
37
 
31
38
  # Context
32
39
  from .context.session import Session, get_session, set_session
@@ -62,6 +69,10 @@ __all__ = [
62
69
  'Repository',
63
70
  'current_actor',
64
71
  'public_audit',
72
+ 'ACTIVE',
73
+ 'INACTIVE',
74
+ 'DEFAULT_STATUSES',
75
+ 'InvalidStatusError',
65
76
  'fargate_handler',
66
77
  'api_handler',
67
78
  'sqs_handler',
@@ -0,0 +1,19 @@
1
+ from .base import Repository
2
+ from .audit import (
3
+ ACTIVE,
4
+ DEFAULT_STATUSES,
5
+ INACTIVE,
6
+ InvalidStatusError,
7
+ current_actor,
8
+ public_audit,
9
+ )
10
+
11
+ __all__ = [
12
+ 'Repository',
13
+ 'ACTIVE',
14
+ 'INACTIVE',
15
+ 'DEFAULT_STATUSES',
16
+ 'InvalidStatusError',
17
+ 'current_actor',
18
+ 'public_audit',
19
+ ]
@@ -1,10 +1,17 @@
1
1
  """
2
- Automatic audit stamping for every write.
2
+ Framework-managed fields on every write: who, when, and what state.
3
3
 
4
4
  Records answer "who last touched this, and when" without any repository having to
5
- remember to fill it in. The four fields —created_at, created_by, updated_at,
6
- updated_by— are injected by wrapping the Motor collection, so call sites keep
7
- using `self.collection.insert_one(...)` exactly as before.
5
+ remember to fill it in. The four audit fields —created_at, created_by,
6
+ updated_at, updated_by— plus `status` are injected by wrapping the Motor
7
+ collection, so call sites keep using `self.collection.insert_one(...)` exactly as
8
+ before.
9
+
10
+ `status` is the lifecycle of a record, uniform across every entity so a listing
11
+ can show it without knowing what it is looking at. Two values by default, active
12
+ and inactive; a repository declaring `statuses` gets its own set. A value outside
13
+ that set is refused rather than stored: a status nobody expected reaches a panel
14
+ as an unknown label and a query as a silent mismatch.
8
15
 
9
16
  Not every write comes from a person. A scraper, a queue consumer or a scheduled
10
17
  task writes too, and the stamp says so: "nobody knows" and "the system did it"
@@ -28,6 +35,11 @@ CREATED_AT = 'created_at'
28
35
  CREATED_BY = 'created_by'
29
36
  UPDATED_AT = 'updated_at'
30
37
  UPDATED_BY = 'updated_by'
38
+ STATUS = 'status'
39
+
40
+ ACTIVE = 'active'
41
+ INACTIVE = 'inactive'
42
+ DEFAULT_STATUSES = [ACTIVE, INACTIVE]
31
43
 
32
44
 
33
45
  def _system_actor() -> Dict[str, str]:
@@ -66,11 +78,27 @@ def _now() -> datetime:
66
78
  return datetime.now(timezone.utc)
67
79
 
68
80
 
69
- def stamp_new(document: Dict[str, Any]) -> Dict[str, Any]:
70
- """The four fields on a document being created.
81
+ class InvalidStatusError(ValueError):
82
+ """A status the repository does not declare."""
83
+
84
+
85
+ def _check_status(value: Any, statuses: Optional[List[str]]) -> None:
86
+ if statuses and value is not None and value not in statuses:
87
+ raise InvalidStatusError(
88
+ f"'{value}' is not a valid status. Declared: {', '.join(statuses)}"
89
+ )
90
+
91
+
92
+ def stamp_new(
93
+ document: Dict[str, Any],
94
+ statuses: Optional[List[str]] = None,
95
+ default_status: Optional[str] = None,
96
+ ) -> Dict[str, Any]:
97
+ """The framework fields on a document being created.
71
98
 
72
99
  Whatever the caller set explicitly wins: this fills gaps, it does not
73
- overrule a repository that knows better.
100
+ overrule a repository that knows better. What it does not let pass is a
101
+ status outside the declared set.
74
102
  """
75
103
  if not isinstance(document, dict):
76
104
  return document
@@ -80,6 +108,10 @@ def stamp_new(document: Dict[str, Any]) -> Dict[str, Any]:
80
108
  document.setdefault(UPDATED_AT, now)
81
109
  document.setdefault(CREATED_BY, who)
82
110
  document.setdefault(UPDATED_BY, who)
111
+
112
+ if statuses:
113
+ document.setdefault(STATUS, default_status or statuses[0])
114
+ _check_status(document.get(STATUS), statuses)
83
115
  return document
84
116
 
85
117
 
@@ -95,6 +127,8 @@ def _merge_into_set(update: Dict[str, Any], values: Dict[str, Any], operator: st
95
127
  def stamp_update(
96
128
  update: Union[Dict[str, Any], List[Dict[str, Any]]],
97
129
  upsert: bool = False,
130
+ statuses: Optional[List[str]] = None,
131
+ default_status: Optional[str] = None,
98
132
  ) -> Union[Dict[str, Any], List[Dict[str, Any]]]:
99
133
  """The two 'updated' fields on a modification, and the 'created' ones on an upsert.
100
134
 
@@ -114,18 +148,28 @@ def stamp_update(
114
148
 
115
149
  # Un reemplazo entero —sin operadores— es un documento, no una modificación.
116
150
  if update and not any(key.startswith('$') for key in update):
117
- return stamp_new(dict(update))
151
+ return stamp_new(dict(update), statuses, default_status)
118
152
 
119
153
  update = dict(update)
154
+ if statuses:
155
+ for operator in ('$set', '$setOnInsert'):
156
+ section = update.get(operator)
157
+ if isinstance(section, dict) and STATUS in section:
158
+ _check_status(section[STATUS], statuses)
159
+
120
160
  _merge_into_set(update, touched, '$set')
121
161
  if upsert:
122
- # Si termina insertando, el registro nace ahora: created_* sólo en ese caso.
123
- _merge_into_set(update, {CREATED_AT: now, CREATED_BY: who}, '$setOnInsert')
162
+ # Si termina insertando, el registro nace ahora: created_* y el estado
163
+ # inicial sólo en ese caso.
164
+ born = {CREATED_AT: now, CREATED_BY: who}
165
+ if statuses:
166
+ born[STATUS] = default_status or statuses[0]
167
+ _merge_into_set(update, born, '$setOnInsert')
124
168
  return update
125
169
 
126
170
 
127
171
  class AuditedCollection:
128
- """A Motor collection that stamps who wrote and when.
172
+ """A Motor collection that fills in who wrote, when, and the record's status.
129
173
 
130
174
  Only the write methods are intercepted; everything else —find, aggregate,
131
175
  count_documents, create_index— passes straight through untouched.
@@ -135,10 +179,23 @@ class AuditedCollection:
135
179
  than asking for the stamp explicitly.
136
180
  """
137
181
 
138
- __slots__ = ('_collection',)
182
+ __slots__ = ('_collection', '_statuses', '_default_status')
139
183
 
140
- def __init__(self, collection: Any):
184
+ def __init__(
185
+ self,
186
+ collection: Any,
187
+ statuses: Optional[List[str]] = None,
188
+ default_status: Optional[str] = None,
189
+ ):
141
190
  object.__setattr__(self, '_collection', collection)
191
+ object.__setattr__(self, '_statuses', statuses)
192
+ object.__setattr__(self, '_default_status', default_status)
193
+
194
+ def _new(self, document: Dict[str, Any]) -> Dict[str, Any]:
195
+ return stamp_new(document, self._statuses, self._default_status)
196
+
197
+ def _update(self, update: Any, upsert: bool) -> Any:
198
+ return stamp_update(update, upsert, self._statuses, self._default_status)
142
199
 
143
200
  def __getattr__(self, name: str) -> Any:
144
201
  return getattr(self._collection, name)
@@ -147,36 +204,36 @@ class AuditedCollection:
147
204
  return f'AuditedCollection({self._collection!r})'
148
205
 
149
206
  async def insert_one(self, document: Dict[str, Any], *args: Any, **kwargs: Any):
150
- return await self._collection.insert_one(stamp_new(document), *args, **kwargs)
207
+ return await self._collection.insert_one(self._new(document), *args, **kwargs)
151
208
 
152
209
  async def insert_many(self, documents: Any, *args: Any, **kwargs: Any):
153
210
  return await self._collection.insert_many(
154
- [stamp_new(document) for document in documents], *args, **kwargs
211
+ [self._new(document) for document in documents], *args, **kwargs
155
212
  )
156
213
 
157
214
  async def update_one(self, filter: Any, update: Any, *args: Any, **kwargs: Any):
158
215
  return await self._collection.update_one(
159
- filter, stamp_update(update, kwargs.get('upsert', False)), *args, **kwargs
216
+ filter, self._update(update, kwargs.get('upsert', False)), *args, **kwargs
160
217
  )
161
218
 
162
219
  async def update_many(self, filter: Any, update: Any, *args: Any, **kwargs: Any):
163
220
  return await self._collection.update_many(
164
- filter, stamp_update(update, kwargs.get('upsert', False)), *args, **kwargs
221
+ filter, self._update(update, kwargs.get('upsert', False)), *args, **kwargs
165
222
  )
166
223
 
167
224
  async def find_one_and_update(self, filter: Any, update: Any, *args: Any, **kwargs: Any):
168
225
  return await self._collection.find_one_and_update(
169
- filter, stamp_update(update, kwargs.get('upsert', False)), *args, **kwargs
226
+ filter, self._update(update, kwargs.get('upsert', False)), *args, **kwargs
170
227
  )
171
228
 
172
229
  async def replace_one(self, filter: Any, replacement: Any, *args: Any, **kwargs: Any):
173
230
  return await self._collection.replace_one(
174
- filter, stamp_new(dict(replacement)), *args, **kwargs
231
+ filter, self._new(dict(replacement)), *args, **kwargs
175
232
  )
176
233
 
177
234
  async def find_one_and_replace(self, filter: Any, replacement: Any, *args: Any, **kwargs: Any):
178
235
  return await self._collection.find_one_and_replace(
179
- filter, stamp_new(dict(replacement)), *args, **kwargs
236
+ filter, self._new(dict(replacement)), *args, **kwargs
180
237
  )
181
238
 
182
239
 
@@ -13,7 +13,7 @@ from typing import Any, Dict, List, Optional, Tuple
13
13
  from ..database.mongo_manager import MongoManager
14
14
  from ..database.external_mongo_manager import ExternalMongoManager
15
15
  from ..context.session import get_session
16
- from .audit import AuditedCollection
16
+ from .audit import DEFAULT_STATUSES, AuditedCollection
17
17
 
18
18
 
19
19
  class Repository(ABC):
@@ -38,6 +38,11 @@ class Repository(ABC):
38
38
  indexes (list): List of index definitions to create automatically.
39
39
  audit (bool): Stamp created_at/created_by/updated_at/updated_by on every
40
40
  write. Default: True — no repository has to remember to do it.
41
+ statuses (list[str] | None): Allowed values of the record's `status`.
42
+ Default: ['active', 'inactive']. None means this collection has no
43
+ status at all.
44
+ default_status (str): What a new record gets. Default: the first of
45
+ `statuses`.
41
46
 
42
47
  Usage:
43
48
  # Core repository — always uses 'core' database
@@ -141,6 +146,35 @@ class Repository(ABC):
141
146
  """Whether this repository uses an external MongoDB cluster. Default: False."""
142
147
  return False
143
148
 
149
+ @property
150
+ def statuses(self) -> Optional[List[str]]:
151
+ """
152
+ The values this record's `status` can take.
153
+
154
+ Every entity carries one, and the same two by default, so a listing can
155
+ show the state of anything without knowing what it is looking at. A
156
+ repository with its own lifecycle declares it:
157
+
158
+ class UserRepository(Repository):
159
+ @property
160
+ def statuses(self):
161
+ return ["active", "pending", "inactive"]
162
+
163
+ A write with a value outside this set is refused, not stored: an
164
+ unexpected status reaches a panel as an unknown label and a query as a
165
+ silent mismatch.
166
+
167
+ None for collections where a lifecycle makes no sense — sessions, tokens,
168
+ counters.
169
+ """
170
+ return list(DEFAULT_STATUSES)
171
+
172
+ @property
173
+ def default_status(self) -> Optional[str]:
174
+ """What a record gets when nobody says otherwise. First of `statuses`."""
175
+ declared = self.statuses
176
+ return declared[0] if declared else None
177
+
144
178
  @property
145
179
  def audit(self) -> bool:
146
180
  """
@@ -220,9 +254,11 @@ class Repository(ABC):
220
254
  db = MongoManager.get_database(self.database_name)
221
255
 
222
256
  collection = db[self.collection_name]
223
- # La envoltura sella las escrituras; todo lo demás pasa de largo.
257
+ # La envoltura completa las escrituras; todo lo demás pasa de largo.
224
258
  self._collection_cache[key] = (
225
- AuditedCollection(collection) if self.audit else collection
259
+ AuditedCollection(collection, self.statuses, self.default_status)
260
+ if self.audit
261
+ else collection
226
262
  )
227
263
 
228
264
  return self._collection_cache[key]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: aws-python-helper
3
- Version: 1.5.0
3
+ Version: 1.6.0
4
4
  Summary: AWS Python Helper Framework
5
5
  Author-email: Fabian Claros <neufabiae@gmail.com>
6
6
  License-Expression: MIT
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "aws-python-helper"
7
- version = "1.5.0"
7
+ version = "1.6.0"
8
8
  description = "AWS Python Helper Framework"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -1,4 +0,0 @@
1
- from .base import Repository
2
- from .audit import current_actor, public_audit
3
-
4
- __all__ = ['Repository', 'current_actor', 'public_audit']