f3-data-models 0.3.4__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: f3-data-models
3
- Version: 0.3.4
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
@@ -566,8 +566,8 @@ class Event(Base):
566
566
  highlight (bool): Whether the event is highlighted. Default is False.
567
567
  start_date (date): The start date of the event.
568
568
  end_date (Optional[date]): The end date of the event.
569
- start_time (Optional[time_with_tz]): The start time of the event.
570
- end_time (Optional[time_with_tz]): The end time of the event.
569
+ start_time (Optional[str]): The start time of the event. Format is 'HHMM', 24-hour time, timezone naive.
570
+ end_time (Optional[str]): The end time of the event. Format is 'HHMM', 24-hour time, timezone naive.
571
571
  day_of_week (Optional[Day_Of_Week]): The day of the week of the event.
572
572
  name (str): The name of the event.
573
573
  description (Optional[text]): A description of the event.
@@ -606,8 +606,8 @@ class Event(Base):
606
606
  highlight: Mapped[bool] = mapped_column(Boolean, default=False)
607
607
  start_date: Mapped[date]
608
608
  end_date: Mapped[Optional[date]]
609
- start_time: Mapped[Optional[time_notz]]
610
- end_time: Mapped[Optional[time_notz]]
609
+ start_time: Mapped[Optional[str]]
610
+ end_time: Mapped[Optional[str]]
611
611
  day_of_week: Mapped[Optional[Day_Of_Week]]
612
612
  name: Mapped[str]
613
613
  description: Mapped[Optional[text]]
@@ -1066,8 +1066,8 @@ class UpdateRequest(Base):
1066
1066
  event_highlight (Optional[bool]): Whether the event is highlighted.
1067
1067
  event_start_date (Optional[date]): The start date of the event.
1068
1068
  event_end_date (Optional[date]): The end date of the event.
1069
- event_start_time (Optional[time_notz]): The start time of the event.
1070
- event_end_time (Optional[time_notz]): The end time of the event.
1069
+ event_start_time (Optional[str]): The start time of the event. Format is 'HHMM', 24-hour time, timezone naive.
1070
+ event_end_time (Optional[str]): The end time of the event. Format is 'HHMM', 24-hour time, timezone naive.
1071
1071
  event_day_of_week (Optional[Day_Of_Week]): The day of the week of the event.
1072
1072
  event_name (str): The name of the event.
1073
1073
  event_description (Optional[text]): A description of the event.
@@ -1113,8 +1113,8 @@ class UpdateRequest(Base):
1113
1113
  event_highlight: Mapped[Optional[bool]]
1114
1114
  event_start_date: Mapped[Optional[date]]
1115
1115
  event_end_date: Mapped[Optional[date]]
1116
- event_start_time: Mapped[Optional[time_notz]]
1117
- event_end_time: Mapped[Optional[time_notz]]
1116
+ event_start_time: Mapped[Optional[str]]
1117
+ event_end_time: Mapped[Optional[str]]
1118
1118
  event_day_of_week: Mapped[Optional[Day_Of_Week]]
1119
1119
  event_name: Mapped[str]
1120
1120
  event_description: Mapped[Optional[text]]
@@ -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
  [tool.poetry]
2
2
  name = "f3-data-models"
3
- version = "0.3.4"
3
+ version = "0.3.6"
4
4
  description = "The data schema and models for F3 Nation applications."
5
5
  authors = ["Evan Petzoldt <evan.petzoldt@protonmail.com>"]
6
6
  readme = "README.md"
File without changes