diff --git a/krill/sample/tests/test_views.py b/krill/sample/tests/test_views.py index b489293..58501d8 100644 --- a/krill/sample/tests/test_views.py +++ b/krill/sample/tests/test_views.py @@ -184,6 +184,129 @@ def test_aliquot_list_filters_by_aliquot_type(self): self.assertIn(self.aliquot, items) self.assertNotIn(other_aliquot, items) + # --- Aliquot list: passage, sample date, search, hide exhausted --- + + def _aliquot_list(self, **params): + self.client.force_login(self.user) + return self.client.get(reverse('sample:sample_list'), {'type': 'aliquot', **params}) + + def _disposition(self, name, disposition_type): + return AliquotDisposition.objects.get_or_create( + name=name, defaults={'disposition_type': disposition_type} + )[0] + + def test_aliquot_list_shows_passage_and_sample_date_not_created(self): + import datetime + self.aliquot.passage = 'p8' + self.aliquot.sample_date = datetime.date(2024, 3, 5) + self.aliquot.save() + response = self._aliquot_list() + self.assertContains(response, 'p8') + self.assertContains(response, '2024-03-05') + + def test_aliquot_list_blank_sample_date_and_passage_show_dash(self): + response = self._aliquot_list() + # Passage and sample date cells (the location dash sits on its own line) + self.assertContains(response, '—', count=2) + + def test_aliquot_list_sorts_by_passage(self): + self.aliquot.passage = 'p9' + self.aliquot.save() + other = Aliquot.objects.create(sample=self.sample, disposition=self.stored_disposition, passage='p10') + asc = list(self._aliquot_list(sort='passage', order='asc').context['items']) + desc = list(self._aliquot_list(sort='passage', order='desc').context['items']) + # Passage is text, so this is alphabetical ("p10" before "p9") + self.assertEqual(asc, [other, self.aliquot]) + self.assertEqual(desc, [self.aliquot, other]) + + def test_aliquot_list_sorts_by_sample_date_with_blanks_last(self): + import datetime + self.aliquot.sample_date = None + self.aliquot.save() + early = Aliquot.objects.create(sample=self.sample, disposition=self.stored_disposition, + sample_date=datetime.date(2020, 1, 1)) + late = Aliquot.objects.create(sample=self.sample, disposition=self.stored_disposition, + sample_date=datetime.date(2024, 1, 1)) + asc = list(self._aliquot_list(sort='sample_date', order='asc').context['items']) + desc = list(self._aliquot_list(sort='sample_date', order='desc').context['items']) + self.assertEqual(asc, [early, late, self.aliquot]) + self.assertEqual(desc, [late, early, self.aliquot]) + + def test_aliquot_search_matches_passage(self): + self.aliquot.passage = 'p8' + self.aliquot.save() + other = Aliquot.objects.create(sample=self.sample, disposition=self.stored_disposition, passage='p12') + items = self._aliquot_list(q='p8').context['items'] + self.assertIn(self.aliquot, items) + self.assertNotIn(other, items) + + def test_aliquot_search_matches_every_listed_field(self): + import datetime + self.sample.experiment = 'Zebrafish-exp' + self.sample.notes = 'quokka-note' + self.sample.save() + member = User.objects.create_user(username='mlovelace', password='x', first_name='Ada', last_name='Lovelace') + target = Aliquot.objects.create( + sample=Sample.objects.create(name='Target Line', source=self.source), + aliquot_type=AliquotType.objects.create(name='Pellet'), + passage='p77', lab_member=member, lab_member_initials='XYZ', + sample_date=datetime.date(2023, 11, 14), + ) + target.sample.experiment = 'Zebrafish-exp' + target.sample.notes = 'quokka-note' + target.sample.save() + target.store_in_location(self.box, 2, 3) # resets disposition to 'Stored' + target.disposition = self._disposition('Frozen Down', 'stored') + target.save() + self.device.name = 'Freezer-Omega' + self.device.save() + self.box.name = 'Box-Kappa' + self.box.save() + + terms = [ + 'Target Line', 'Pellet', 'Frozen Down', 'p77', 'XYZ', 'Ada', 'Lovelace', 'mlovelace', + '2023-11', 'Zebrafish', 'quokka', 'Freezer-Omega', 'Box-Kappa', str(target.id), f'#{target.id}', + ] + for term in terms: + with self.subTest(term=term): + items = self._aliquot_list(q=term).context['items'] + self.assertIn(target, items) + # Non-matching term finds nothing + self.assertEqual(len(self._aliquot_list(q='no-such-thing').context['items']), 0) + + def test_aliquot_search_number_is_exact_not_substring(self): + """'1' must not match aliquot 12 through the number path (text fields may still match).""" + a = Aliquot.objects.create(sample=self.sample, disposition=self.stored_disposition) + items = self._aliquot_list(q=f'#{a.id}').context['items'] + self.assertEqual(list(items), [a]) + + def test_hide_finished_hides_exhausted_only(self): + exhausted = Aliquot.objects.create(sample=self.sample, disposition=self._disposition('Used', 'exhausted')) + disposed = Aliquot.objects.create(sample=self.sample, disposition=self._disposition('Disposed', 'disposed')) + in_use = Aliquot.objects.create(sample=self.sample, disposition=self._disposition('In Use', 'in_use')) + no_disposition = Aliquot.objects.create(sample=self.sample) + items = self._aliquot_list(hide_finished='1').context['items'] + self.assertNotIn(exhausted, items) + for visible in (self.aliquot, disposed, in_use, no_disposition): + self.assertIn(visible, items) + + def test_hide_finished_is_off_by_default(self): + exhausted = Aliquot.objects.create(sample=self.sample, disposition=self._disposition('Used', 'exhausted')) + response = self._aliquot_list() + self.assertIn(exhausted, response.context['items']) + self.assertFalse(response.context['hide_finished']) + + def test_hide_finished_works_with_search_and_sort(self): + exhausted = Aliquot.objects.create(sample=self.sample, disposition=self._disposition('Used', 'exhausted'), passage='p5') + self.aliquot.passage = 'p5' + self.aliquot.save() + response = self._aliquot_list(hide_finished='1', q='p5', sort='passage') + self.assertEqual(list(response.context['items']), [self.aliquot]) + self.assertContains(response, 'name="hide_finished" value="1" checked') + def test_aliquot_list_context_includes_filter_choices(self): """Context includes disposition_choices, aliquot_types, and active filter values.""" self.client.force_login(self.user) diff --git a/krill/sample/views/list.py b/krill/sample/views/list.py index d644f66..cb612d5 100644 --- a/krill/sample/views/list.py +++ b/krill/sample/views/list.py @@ -1,6 +1,7 @@ from django.views.generic import ListView from django.contrib.auth.mixins import LoginRequiredMixin -from django.db.models import Q +from django.db.models import CharField, F, Q +from django.db.models.functions import Cast from ..models.sample import Sample from ..models.aliquot import Aliquot, AliquotType, AliquotDisposition, AliquotLocation from ..models.source import Source @@ -30,10 +31,7 @@ def get_queryset(self): search_query = self.request.GET.get('q', '') if search_query: if model_type == 'aliquot': - queryset = queryset.filter( - Q(sample__name__icontains=search_query) | - Q(aliquot_type__name__icontains=search_query) - ) + queryset = self._search_aliquots(queryset, search_query) elif model_type in ('sample', 'source', 'aliquot-type'): queryset = queryset.filter(name__icontains=search_query) @@ -51,6 +49,10 @@ def get_queryset(self): except (ValueError, TypeError): pass + # Hide finished (exhausted) aliquots. Disposed ones stay visible. + if self.request.GET.get('hide_finished') == '1': + queryset = queryset.exclude(disposition__disposition_type='exhausted') + # Sorting sort_by = self.request.GET.get('sort', 'name') sort_order = self.request.GET.get('order', 'asc') @@ -66,8 +68,12 @@ def get_queryset(self): queryset = queryset.order_by(f'{sort_prefix}aliquot_type__name', 'id') elif model_type == 'aliquot' and sort_by == 'sample': queryset = queryset.order_by(f'{sort_prefix}sample__name', 'id') - elif model_type == 'aliquot' and sort_by == 'created_at': - queryset = queryset.order_by(f'{sort_prefix}created_at', 'id') + elif model_type == 'aliquot' and sort_by == 'sample_date': + # Blank dates sort last in both directions + date_order = F('sample_date').desc(nulls_last=True) if sort_order == 'desc' else F('sample_date').asc(nulls_last=True) + queryset = queryset.order_by(date_order, 'id') + elif model_type == 'aliquot' and sort_by == 'passage': + queryset = queryset.order_by(f'{sort_prefix}passage', 'id') elif model_type == 'aliquot' and sort_by == 'location': queryset = queryset.order_by(f'{sort_prefix}location__box__rack__shelf__device__name', 'id') elif model_type == 'sample' and sort_by == 'source': @@ -88,6 +94,32 @@ def get_queryset(self): return queryset + @staticmethod + def _search_aliquots(queryset, term): + """Match the term against any aliquot field shown or recorded in Krill.""" + term = term.strip() + queryset = queryset.annotate(sample_date_text=Cast('sample_date', CharField())) + match = ( + Q(sample__name__icontains=term) | + Q(sample__experiment__icontains=term) | + Q(sample__notes__icontains=term) | + Q(aliquot_type__name__icontains=term) | + Q(disposition__name__icontains=term) | + Q(passage__icontains=term) | + Q(lab_member_initials__icontains=term) | + Q(lab_member__first_name__icontains=term) | + Q(lab_member__last_name__icontains=term) | + Q(lab_member__username__icontains=term) | + Q(sample_date_text__icontains=term) | + Q(location__box__name__icontains=term) | + Q(location__box__rack__shelf__device__name__icontains=term) + ) + # Aliquot number: "12" or "#12" matches aliquot 12 exactly + number = term.lstrip('#') + if number.isdigit(): + match |= Q(id=int(number)) + return queryset.filter(match) + def get_paginate_by(self, queryset): if self.request.GET.get('page_size') == '20': return 20 @@ -120,11 +152,13 @@ def get_context_data(self, **kwargs): if model_type == 'aliquot': context['disposition_filter'] = self.request.GET.get('disposition', '') context['aliquot_type_filter'] = self.request.GET.get('aliquot_type', '') + context['hide_finished'] = self.request.GET.get('hide_finished') == '1' context['disposition_choices'] = AliquotDisposition.DISPOSITION_CHOICES context['aliquot_types'] = AliquotType.objects.order_by('name') else: context['disposition_filter'] = '' context['aliquot_type_filter'] = '' + context['hide_finished'] = False context['disposition_choices'] = [] context['aliquot_types'] = AliquotType.objects.none() diff --git a/krill/static/krill/style.css b/krill/static/krill/style.css index 14497ea..97675d5 100644 --- a/krill/static/krill/style.css +++ b/krill/static/krill/style.css @@ -2590,6 +2590,32 @@ main table tbody tr:last-child td{ outline-offset: 1px; } +.toolbar-toggle { + display: inline-flex; + align-items: center; + gap: 0.4rem; + padding: 0.45rem 0.6rem; + border: 1px solid var(--color-gray-border); + border-radius: var(--border-radius-1); + background: var(--color-white); + color: var(--color-dark); + font-size: 0.85rem; + font-family: var(--font-family); + line-height: normal; + white-space: nowrap; + cursor: pointer; +} + +.toolbar-toggle input[type="checkbox"] { + width: auto; + margin: 0; + cursor: pointer; +} + +.toolbar-toggle:has(input:checked) { + border-color: var(--color-primary); +} + .toolbar-btn-search { display: flex; align-items: center; diff --git a/krill/templates/sample/list.html b/krill/templates/sample/list.html index baaa60b..d0e72da 100644 --- a/krill/templates/sample/list.html +++ b/krill/templates/sample/list.html @@ -37,6 +37,11 @@

{{ model_name }}

{% endfor %} + @@ -46,7 +51,7 @@

{{ model_name }}

- {% if search_query or disposition_filter or aliquot_type_filter %} + {% if search_query or disposition_filter or aliquot_type_filter or hide_finished %}
{% if search_query %} @@ -60,6 +65,12 @@

{{ model_name }}

{% endif %} + {% if hide_finished %} + + Hiding exhausted + + + {% endif %} {% if aliquot_type_filter %} Type: {% for at in aliquot_types %}{% if at.id|stringformat:"i" == aliquot_type_filter %}{{ at.name }}{% endif %}{% endfor %} @@ -151,9 +162,10 @@

{{ model_name }}

Aliquot Type + Passage Disposition Location - Created + Sample Date @@ -162,6 +174,7 @@

{{ model_name }}

{{ item.sample.name }} #{{ item.id }} {% if item.aliquot_type %}{{ item.aliquot_type.name }}{% else %}—{% endif %} + {{ item.passage|default:"—" }} {% if item.disposition %}{{ item.disposition.name }}{% else %}—{% endif %} {% if item.location %} @@ -173,7 +186,7 @@

{{ model_name }}

{% else %}—{% endif %} - {{ item.created_at|date:"Y-m-d" }} + {{ item.sample_date|date:"Y-m-d"|default:"—" }} open_in_new @@ -181,8 +194,8 @@

{{ model_name }}

{% empty %} - -{% if search_query or disposition_filter or aliquot_type_filter %} + +{% if search_query or disposition_filter or aliquot_type_filter or hide_finished %} No aliquots match your filters — try clearing a filter above. {% else %} No aliquots yet.
Create your first aliquot to get started.