f3-data-models 0.3.7__py3-none-any.whl → 0.3.8__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/models.py CHANGED
@@ -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.
f3_data_models/testing.py CHANGED
@@ -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=4),
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.__dict__
26
+ update_dict = event.to_update_dict()
27
27
  DbManager.update_records(Event, [Event.id == 3], update_dict)
28
28
 
29
- # with get_session() as session:
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__":
f3_data_models/utils.py CHANGED
@@ -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 key, value in fields.items():
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 key, value in fields.items():
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 key, value in fields.items():
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]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: f3-data-models
3
- Version: 0.3.7
3
+ Version: 0.3.8
4
4
  Summary: The data schema and models for F3 Nation applications.
5
5
  License: MIT
6
6
  Author: Evan Petzoldt
@@ -0,0 +1,7 @@
1
+ f3_data_models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
+ f3_data_models/models.py,sha256=jGxAG5Dmzhf2u4cLMUxY48yKENn5HC5N9_eUUSb31yc,45109
3
+ f3_data_models/testing.py,sha256=Pd-TYvkeai2DuN9ubPPdqjo4xS_Zd3T1WGV7J_SXPdc,921
4
+ f3_data_models/utils.py,sha256=UpNx1E_kmt8_1vN4fdFSy55yPCoDA2algzof_5EAJ2k,13835
5
+ f3_data_models-0.3.8.dist-info/METADATA,sha256=Hza4-PUUC0sK4D-BJHGIx81WXrSjcS8PFYB98euxGhE,2716
6
+ f3_data_models-0.3.8.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
7
+ f3_data_models-0.3.8.dist-info/RECORD,,
@@ -1,7 +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/testing.py,sha256=vetCEBQC8R1hf0TcH3-lINXdlHMVIcjC6OhVWiiFWU0,1204
4
- f3_data_models/utils.py,sha256=5Hg0f1cPMYc-URdTbDTLMiG9rCFN5NskJ4lUHw1a2NU,13747
5
- f3_data_models-0.3.7.dist-info/METADATA,sha256=F34OA9YQW-60PwJ77YytFIHNL_-zMlbew6t23Rrdfm4,2716
6
- f3_data_models-0.3.7.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
7
- f3_data_models-0.3.7.dist-info/RECORD,,