ic-python-db 0.7.1__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.
ic_python_db/hooks.py ADDED
@@ -0,0 +1,51 @@
1
+ """Simple hook system for entity modifications.
2
+
3
+ Entities can define an on_event static method to intercept and control changes:
4
+
5
+ from ic_python_db import Entity, String, ACTION_MODIFY
6
+
7
+ class User(Entity):
8
+ name = String()
9
+
10
+ @staticmethod
11
+ def on_event(entity, field_name, old_value, new_value, action):
12
+ # action: ACTION_CREATE, ACTION_MODIFY, or ACTION_DELETE
13
+ # Returns: (allow: bool, modified_value: any)
14
+
15
+ if action == ACTION_MODIFY and field_name == 'name':
16
+ return True, new_value.upper() # Auto-capitalize
17
+
18
+ return True, new_value
19
+ """
20
+
21
+ from typing import Any, Optional, Tuple
22
+
23
+ from .constants import ACTION_CREATE, ACTION_DELETE, ACTION_MODIFY
24
+
25
+
26
+ def call_entity_hook(
27
+ entity, field_name: Optional[str], old_value: Any, new_value: Any, action: str
28
+ ) -> Tuple[bool, Any]:
29
+ """Call entity's on_event hook if defined.
30
+
31
+ Args:
32
+ entity: Entity instance
33
+ field_name: Field being changed (None for entity-level actions)
34
+ old_value: Previous value
35
+ new_value: New value
36
+ action: ACTION_CREATE, ACTION_MODIFY, or ACTION_DELETE
37
+
38
+ Returns:
39
+ (allow: bool, modified_value: Any)
40
+ """
41
+ if hasattr(entity.__class__, "on_event"):
42
+ hook = entity.__class__.on_event
43
+ if callable(hook):
44
+ result = hook(entity, field_name, old_value, new_value, action)
45
+
46
+ if isinstance(result, tuple) and len(result) == 2:
47
+ return result
48
+ elif isinstance(result, bool):
49
+ return result, new_value
50
+
51
+ return True, new_value
ic_python_db/mixins.py ADDED
@@ -0,0 +1,65 @@
1
+ """Mixins for adding functionality to database entities."""
2
+
3
+ from typing import Any, Dict, Optional
4
+
5
+ from .system_time import SystemTime
6
+
7
+
8
+ class TimestampedMixin:
9
+ """Mixin that adds creation and update timestamps to entities."""
10
+
11
+ def __init__(self):
12
+ self._timestamp_created = 0
13
+ self._timestamp_updated = 0
14
+ self._creator = None
15
+ self._updater = None
16
+ self._owner = None
17
+
18
+ def _update_timestamps(self, caller_id: Optional[str] = None) -> None:
19
+ """Update timestamps and track who made the changes.
20
+
21
+ Args:
22
+ caller_id: ID of the user/process making the change
23
+ """
24
+ now = SystemTime.get_instance().get_time()
25
+ if not self._timestamp_created:
26
+ self._timestamp_created = now
27
+ self._creator = caller_id
28
+ self._owner = caller_id
29
+
30
+ self._timestamp_updated = now
31
+ self._updater = caller_id
32
+
33
+ def set_owner(self, new_owner: str) -> None:
34
+ """Set a new owner for this entity.
35
+
36
+ Args:
37
+ new_owner: ID of the new owner
38
+ """
39
+ self._owner = new_owner
40
+
41
+ def check_ownership(self, caller_id: str) -> bool:
42
+ """Check if the caller owns this entity.
43
+
44
+ Args:
45
+ caller_id: ID of the caller to check
46
+
47
+ Returns:
48
+ bool: True if caller owns the entity, False otherwise
49
+ """
50
+ return self._owner == caller_id
51
+
52
+ def serialize(self) -> Dict[str, Any]:
53
+ """Convert timestamps and ownership info to a dictionary.
54
+
55
+ Returns:
56
+ Dict containing the timestamp and ownership data
57
+ """
58
+
59
+ return {
60
+ "timestamp_created": SystemTime.format_timestamp(self._timestamp_created),
61
+ "timestamp_updated": SystemTime.format_timestamp(self._timestamp_updated),
62
+ "creator": self._creator,
63
+ "updater": self._updater,
64
+ "owner": self._owner,
65
+ }