f3-data-models 0.3.7__tar.gz → 0.3.8__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.
- {f3_data_models-0.3.7 → f3_data_models-0.3.8}/PKG-INFO +1 -1
- {f3_data_models-0.3.7 → f3_data_models-0.3.8}/f3_data_models/models.py +22 -0
- {f3_data_models-0.3.7 → f3_data_models-0.3.8}/f3_data_models/testing.py +3 -10
- {f3_data_models-0.3.7 → f3_data_models-0.3.8}/f3_data_models/utils.py +8 -6
- {f3_data_models-0.3.7 → f3_data_models-0.3.8}/pyproject.toml +1 -1
- {f3_data_models-0.3.7 → f3_data_models-0.3.8}/README.md +0 -0
- {f3_data_models-0.3.7 → f3_data_models-0.3.8}/f3_data_models/__init__.py +0 -0
@@ -17,6 +17,7 @@ from sqlalchemy import (
|
|
17
17
|
UniqueConstraint,
|
18
18
|
Enum,
|
19
19
|
Uuid,
|
20
|
+
inspect,
|
20
21
|
)
|
21
22
|
from typing_extensions import Annotated
|
22
23
|
from sqlalchemy.orm import (
|
@@ -25,6 +26,7 @@ from sqlalchemy.orm import (
|
|
25
26
|
Mapped,
|
26
27
|
relationship,
|
27
28
|
)
|
29
|
+
from sqlalchemy.orm.attributes import InstrumentedAttribute
|
28
30
|
import enum
|
29
31
|
|
30
32
|
# Custom Annotations
|
@@ -150,6 +152,26 @@ class Base(DeclarativeBase):
|
|
150
152
|
if c.key not in ["created", "updated"]
|
151
153
|
}
|
152
154
|
|
155
|
+
def to_update_dict(self) -> Dict[InstrumentedAttribute, Any]:
|
156
|
+
update_dict = {}
|
157
|
+
mapper = inspect(self).mapper
|
158
|
+
|
159
|
+
# Add simple attributes
|
160
|
+
for attr in mapper.column_attrs:
|
161
|
+
if attr.key not in ["created", "updated", "id"]:
|
162
|
+
update_dict[attr] = getattr(self, attr.key)
|
163
|
+
|
164
|
+
# Add relationships
|
165
|
+
for rel in mapper.relationships:
|
166
|
+
related_value = getattr(self, rel.key)
|
167
|
+
if related_value is not None:
|
168
|
+
if rel.uselist:
|
169
|
+
update_dict[rel] = [item for item in related_value]
|
170
|
+
print(rel, update_dict[rel])
|
171
|
+
else:
|
172
|
+
update_dict[rel] = related_value
|
173
|
+
return update_dict
|
174
|
+
|
153
175
|
def __repr__(self):
|
154
176
|
"""
|
155
177
|
Get a string representation of the model instance.
|
@@ -15,7 +15,7 @@ def test_update_event():
|
|
15
15
|
start_time="0500",
|
16
16
|
end_time="0600",
|
17
17
|
event_x_event_types=[
|
18
|
-
EventType_x_Event(event_type_id=
|
18
|
+
EventType_x_Event(event_type_id=3),
|
19
19
|
],
|
20
20
|
recurrence_pattern=Event_Cadence.weekly,
|
21
21
|
day_of_week=Day_Of_Week.monday,
|
@@ -23,17 +23,10 @@ def test_update_event():
|
|
23
23
|
index_within_interval=1,
|
24
24
|
name="Test Event",
|
25
25
|
)
|
26
|
-
update_dict = event.
|
26
|
+
update_dict = event.to_update_dict()
|
27
27
|
DbManager.update_records(Event, [Event.id == 3], update_dict)
|
28
28
|
|
29
|
-
|
30
|
-
# # delete event_x_event_types
|
31
|
-
# session.query(EventType_x_Event).filter(
|
32
|
-
# EventType_x_Event.event_id == 3
|
33
|
-
# ).delete()
|
34
|
-
# # add event_x_event_types
|
35
|
-
# session.add(EventType_x_Event(event_id=3, event_type_id=4))
|
36
|
-
# session.commit()
|
29
|
+
event = DbManager.get(Event, 3)
|
37
30
|
|
38
31
|
|
39
32
|
if __name__ == "__main__":
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import os
|
2
2
|
from dataclasses import dataclass
|
3
|
-
from typing import List, Optional, Tuple, TypeVar
|
3
|
+
from typing import List, Optional, Tuple, TypeVar, Type, Generic # noqa
|
4
4
|
|
5
5
|
import sqlalchemy
|
6
6
|
from sqlalchemy import Select, and_, select, inspect
|
@@ -89,7 +89,7 @@ def _joinedloads(cls: T, query: Select, joinedloads: list | str) -> Select:
|
|
89
89
|
|
90
90
|
|
91
91
|
class DbManager:
|
92
|
-
def get(cls: T, id: int, joinedloads: list | str = []) -> T:
|
92
|
+
def get(cls: Type[T], id: int, joinedloads: list | str = []) -> T:
|
93
93
|
session = get_session()
|
94
94
|
try:
|
95
95
|
query = select(cls).filter(cls.id == id)
|
@@ -175,9 +175,9 @@ class DbManager:
|
|
175
175
|
|
176
176
|
mapper = class_mapper(cls)
|
177
177
|
relationships = mapper.relationships.keys()
|
178
|
-
for
|
178
|
+
for attr, value in fields.items():
|
179
|
+
key = attr.key
|
179
180
|
if hasattr(cls, key) and key not in relationships:
|
180
|
-
attr = getattr(cls, key)
|
181
181
|
if isinstance(attr, InstrumentedAttribute):
|
182
182
|
setattr(record, key, value)
|
183
183
|
elif key in relationships:
|
@@ -232,14 +232,16 @@ class DbManager:
|
|
232
232
|
|
233
233
|
for obj in objects:
|
234
234
|
# Update simple fields
|
235
|
-
for
|
235
|
+
for attr, value in fields.items():
|
236
|
+
key = attr.key
|
236
237
|
if key in valid_attributes and not isinstance(
|
237
238
|
value, InstrumentedList
|
238
239
|
):
|
239
240
|
setattr(obj, key, value)
|
240
241
|
|
241
242
|
# Update relationships separately
|
242
|
-
for
|
243
|
+
for attr, value in fields.items():
|
244
|
+
key = attr.key
|
243
245
|
if key in valid_relationships:
|
244
246
|
# Handle relationships separately
|
245
247
|
relationship = inspect(cls).mapper.relationships[key]
|
File without changes
|
File without changes
|