Skip to content

Commit

Permalink
fix caching
Browse files Browse the repository at this point in the history
  • Loading branch information
felixrindt committed Sep 10, 2024
1 parent d03a9bb commit d1654ac
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
28 changes: 26 additions & 2 deletions ephios/core/models/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ class Shift(DatetimeDisplayMixin, Model):

structure_slug = SlugField(_("structure"))
structure_configuration = JSONField(
default=dict, encoder=CustomJSONEncoder, decoder=CustomJSONDecoder
default=dict,
encoder=CustomJSONEncoder,
decoder=CustomJSONDecoder,
)

class Meta:
Expand All @@ -272,7 +274,7 @@ class Meta:
ordering = ("meeting_time", "start_time", "id")
db_table = "shift"

@property
@cached_property
def signup_flow(self) -> "AbstractSignupFlow":
from ephios.core.signup.flow import signup_flow_from_slug

Expand Down Expand Up @@ -304,6 +306,28 @@ def structure(self) -> "AbstractShiftStructure":

return FallbackShiftStructure(self, event=event)

def _clear_cached_signup_objects(self):
"""
when changing data on the shift, cached info in the signup flow or structure might become outdated,
so we clear the cached versions. (most relevant to tests, as we use new instances every request)
"""
try:
del self.structure
except AttributeError:
pass
try:
del self.signup_flow
except AttributeError:
pass

def save(self, *args, **kwargs):
self._clear_cached_signup_objects()
return super().save(*args, **kwargs)

def refresh_from_db(self, using=None, fields=None):
self._clear_cached_signup_objects()
return super().refresh_from_db(using, fields)

def get_participants(self, with_state_in=frozenset({AbstractParticipation.States.CONFIRMED})):
for participation in self.participations.filter(state__in=with_state_in):
yield participation.participant
Expand Down
2 changes: 1 addition & 1 deletion ephios/core/services/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Position:
designated_for: Collection[AbstractParticipant] # designated by disposition
preferred_by: Collection[AbstractParticipant] # preferred by participant (less important)
label: Optional[str] = None
aux_score: float = 0.0 # additional score control
aux_score: float = 0.0 # additional score control in range [0,1]

def __post_init__(self):
self.required_qualifications = frozenset(self.required_qualifications)
Expand Down
3 changes: 3 additions & 0 deletions ephios/core/signup/flow/participant_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
class BaseSignupError(Exception):
"""Superclass of errors used in the signup mechanism."""

def __init__(self, message):
self.message = message


class SignupDisallowedError(BaseSignupError):
"""
Expand Down
1 change: 1 addition & 0 deletions ephios/plugins/complexsignup/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ def _search_block(
preferred_by=preferred_by,
required=required,
label=label,
aux_score=1,
)
participation = matching.participation_for_position(match_id) if matching else None
signup_stats += SignupStats.ZERO.replace(
Expand Down
5 changes: 5 additions & 0 deletions ephios/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,8 @@ def GET_SITE_URL():

if env.bool("TRUST_X_FORWARDED_PROTO", default=False):
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")

# Transitional/deprecated settings
FORMS_URLFIELD_ASSUME_HTTPS = (
True # https://docs.djangoproject.com/en/5.1/ref/settings/#forms-urlfield-assume-https
)
12 changes: 6 additions & 6 deletions tests/core/services/test_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ def test_match_participants_to_positions(qualifications):
# create 3 participants
ann = PlaceholderParticipant("Ann", {qualifications.nfs}, None, None)
ben = PlaceholderParticipant("Ben", {qualifications.rs, qualifications.ce}, None, None)
leader = Position("Führer", True, {qualifications.nfs}, [])
driver = Position("Fahrer", True, {qualifications.rs, qualifications.c}, [])
leader = Position("Führer", True, {qualifications.nfs}, [], [])
driver = Position("Fahrer", True, {qualifications.rs, qualifications.c}, [], [])

assert match_participants_to_positions([], []).pairings == set()
assert match_participants_to_positions([ben], []).pairings == set()
Expand Down Expand Up @@ -51,10 +51,10 @@ def test_maximise_number_of_matches_in_adverse_case():
z = PlaceholderParticipant("Z", {q_d}, None, None)

positions = [
Position("A", False, {q_a}, []),
Position("B", False, {q_b}, [w]),
Position("C", False, {q_c}, [x]),
Position("D", False, {q_d}, [y]),
Position("A", False, {q_a}, [], []),
Position("B", False, {q_b}, [], [w]),
Position("C", False, {q_c}, [], [x]),
Position("D", False, {q_d}, [], [y]),
]
participants = [w, x, y, z]

Expand Down

0 comments on commit d1654ac

Please sign in to comment.