Skip to content

Commit

Permalink
add 'has_note' filter to Session list view
Browse files Browse the repository at this point in the history
  • Loading branch information
bimac committed Jan 9, 2025
1 parent b7f77f9 commit e2eea91
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions alyx/actions/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from django.conf import settings
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.db.models import Case, When
from django.db.models import Case, When, Exists, OuterRef
from django.urls import reverse
from django.utils.html import format_html
from django_admin_listfilter_dropdown.filters import RelatedDropdownFilter
Expand All @@ -15,11 +15,12 @@

from alyx.base import (BaseAdmin, DefaultListFilter, BaseInlineAdmin, get_admin_url)
from .models import (OtherAction, ProcedureType, Session, EphysSession, Surgery, VirusInjection,
WaterAdministration, WaterRestriction, Weighing, WaterType,
WaterAdministration, WaterRestriction, Weighing, WaterType, Session,
Notification, NotificationRule, Cull, CullReason, CullMethod, ImagingSession
)
from data.models import Dataset, FileRecord
from misc.admin import NoteInline
from misc.models import Note
from subjects.models import Subject
from .water_control import WaterControl
from experiments.models import ProbeInsertion, FOV
Expand Down Expand Up @@ -116,15 +117,34 @@ def lookups(self, request, model_admin):
)

def queryset(self, request, queryset):
regex_string = r'^(?:[ ]*|auto-generated session)$'
if self.value() == 'narrative':
return queryset.exclude(narrative__regex=regex_string)
return queryset.exclude(narrative__regex=r'^(?: *|auto-generated session)$')
if self.value() == 'no_narrative':
return queryset.filter(narrative__regex=regex_string)
return queryset.filter(narrative__regex=r'^(?: *|auto-generated session)$')
elif self.value is None:
return queryset.all()


class HasNoteFilter(DefaultListFilter):
title = 'note'
parameter_name = 'note'

def lookups(self, request, model_admin):
return (
(None, 'All'),
('note', 'Note'),
('no_note', 'No note'),
)

def queryset(self, request, queryset):
if self.value() == 'note':
return queryset.filter(Exists(Note.objects.filter(object_id=OuterRef('pk'))))
if self.value() == 'no_note':
return queryset.exclude(Exists(Note.objects.filter(object_id=OuterRef('pk'))))
elif self.value is None:
return queryset.all()


def _bring_to_front(ids, id):
if id in ids:
ids.remove(id)
Expand Down Expand Up @@ -518,6 +538,7 @@ class SessionAdmin(BaseActionAdmin):
('projects', RelatedDropdownFilter),
('lab', RelatedDropdownFilter),
(HasNarrativeFilter),
(HasNoteFilter),
]
search_fields = ('subject__nickname', 'lab__name', 'projects__name', 'users__username',
'task_protocol', 'pk')
Expand Down

0 comments on commit e2eea91

Please sign in to comment.