Skip to content

Commit

Permalink
Cache availability_helpers, to reduce SQL overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
OdyX committed Mar 24, 2019
1 parent 7acbe29 commit e3f320e
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions apps/challenge/views/season.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from django.db.models import Case, Count, F, IntegerField, Q, When from django.db.models import Case, Count, F, IntegerField, Q, When
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.template.defaultfilters import date, time from django.template.defaultfilters import date, time
from django.utils.functional import cached_property
from django.utils.translation import ugettext as u, ugettext_lazy as _ from django.utils.translation import ugettext as u, ugettext_lazy as _
from django.views.generic import ListView from django.views.generic import ListView
from django.views.generic.detail import DetailView from django.views.generic.detail import DetailView
Expand Down Expand Up @@ -201,6 +202,7 @@ def current_availabilities(self):
def current_availabilities_present(self): def current_availabilities_present(self):
return self.current_availabilities().exclude(availability='n') return self.current_availabilities().exclude(availability='n')


@cached_property
def available_helpers(self): def available_helpers(self):
# Only take available people # Only take available people
# Fill in the helpers with the ones we currently have # Fill in the helpers with the ones we currently have
Expand Down Expand Up @@ -696,28 +698,28 @@ def get_initial(self):
super(SeasonStaffChoiceUpdateView, self) super(SeasonStaffChoiceUpdateView, self)
.get_initial( .get_initial(
all_hsas=None, all_hsas=None,
all_helpers=self.available_helpers() all_helpers=self.available_helpers
) )
) )


def get_form_kwargs(self): def get_form_kwargs(self):
form_kwargs = \ form_kwargs = \
super(SeasonStaffChoiceUpdateView, self).get_form_kwargs() super(SeasonStaffChoiceUpdateView, self).get_form_kwargs()
form_kwargs['available_helpers'] = self.available_helpers() form_kwargs['available_helpers'] = self.available_helpers
return form_kwargs return form_kwargs


def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
context = \ context = \
super(SeasonStaffChoiceUpdateView, self).get_context_data(**kwargs) super(SeasonStaffChoiceUpdateView, self).get_context_data(**kwargs)
context['available_helpers'] = self.available_helpers() context['available_helpers'] = self.available_helpers
return context return context


def form_valid(self, form): def form_valid(self, form):
# Update all staff choices # Update all staff choices
for session in self.object.sessions_with_qualifs: for session in self.object.sessions_with_qualifs:
session_helpers = {} # Those picked for the season session_helpers = {} # Those picked for the season
session_non_helpers = {} # Those not picked for the season session_non_helpers = {} # Those not picked for the season
for helper_category, helpers in self.available_helpers(): for helper_category, helpers in self.available_helpers:
for helper in helpers: for helper in helpers:
fieldkey = STAFF_FIELDKEY.format(hpk=helper.pk, fieldkey = STAFF_FIELDKEY.format(hpk=helper.pk,
spk=session.pk) spk=session.pk)
Expand Down

0 comments on commit e3f320e

Please sign in to comment.