f3-data-models 0.3.5__tar.gz → 0.3.6__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.5 → f3_data_models-0.3.6}/PKG-INFO +1 -1
- {f3_data_models-0.3.5 → f3_data_models-0.3.6}/f3_data_models/utils.py +56 -14
- {f3_data_models-0.3.5 → f3_data_models-0.3.6}/pyproject.toml +1 -1
- {f3_data_models-0.3.5 → f3_data_models-0.3.6}/README.md +0 -0
- {f3_data_models-0.3.5 → f3_data_models-0.3.6}/f3_data_models/__init__.py +0 -0
- {f3_data_models-0.3.5 → f3_data_models-0.3.6}/f3_data_models/models.py +0 -0
@@ -3,11 +3,13 @@ from dataclasses import dataclass
|
|
3
3
|
from typing import List, Optional, Tuple, TypeVar
|
4
4
|
|
5
5
|
import sqlalchemy
|
6
|
-
from sqlalchemy import Select, and_, select
|
6
|
+
from sqlalchemy import Select, and_, select, inspect
|
7
7
|
|
8
8
|
from sqlalchemy.dialects.postgresql import insert
|
9
9
|
from sqlalchemy.engine import Engine
|
10
10
|
from sqlalchemy.orm import sessionmaker, joinedload
|
11
|
+
from sqlalchemy.orm.collections import InstrumentedList
|
12
|
+
from sqlalchemy.orm.attributes import flag_modified
|
11
13
|
|
12
14
|
from f3_data_models.models import Base
|
13
15
|
|
@@ -166,21 +168,61 @@ class DbManager:
|
|
166
168
|
|
167
169
|
def update_record(cls: T, id, fields):
|
168
170
|
session = get_session()
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
)
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
171
|
+
# Fetch the object to be updated
|
172
|
+
obj = session.query(cls).get(id)
|
173
|
+
if not obj:
|
174
|
+
raise ValueError(f"Object with id {id} not found")
|
175
|
+
|
176
|
+
# Get the list of valid attributes for the class
|
177
|
+
valid_attributes = {attr.key for attr in inspect(cls).mapper.column_attrs}
|
178
|
+
valid_relationships = {rel.key for rel in inspect(cls).mapper.relationships}
|
179
|
+
|
180
|
+
# Update simple fields
|
181
|
+
for key, value in fields.items():
|
182
|
+
if key in valid_attributes and not isinstance(value, InstrumentedList):
|
183
|
+
setattr(obj, key, value)
|
184
|
+
|
185
|
+
# Update relationships
|
186
|
+
for key, value in fields.items():
|
187
|
+
if key in valid_relationships and isinstance(value, InstrumentedList):
|
188
|
+
related_objects = getattr(obj, key)
|
189
|
+
related_objects.clear()
|
190
|
+
related_objects.extend(value)
|
191
|
+
|
192
|
+
# Flag the object as modified to ensure changes are detected
|
193
|
+
flag_modified(obj, key)
|
194
|
+
|
195
|
+
# Commit the changes
|
196
|
+
session.commit()
|
197
|
+
|
198
|
+
def update_records(cls, filters, fields):
|
179
199
|
session = get_session()
|
180
200
|
try:
|
181
|
-
|
182
|
-
|
183
|
-
|
201
|
+
# Fetch the objects to be updated
|
202
|
+
objects = session.query(cls).filter(and_(*filters)).all()
|
203
|
+
|
204
|
+
# Get the list of valid attributes for the class
|
205
|
+
valid_attributes = {attr.key for attr in inspect(cls).mapper.column_attrs}
|
206
|
+
valid_relationships = {rel.key for rel in inspect(cls).mapper.relationships}
|
207
|
+
|
208
|
+
for obj in objects:
|
209
|
+
# Update simple fields
|
210
|
+
for key, value in fields.items():
|
211
|
+
if key in valid_attributes and not isinstance(
|
212
|
+
value, InstrumentedList
|
213
|
+
):
|
214
|
+
setattr(obj, key, value)
|
215
|
+
|
216
|
+
# Update relationships
|
217
|
+
for key, value in fields.items():
|
218
|
+
if key in valid_relationships and isinstance(
|
219
|
+
value, InstrumentedList
|
220
|
+
):
|
221
|
+
related_objects = getattr(obj, key)
|
222
|
+
related_objects.clear()
|
223
|
+
related_objects.extend(value)
|
224
|
+
flag_modified(obj, key)
|
225
|
+
|
184
226
|
session.flush()
|
185
227
|
finally:
|
186
228
|
session.commit()
|
File without changes
|
File without changes
|
File without changes
|