Self hosted FLOSS fitness/workout, nutrition and weight tracker
Attach a log to its session under a lock
Roland Geidere844781be63f
diff --git a/wger/manager/models/log.py b/wger/manager/models/log.pyindex 462a8c9a8..9fa4f2f1e 100644--- a/wger/manager/models/log.py+++ b/wger/manager/models/log.py@@ -14,10 +14,16 @@# You should have received a copy of the GNU Affero General Public License# along with this program. If not, see <http://www.gnu.org/licenses/>.+# Standard Library+import datetime+# Djangofrom django.contrib.auth.models import Userfrom django.core.exceptions import ValidationError-from django.db import models+from django.db import (+ models,+ transaction,+)from django.utils import timezone# wger@@ -248,6 +254,52 @@ class WorkoutLog(models.Model):if self.weight is not None and self.weight_unit is None:raise ValidationError('Weight unit must be present if weight has a value.')+ def assign_session(self):+ """+ Attach this log to a session, creating one if nothing fits++ In order: a session the log falls into, then the most recent session that+ is still open and started no longer than a session may last ago, otherwise+ a new one.+ """+ log_date = self.date+ if isinstance(log_date, datetime.date) and not isinstance(log_date, datetime.datetime):+ log_date = timezone.make_aware(datetime.datetime.combine(log_date, datetime.time()))+ elif timezone.is_naive(log_date):+ log_date = timezone.make_aware(log_date)++ # Looking up and creating has to happen as one step, otherwise a batch+ # upload creates a session per log. There is no row yet that could be+ # locked, so the user row stands in for it.+ with transaction.atomic():+ User.objects.select_for_update().filter(pk=self.user_id).first()++ session = WorkoutSession.objects.filter(+ user=self.user,+ routine=self.routine,+ datetime_start__lte=log_date,+ datetime_end__gte=log_date,+ ).first()++ if not session:+ session = (+ WorkoutSession.objects.filter(+ user=self.user,+ routine=self.routine,+ datetime_end__isnull=True,+ datetime_start__gte=log_date - WorkoutSession.max_duration(),+ datetime_start__lte=log_date,+ )+ .order_by('-datetime_start')+ .first()+ )++ self.session = session or WorkoutSession.objects.create(+ user=self.user,+ datetime_start=log_date,+ routine=self.routine,+ )+def save(self, *args, **kwargs):"""Plumbing@@ -271,51 +323,7 @@ class WorkoutLog(models.Model):# Auto-create a session only if the client didn't provide one.if not self.session_id:- # Standard Library- import datetime-- # Django- from django.utils import timezone-- # Ensure self.date is a timezone-aware datetime for comparisons- log_date = self.date- if isinstance(log_date, datetime.date) and not isinstance(log_date, datetime.datetime):- log_date = timezone.make_aware(datetime.datetime.combine(log_date, datetime.time()))- elif timezone.is_naive(log_date):- log_date = timezone.make_aware(log_date)-- # 1. Check if the log falls within an existing closed session's timeframe- matching_session = WorkoutSession.objects.filter(- user=self.user,- routine=self.routine,- datetime_start__lte=log_date,- datetime_end__gte=log_date,- ).first()-- # 2. Check if the log falls within a reasonable window of an "open" session- if not matching_session:- time_threshold = log_date - WorkoutSession.max_duration()-- matching_session = (- WorkoutSession.objects.filter(- user=self.user,- routine=self.routine,- datetime_end__isnull=True,- datetime_start__gte=time_threshold,- datetime_start__lte=log_date,- )- .order_by('-datetime_start')- .first()- )-- if matching_session:- self.session = matching_session- else:- self.session = WorkoutSession.objects.create(- user=self.user,- datetime_start=log_date,- routine=self.routine,- )+ self.assign_session()# If the user of next_log is not this user, remove foreign keyif self.next_log and self.next_log.user != self.user: