Skip to content

Commit

Permalink
Org: Avoids storing ORM objects in orm_cached properties
Browse files Browse the repository at this point in the history
This should improve reliability and should introduce less flaky
behavior caused by incorrect merges of objects into the session.

TYPE: Bugfix
LINK: OGC-1813
  • Loading branch information
Daverball authored Sep 13, 2024
1 parent d64955e commit f864532
Show file tree
Hide file tree
Showing 39 changed files with 941 additions and 465 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ eggs/
.tmontmp

# mypy
.mypy_cache/*
.mypy_cache/

# ruff
.ruff_cache/

# sublime
*.sublime-workspace
Expand Down
2 changes: 2 additions & 0 deletions src/onegov/activity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
OccasionDate,
OccasionNeed,
Period,
PeriodMeta,
PublicationRequest,
Volunteer
)
Expand Down Expand Up @@ -41,6 +42,7 @@
'OccasionDate',
'OccasionNeed',
'Period',
'PeriodMeta',
'ActivityCollection',
'AttendeeCollection',
'BookingCollection',
Expand Down
3 changes: 2 additions & 1 deletion src/onegov/activity/collections/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from collections.abc import Collection, Iterable, Iterator
from datetime import date
from markupsafe import Markup
from onegov.activity.models import PeriodMeta
from onegov.activity.models.activity import ActivityState
from onegov.user import User
from sqlalchemy.orm import Query, Session
Expand Down Expand Up @@ -578,7 +579,7 @@ def delete(self, activity: Activity) -> None:

def available_weeks(
self,
period: Period | None
period: 'Period | PeriodMeta | None'
) -> 'Iterator[tuple[date, date]]':
if not period:
return
Expand Down
10 changes: 7 additions & 3 deletions src/onegov/activity/collections/volunteer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
if TYPE_CHECKING:
from collections.abc import Sequence
from datetime import date, datetime
from onegov.activity.models import Period
from onegov.activity.models import Period, PeriodMeta
from onegov.activity.models.volunteer import VolunteerState
from sqlalchemy.orm import Query, Session
from uuid import UUID
Expand Down Expand Up @@ -73,7 +73,11 @@ class ReportRowWithoutVolunteer(NamedTuple):

class VolunteerCollection(GenericCollection[Volunteer]):

def __init__(self, session: 'Session', period: 'Period | None') -> None:
def __init__(
self,
session: 'Session',
period: 'Period | PeriodMeta | None'
) -> None:
super().__init__(session)
self.period = period

Expand All @@ -93,5 +97,5 @@ def report(self) -> 'Query[ReportRow]':

return self.session.execute(query)

def for_period(self, period: 'Period | None') -> 'Self':
def for_period(self, period: 'Period | PeriodMeta | None') -> 'Self':
return self.__class__(self.session, period)
3 changes: 2 additions & 1 deletion src/onegov/activity/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from onegov.activity.models.occasion import Occasion
from onegov.activity.models.occasion_date import OccasionDate, DAYS
from onegov.activity.models.occasion_need import OccasionNeed
from onegov.activity.models.period import Period
from onegov.activity.models.period import Period, PeriodMeta
from onegov.activity.models.publication_request import PublicationRequest
from onegov.activity.models.volunteer import Volunteer

Expand All @@ -22,6 +22,7 @@
'OccasionDate',
'OccasionNeed',
'Period',
'PeriodMeta',
'PublicationRequest',
'Volunteer',
'ACTIVITY_STATES',
Expand Down
6 changes: 3 additions & 3 deletions src/onegov/activity/models/activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import uuid
from collections.abc import Iterable
from onegov.activity.collections import PublicationRequestCollection
from onegov.activity.models import PublicationRequest
from onegov.activity.models import PeriodMeta, PublicationRequest
from onegov.core.orm.mixins import dict_property
from typing import Literal
from typing import Self, TypeAlias
Expand Down Expand Up @@ -218,7 +218,7 @@ def latest_request(self) -> 'PublicationRequest | None':

def request_by_period(
self,
period: Period | None
period: 'Period | PeriodMeta | None'
) -> 'PublicationRequest | None':

if not period:
Expand All @@ -228,7 +228,7 @@ def request_by_period(

return q.first()

def has_occasion_in_period(self, period: Period) -> bool:
def has_occasion_in_period(self, period: 'Period | PeriodMeta') -> bool:
q = object_session(self).query(
exists().where(and_(
Occasion.activity_id == self.id,
Expand Down
Loading

0 comments on commit f864532

Please sign in to comment.