Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
123 changes: 123 additions & 0 deletions krill/sample/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<th data-sort="passage"')
self.assertContains(response, '<th data-sort="sample_date"')
self.assertNotContains(response, '<th data-sort="created_at"')
self.assertContains(response, '<td>p8</td>')
self.assertContains(response, '<td>2024-03-05</td>')

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, '<td>—</td>', 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)
Expand Down
48 changes: 41 additions & 7 deletions krill/sample/views/list.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)

Expand All @@ -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')
Expand All @@ -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':
Expand All @@ -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
Expand Down Expand Up @@ -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()

Expand Down
26 changes: 26 additions & 0 deletions krill/static/krill/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
23 changes: 18 additions & 5 deletions krill/templates/sample/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ <h1>{{ model_name }}</h1>
<option value="{{ at.id }}" {% if aliquot_type_filter == at.id|stringformat:"i" %}selected{% endif %}>{{ at.name }}</option>
{% endfor %}
</select>
<label class="toolbar-toggle">
<input type="checkbox" name="hide_finished" value="1" {% if hide_finished %}checked{% endif %}
onchange="this.form.submit()">
Hide exhausted
</label>
<button type="submit" class="toolbar-btn-search" title="Search">
<span class="material-icons-round">search</span>
</button>
Expand All @@ -46,7 +51,7 @@ <h1>{{ model_name }}</h1>
</a>
</div>
</form>
{% if search_query or disposition_filter or aliquot_type_filter %}
{% if search_query or disposition_filter or aliquot_type_filter or hide_finished %}
<div class="filter-chips">
{% if search_query %}
<span class="filter-chip">
Expand All @@ -60,6 +65,12 @@ <h1>{{ model_name }}</h1>
<button type="button" class="chip-remove" onclick="removeFilterParam('disposition')" title="Remove">×</button>
</span>
{% endif %}
{% if hide_finished %}
<span class="filter-chip">
Hiding exhausted
<button type="button" class="chip-remove" onclick="removeFilterParam('hide_finished')" title="Remove">×</button>
</span>
{% endif %}
{% if aliquot_type_filter %}
<span class="filter-chip">
Type: {% for at in aliquot_types %}{% if at.id|stringformat:"i" == aliquot_type_filter %}{{ at.name }}{% endif %}{% endfor %}
Expand Down Expand Up @@ -151,9 +162,10 @@ <h2>{{ model_name }}</h2>
<tr>
<th data-sort="sample" class="{% if sort_by == 'sample' %}sort-{{ sort_order }}{% endif %}">Aliquot</th>
<th data-sort="type" class="{% if sort_by == 'type' %}sort-{{ sort_order }}{% endif %}">Type</th>
<th data-sort="passage" class="{% if sort_by == 'passage' %}sort-{{ sort_order }}{% endif %}">Passage</th>
<th data-sort="disposition" class="{% if sort_by == 'disposition' %}sort-{{ sort_order }}{% endif %}">Disposition</th>
<th data-sort="location" class="{% if sort_by == 'location' %}sort-{{ sort_order }}{% endif %}">Location</th>
<th data-sort="created_at" class="{% if sort_by == 'created_at' %}sort-{{ sort_order }}{% endif %}">Created</th>
<th data-sort="sample_date" class="{% if sort_by == 'sample_date' %}sort-{{ sort_order }}{% endif %}">Sample Date</th>
<th></th>
</tr>
</thead>
Expand All @@ -162,6 +174,7 @@ <h2>{{ model_name }}</h2>
<tr data-url="{% url 'sample:detail' type='aliquot' pk=item.id %}">
<td><a href="{% url 'sample:detail' type='aliquot' pk=item.id %}">{{ item.sample.name }} <span style="color:var(--color-dark-variant);font-weight:400">#{{ item.id }}</span></a></td>
<td>{% if item.aliquot_type %}<span class="status-badge">{{ item.aliquot_type.name }}</span>{% else %}—{% endif %}</td>
<td>{{ item.passage|default:"—" }}</td>
<td>{% if item.disposition %}<span class="status-badge {{ item.disposition.css_class }}">{{ item.disposition.name }}</span>{% else %}—{% endif %}</td>
<td>
{% if item.location %}
Expand All @@ -173,16 +186,16 @@ <h2>{{ model_name }}</h2>
</span>
{% else %}—{% endif %}
</td>
<td>{{ item.created_at|date:"Y-m-d" }}</td>
<td>{{ item.sample_date|date:"Y-m-d"|default:"—" }}</td>
<td>
<a class="action-button view" href="{% url 'sample:detail' type='aliquot' pk=item.id %}" target="_blank" title="Open in new tab">
<span class="material-icons-round">open_in_new</span>
</a>
</td>
</tr>
{% empty %}
<tr><td colspan="6" style="text-align:center;padding:2rem;color:var(--color-dark-variant)">
{% if search_query or disposition_filter or aliquot_type_filter %}
<tr><td colspan="7" style="text-align:center;padding:2rem;color:var(--color-dark-variant)">
{% 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. <a href="{% url 'sample:sample_create' %}?type=aliquot">Create your first aliquot</a> to get started.
Expand Down
Loading