Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add projects field to session_info in ProbeInsertion serializer #890

Merged
merged 3 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions alyx/experiments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,30 @@
Channel, BrainRegion, ChronicInsertion, FOV, FOVLocation,
ImagingType, ImagingStack)
from data.models import DatasetType, Dataset, DataRepository, FileRecord
from subjects.models import Subject
from subjects.models import Subject, Project
from misc.models import Lab


class SessionListSerializer(serializers.ModelSerializer):

projects = serializers.SlugRelatedField(read_only=False,
slug_field='name',
queryset=Project.objects.all(),
many=True)

@staticmethod
def setup_eager_loading(queryset):
""" Perform necessary eager loading of data to avoid horrible performance."""
queryset = queryset.select_related('subject', 'lab')
queryset = queryset.prefetch_related('projects')
return queryset.order_by('-start_time')

subject = serializers.SlugRelatedField(read_only=True, slug_field='nickname')
lab = serializers.SlugRelatedField(read_only=True, slug_field='name')

class Meta:
model = Session
fields = ('subject', 'start_time', 'number', 'lab', 'id', 'task_protocol')
fields = ('subject', 'start_time', 'number', 'lab', 'id', 'projects', 'task_protocol')


class TrajectoryEstimateSerializer(serializers.ModelSerializer):
Expand Down
8 changes: 6 additions & 2 deletions alyx/experiments/tests_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from alyx.base import BaseTests
from actions.models import Session, ProcedureType
from misc.models import Lab
from subjects.models import Subject
from subjects.models import Subject, Project
from experiments.models import ProbeInsertion, ImagingType
from data.models import Dataset, DatasetType, Tag

Expand All @@ -22,6 +22,7 @@ def setUp(self):
self.session = Session.objects.first()
# need to add ephys procedure
self.session.task_protocol = 'ephys'
self.session.projects.add(Project.objects.get_or_create(name='brain_wide')[0])
self.session.save()
self.dict_insertion = {'session': str(self.session.id),
'name': 'probe_00',
Expand Down Expand Up @@ -76,6 +77,9 @@ def test_create_list_delete_probe_insertion(self):
# test the list endpoint
response = self.client.get(url)
d = self.ar(response, 200)
self.assertIn('session_info', d[0])
# Ensure the session_info includes the projects as a list of names
self.assertCountEqual(d[0]['session_info'].get('projects', []), ['brain_wide'])

# test the session filter
urlf = url + '?&session=' + str(self.session.id) + '&name=probe_00'
Expand Down Expand Up @@ -112,7 +116,7 @@ def test_probe_insertion_rest(self):
self.assertTrue(len(probe_ins) == 0)

# test the project filter
urlf = (reverse('probeinsertion-list') + '?&project=brain_wide')
urlf = (reverse('probeinsertion-list') + '?&project=foobar')
probe_ins = self.ar(self.client.get(urlf))
self.assertTrue(len(probe_ins) == 0)

Expand Down