f3-data-models 0.3.5__py3-none-any.whl → 0.3.6__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.
f3_data_models/utils.py CHANGED
@@ -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
- try:
170
- session.query(cls).filter(cls.id == id).update(
171
- fields, synchronize_session="fetch"
172
- )
173
- session.flush()
174
- finally:
175
- session.commit()
176
- close_session(session)
177
-
178
- def update_records(cls: T, filters, fields):
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
- session.query(cls).filter(and_(*filters)).update(
182
- fields, synchronize_session="fetch"
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()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: f3-data-models
3
- Version: 0.3.5
3
+ Version: 0.3.6
4
4
  Summary: The data schema and models for F3 Nation applications.
5
5
  License: MIT
6
6
  Author: Evan Petzoldt
@@ -0,0 +1,6 @@
1
+ f3_data_models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ f3_data_models/models.py,sha256=SLOXwM2BMnlGTwkVZfG3fM9hoksvPkcFRqzpEI4bm2Y,44293
3
+ f3_data_models/utils.py,sha256=fGv-qHviSsUqHmYElBvYgWbqX5LAlTJCoKSMIqd8ioA,10738
4
+ f3_data_models-0.3.6.dist-info/METADATA,sha256=G6dtENVh5a3JKZ4NdCl3DVY6dTybhdEl5jLvQmKnFHY,2716
5
+ f3_data_models-0.3.6.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
6
+ f3_data_models-0.3.6.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 2.0.1
2
+ Generator: poetry-core 2.1.1
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -1,6 +0,0 @@
1
- f3_data_models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- f3_data_models/models.py,sha256=SLOXwM2BMnlGTwkVZfG3fM9hoksvPkcFRqzpEI4bm2Y,44293
3
- f3_data_models/utils.py,sha256=1hdZ2tvMiNibNZvwt0dx4PhPCw_pJGg1mG2cCvwc6CI,8857
4
- f3_data_models-0.3.5.dist-info/METADATA,sha256=L2i2GhGM0d1ueb9uQuHNqPJs_X1NbuJKdLD6jaKp6RM,2716
5
- f3_data_models-0.3.5.dist-info/WHEEL,sha256=IYZQI976HJqqOpQU6PHkJ8fb3tMNBFjg-Cn-pwAbaFM,88
6
- f3_data_models-0.3.5.dist-info/RECORD,,