Skip to content

Commit

Permalink
Do not add subject's projects to session projects upon save
Browse files Browse the repository at this point in the history
  • Loading branch information
k1o0 committed Mar 21, 2024
1 parent 8c4cbe5 commit 4b28460
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 35 deletions.
6 changes: 1 addition & 5 deletions alyx/actions/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,7 @@ def save(self, *args, **kwargs):
# Default project is the subject's projects.
if not self.lab:
self.lab = self.subject.lab
obj = super(Session, self).save(*args, **kwargs)
if self.projects.count() == 0 and self.subject.projects.count() > 0:
from subjects.models import Project
self.projects.add(*Project.objects.filter(subject=self.subject))
return obj
return super(Session, self).save(*args, **kwargs)

def __str__(self):
try:
Expand Down
32 changes: 2 additions & 30 deletions alyx/actions/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from alyx import base
from actions.water_control import to_date
from actions.models import (
WaterAdministration, WaterRestriction, WaterType, Weighing, Session,
WaterAdministration, WaterRestriction, WaterType, Weighing,
Notification, NotificationRule, create_notification, Surgery, ProcedureType)
from actions.notifications import check_water_administration, check_weighed
from misc.models import LabMember, LabMembership, Lab
from subjects.models import Subject, Project
from subjects.models import Subject


class WaterControlTests(TestCase):
Expand Down Expand Up @@ -254,31 +254,3 @@ def _assert_users(users, expected):
nr.subjects_scope = 'none'
nr.save()
_assert_users([self.user2], [self.user2])


class SessionTests(TestCase):

def setUp(self):
# Create a subject
self.lab = Lab.objects.create(name='testlab', reference_weight_pct=.85)
self.subject = Subject.objects.create(
nickname='bigboy', birth_date='2018-09-01', lab=self.lab)
self.projects = [Project.objects.create(name=f'project_{i}') for i in range(4)]

def test_session_create(self):
"""Test subject projects added when session has none."""
# First create a session with no projects and no subject projects
start_time = datetime.datetime(year=2018, month=10, day=1)
session = Session.objects.create(
subject=self.subject, start_time=start_time, number=1, type='Experiment')
session.save()
self.assertFalse(session.projects.count())
# Now save after adding projects to the associated subject
self.subject.projects.add(*[x.pk for x in self.projects[:2]])
session.save()
self.assertEqual(2, session.projects.count())
# Now create a new session and add projects before saving
session = Session.objects.create(
subject=self.subject, start_time=start_time, number=2, type='Experiment')
session.projects.add(self.projects[-1].pk)
self.assertEqual(3, session.projects.count())

0 comments on commit 4b28460

Please sign in to comment.