From 4b28460544c3cedfd9f83bbbe0230430f690ce73 Mon Sep 17 00:00:00 2001 From: k1o0 Date: Thu, 21 Mar 2024 18:10:40 +0200 Subject: [PATCH] Do not add subject's projects to session projects upon save --- alyx/actions/models.py | 6 +----- alyx/actions/tests.py | 32 ++------------------------------ 2 files changed, 3 insertions(+), 35 deletions(-) diff --git a/alyx/actions/models.py b/alyx/actions/models.py index ea36f62b..488cfb54 100644 --- a/alyx/actions/models.py +++ b/alyx/actions/models.py @@ -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: diff --git a/alyx/actions/tests.py b/alyx/actions/tests.py index 47246197..989a2743 100644 --- a/alyx/actions/tests.py +++ b/alyx/actions/tests.py @@ -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): @@ -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())