Skip to content

Commit

Permalink
Fix error when creating session via REST without providing users (issue
Browse files Browse the repository at this point in the history
  • Loading branch information
k1o0 committed Jan 7, 2025
1 parent b93df5d commit 071cc3a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
7 changes: 5 additions & 2 deletions alyx/actions/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class BaseActionSerializer(serializers.HyperlinkedModelSerializer):
slug_field='username',
queryset=get_user_model().objects.all(),
required=False,
default=serializers.CurrentUserDefault(),
)

location = serializers.SlugRelatedField(
Expand All @@ -57,7 +56,6 @@ class BaseActionSerializer(serializers.HyperlinkedModelSerializer):
queryset=LabLocation.objects.all(),
allow_null=True,
required=False,

)

procedures = serializers.SlugRelatedField(
Expand All @@ -76,6 +74,11 @@ class BaseActionSerializer(serializers.HyperlinkedModelSerializer):
many=False,
required=False,)

def create(self, validated_data):
if not validated_data.get('users'):
validated_data['users'] = [self.context['request'].user]
return super().create(validated_data)


class LabLocationSerializer(serializers.ModelSerializer):

Expand Down
18 changes: 9 additions & 9 deletions alyx/actions/tests_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def test_sessions_projects(self):
def test_sessions(self):
a_dict4json = {'String': 'this is not a JSON', 'Integer': 4, 'List': ['titi', 4]}
ses_dict = {'subject': self.subject.nickname,
'users': [self.superuser.username],
'users': [self.superuser2.username],
'projects': [self.projectX.name, self.projectY.name],
'narrative': 'auto-generated-session, test',
'start_time': '2018-07-09T12:34:56',
Expand Down Expand Up @@ -223,24 +223,24 @@ def test_sessions(self):
# create another session for further testing
ses_dict['start_time'] = '2018-07-11T12:34:56'
ses_dict['end_time'] = '2018-07-11T12:34:57'
ses_dict['users'] = [self.superuser.username, self.superuser2.username]
# should use default user when not provided
del ses_dict['users']
ses_dict['lab'] = self.lab02.name
ses_dict['n_correct_trials'] = 37
r = self.post(reverse('session-list'), data=ses_dict)
s2 = self.ar(r, code=201)
self.assertEqual(['test'], s2['users'])
s2.pop('json')
# Test the date range filter
r = self.client.get(reverse('session-list') + '?date_range=2018-07-09,2018-07-09')
rdata = self.ar(r)
self.assertEqual(rdata[0], s1)
# Test the user filter, this should return 2 sessions
# Test the user filter, this should return 1 session
d = self.ar(self.client.get(reverse('session-list') + '?users=test'))
self.assertEqual(len(d), 2)
# This should return only one session
d = self.ar(self.client.get(reverse('session-list') + '?users=test2'))
self.assertEqual(len(d), 1)
for k in d[0]:
self.assertEqual(d[0][k], s2[k])
# This should return 0 sessions
d = self.ar(self.client.get(reverse('session-list') + '?users=foo'))
self.assertEqual(len(d), 0)
# This should return only one session
d = self.ar(self.client.get(reverse('session-list') + '?lab=awesomelab'))
self.assertEqual(len(d), 1)
Expand All @@ -254,7 +254,7 @@ def test_sessions(self):
self.assertEqual(d[0]['url'], s2['url'])
self.assertEqual(1, len(d))
# test the Session serializer water admin related field
ses = Session.objects.get(subject=self.subject, users=self.superuser,
ses = Session.objects.get(subject=self.subject, users=self.superuser2,
lab__name='superlab', start_time__date='2018-07-09')
WaterAdministration.objects.create(subject=self.subject, session=ses, water_administered=1)
d = self.ar(self.client.get(reverse('session-list') + '?date_range=2018-07-09,2018-07-09'))
Expand Down

0 comments on commit 071cc3a

Please sign in to comment.