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
163 changes: 163 additions & 0 deletions krill/sample/tests/test_lineage_and_remaining.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import datetime

from django.urls import reverse

from ..models.aliquot import Aliquot, AliquotDisposition, AliquotType
from ..models.sample import Sample
from .test_views import SampleViewTest


class CreateChildAliquotTest(SampleViewTest):
def setUp(self):
super().setUp()
self.client.force_login(self.user)
self.create_url = reverse('sample:sample_create')

def test_create_form_prefills_parent_and_sample(self):
response = self.client.get(self.create_url, {'type': 'aliquot', 'parent': self.aliquot.id})
form = response.context['form']
self.assertEqual(form.initial['parent'], self.aliquot.id)
self.assertEqual(form.initial['sample'], self.sample.id)
self.assertContains(response, f'<option value="{self.aliquot.id}" selected>')

def test_create_form_ignores_bad_parent(self):
for bad in ('abc', '99999', ''):
with self.subTest(parent=bad):
response = self.client.get(self.create_url, {'type': 'aliquot', 'parent': bad})
self.assertEqual(response.status_code, 200)
self.assertNotIn('parent', response.context['form'].initial)

def test_parent_param_ignored_for_other_types(self):
response = self.client.get(self.create_url, {'type': 'source', 'parent': self.aliquot.id})
self.assertEqual(response.status_code, 200)

def test_posting_prefilled_form_creates_child_of_original(self):
response = self.client.post(
self.create_url + f'?type=aliquot&parent={self.aliquot.id}',
{
'parent': self.aliquot.id,
'sample': self.sample.id,
'aliquot_type': self.aliquot_type.id,
'disposition': self.stored_disposition.id,
'access_level': 'all_members',
'count': 1,
},
)
self.assertEqual(response.status_code, 302)
child = Aliquot.objects.get(parent=self.aliquot)
self.assertEqual(child.sample, self.sample)
self.assertIn(child, self.aliquot.children.all())

def test_both_aliquot_pages_have_create_child_button(self):
expected = f'{reverse("sample:sample_create")}?type=aliquot&amp;parent={self.aliquot.id}'
for url in (
reverse('sample:detail', kwargs={'type': 'aliquot', 'pk': self.aliquot.id}),
reverse('sample:aliquot_detail', kwargs={'pk': self.aliquot.id}),
):
with self.subTest(url=url):
response = self.client.get(url)
self.assertContains(response, 'Create child aliquot')
self.assertContains(response, expected.replace('&amp;', '&'))

def test_parent_page_lists_children_and_child_page_shows_parent(self):
child = Aliquot.objects.create(
sample=self.sample, parent=self.aliquot, disposition=self.stored_disposition,
aliquot_type=self.aliquot_type, passage='p3',
)
for url in (
reverse('sample:detail', kwargs={'type': 'aliquot', 'pk': self.aliquot.id}),
reverse('sample:aliquot_detail', kwargs={'pk': self.aliquot.id}),
):
with self.subTest(page='parent', url=url):
response = self.client.get(url)
self.assertContains(response, reverse('sample:detail', kwargs={'type': 'aliquot', 'pk': child.id}))
self.assertContains(response, 'p3')
for url in (
reverse('sample:detail', kwargs={'type': 'aliquot', 'pk': child.id}),
reverse('sample:aliquot_detail', kwargs={'pk': child.id}),
):
with self.subTest(page='child', url=url):
response = self.client.get(url)
self.assertContains(response, reverse('sample:detail', kwargs={'type': 'aliquot', 'pk': self.aliquot.id}))

def test_aliquot_without_lineage_shows_none(self):
response = self.client.get(reverse('sample:aliquot_detail', kwargs={'pk': self.aliquot.id}))
self.assertContains(response, 'Lineage')
self.assertEqual(list(response.context['children']), [])


class SampleRemainingAliquotsTest(SampleViewTest):
def setUp(self):
super().setUp()
self.client.force_login(self.user)
self.url = reverse('sample:detail', kwargs={'type': 'sample', 'pk': self.sample.id})

def _disp(self, name, kind):
return AliquotDisposition.objects.get_or_create(name=name, defaults={'disposition_type': kind})[0]

def test_lists_only_stored_aliquots_in_a_box(self):
# self.aliquot is 'stored' but has no box position, so it is not "remaining"
placed = Aliquot.objects.create(
sample=self.sample, aliquot_type=self.aliquot_type, disposition=self.stored_disposition,
passage='p8', sample_date=datetime.date(2024, 3, 5),
)
placed.store_in_location(self.box, 1, 2)
in_use = Aliquot.objects.create(sample=self.sample, disposition=self._disp('In Use', 'in_use'))
exhausted = Aliquot.objects.create(sample=self.sample, disposition=self._disp('Used', 'exhausted'))
disposed = Aliquot.objects.create(sample=self.sample, disposition=self._disp('Disposed', 'disposed'))
other_sample = Sample.objects.create(name='Other', source=self.source)
other = Aliquot.objects.create(sample=other_sample, disposition=self.stored_disposition)
other.store_in_location(self.box, 1, 3)

response = self.client.get(self.url)
self.assertEqual(response.context['remaining_aliquots'], [placed])
self.assertEqual(response.context['remaining_count'], 1)
self.assertContains(response, 'Remaining aliquots (1)')
for text in ('p8', '2024-03-05', 'Test Type', 'Test Device', 'Test Box', '(1, 2)'):
self.assertContains(response, text)
for gone in (in_use, exhausted, disposed, other, self.aliquot):
self.assertNotIn(gone, response.context['remaining_aliquots'])

def test_no_stored_aliquots_shows_zero_and_message(self):
response = self.client.get(self.url)
self.assertEqual(response.context['remaining_count'], 0)
self.assertContains(response, 'Remaining aliquots (0)')
self.assertContains(response, 'No aliquots of this sample are currently stored.')

def test_link_goes_to_aliquot_list_filtered_to_sample(self):
response = self.client.get(self.url)
self.assertContains(response, f'{reverse("sample:sample_list")}?type=aliquot&sample={self.sample.id}')

def test_other_detail_pages_have_no_remaining_section(self):
response = self.client.get(reverse('sample:detail', kwargs={'type': 'source', 'pk': self.source.id}))
self.assertNotContains(response, 'Remaining aliquots')


class AliquotListSampleFilterTest(SampleViewTest):
def setUp(self):
super().setUp()
self.client.force_login(self.user)
self.other_sample = Sample.objects.create(name='Test Sample 2', source=self.source)
self.other = Aliquot.objects.create(sample=self.other_sample, disposition=self.stored_disposition)

def _list(self, **params):
return self.client.get(reverse('sample:sample_list'), {'type': 'aliquot', **params})

def test_sample_filter_is_exact(self):
# 'Test Sample' is a substring of 'Test Sample 2'; the id filter must not mix them up
items = self._list(sample=self.sample.id).context['items']
self.assertEqual(list(items), [self.aliquot])

def test_sample_filter_shows_chip_and_survives_the_search_form(self):
response = self._list(sample=self.sample.id)
self.assertEqual(response.context['sample_filter'], self.sample)
self.assertContains(response, 'Sample: Test Sample')
self.assertContains(response, f'name="sample" value="{self.sample.id}"')

def test_bad_sample_filter_is_ignored(self):
for bad in ('abc', '99999'):
with self.subTest(sample=bad):
response = self._list(sample=bad)
self.assertEqual(response.status_code, 200)
self.assertIsNone(response.context['sample_filter'])
self.assertEqual(len(self._list(sample='abc').context['items']), 2)
12 changes: 12 additions & 0 deletions krill/sample/views/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ def get_form_class(self):
return SourceForm
else:
return SampleForm
def get_initial(self):
"""?type=aliquot&parent=<id> opens the form with the parent and its sample filled in."""
initial = super().get_initial()
if self.request.GET.get('type') == 'aliquot':
parent_id = self.request.GET.get('parent', '')
if parent_id.isdigit():
parent = Aliquot.objects.filter(pk=int(parent_id)).first()
if parent:
initial['parent'] = parent.pk
initial['sample'] = parent.sample_id
return initial

def get_success_url(self):
model_type = self.request.GET.get('type', 'sample')
return f"{reverse('sample:sample_list')}?type={model_type}"
Expand Down
23 changes: 23 additions & 0 deletions krill/sample/views/detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@
from ..forms import SampleForm, AliquotForm, AliquotTypeForm, SourceForm, AliquotMoveForm
from ..models.aliquot import AliquotDisposition

def remaining_aliquots(sample):
"""Aliquots of a sample still in the freezer: stored disposition and a box position."""
return (
Aliquot.objects.filter(
sample=sample,
disposition__disposition_type='stored',
location__isnull=False,
)
.select_related(
'aliquot_type',
'location__box__rack__shelf__device',
)
.order_by('sample_date', 'id')
)


@method_decorator(login_required, name='dispatch')
class ModelDetailView(DetailView):
template_name = 'sample/detail.html'
Expand Down Expand Up @@ -39,8 +55,13 @@ def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['model_type'] = self.kwargs.get('type', 'sample')
context['form'] = self.get_form_class()(instance=self.object)
if context['model_type'] == 'sample':
remaining = list(remaining_aliquots(self.object))
context['remaining_aliquots'] = remaining
context['remaining_count'] = len(remaining)
# Add storage location information for aliquots
if self.kwargs.get('type') == 'aliquot':
context['children'] = self.object.children.select_related('aliquot_type', 'disposition')
try:
location = self.object.location
context['storage_location'] = {
Expand Down Expand Up @@ -83,6 +104,7 @@ def get_queryset(self):
'aliquot_type',
'disposition',
'location__box__rack__shelf__device',
'parent__sample',
)

def get_context_data(self, **kwargs):
Expand All @@ -91,6 +113,7 @@ def get_context_data(self, **kwargs):
# Add forms to context for editing and moving
context['form'] = AliquotForm(instance=aliquot)
context['move_form'] = AliquotMoveForm()
context['children'] = aliquot.children.select_related('aliquot_type', 'disposition')
context['excluded_form_fields'] = ['count', 'assign_to_box', 'box', 'start_row', 'start_column']
# Get storage location if aliquot is stored
try:
Expand Down
7 changes: 7 additions & 0 deletions krill/sample/views/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def get_queryset(self):
except (ValueError, TypeError):
pass

sample_filter = self.request.GET.get('sample', '')
if sample_filter.isdigit():
queryset = queryset.filter(sample_id=int(sample_filter))

# Hide finished (exhausted) aliquots. Disposed ones stay visible.
if self.request.GET.get('hide_finished') == '1':
queryset = queryset.exclude(disposition__disposition_type='exhausted')
Expand Down Expand Up @@ -153,12 +157,15 @@ def get_context_data(self, **kwargs):
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'
sample_filter = self.request.GET.get('sample', '')
context['sample_filter'] = Sample.objects.filter(pk=int(sample_filter)).first() if sample_filter.isdigit() else None
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['sample_filter'] = None
context['disposition_choices'] = []
context['aliquot_types'] = AliquotType.objects.none()

Expand Down
39 changes: 39 additions & 0 deletions krill/static/krill/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1860,6 +1860,34 @@ main table tbody tr:last-child td{
box-shadow: var(--box-shadow);
}

.detail-table-wrap {
overflow-x: auto;
}

.detail-table {
width: 100%;
border-collapse: collapse;
}

.detail-table th {
padding: 0.6rem 1rem 0.6rem 0;
text-align: left;
color: var(--color-dark);
font-weight: 600;
border-bottom: 2px solid var(--color-light);
white-space: nowrap;
}

.detail-table td {
padding: 0.6rem 1rem 0.6rem 0;
color: var(--color-dark-variant);
border-bottom: 1px solid var(--color-light);
}

.detail-section-actions {
margin-top: 1.25rem;
}

.detail-section h3 {
display: flex;
align-items: center;
Expand Down Expand Up @@ -1934,6 +1962,17 @@ main table tbody tr:last-child td{
color: var(--color-dark-variant);
}

.detail-link {
color: var(--color-primary);
font-weight: 600;
text-decoration: underline;
text-underline-offset: 2px;
}

.detail-link:hover {
text-decoration-thickness: 2px;
}

.box-link {
color: var(--color-primary);
text-decoration: none;
Expand Down
26 changes: 26 additions & 0 deletions krill/templates/sample/_aliquot_lineage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div class="detail-section">
<h3><i class="material-icons-round">account_tree</i> Lineage</h3>
<div class="detail-field">
<label>Parent:</label>
<span>
{% if aliquot.parent %}
<a href="{% url 'sample:detail' type='aliquot' pk=aliquot.parent.id %}" class="detail-link">{{ aliquot.parent.sample.name }} #{{ aliquot.parent.id }}</a>
{% else %}None{% endif %}
</span>
</div>
<div class="detail-field">
<label>Children:</label>
<span>
{% for child in children %}
<a href="{% url 'sample:detail' type='aliquot' pk=child.id %}" class="detail-link">#{{ child.id }}</a>
({{ child.aliquot_type.name|default:"no type" }}{% if child.passage %}, {{ child.passage }}{% endif %}{% if child.disposition %}, {{ child.disposition.name }}{% endif %}){% if not forloop.last %}, {% endif %}
{% empty %}None{% endfor %}
</span>
</div>
<div class="detail-section-actions">
<a href="{% url 'sample:sample_create' %}?type=aliquot&parent={{ aliquot.id }}" class="btn btn-sm btn-success">
<span class="material-icons-round">add</span>
Create child aliquot
</a>
</div>
</div>
31 changes: 31 additions & 0 deletions krill/templates/sample/_sample_remaining_aliquots.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<div class="detail-section">
<h3><i class="material-icons-round">inventory_2</i> Remaining aliquots ({{ remaining_count }})</h3>
{% if remaining_aliquots %}
<div class="detail-table-wrap">
<table class="detail-table">
<thead>
<tr><th>Aliquot</th><th>Type</th><th>Passage</th><th>Sample Date</th><th>Location</th></tr>
</thead>
<tbody>
{% for a in remaining_aliquots %}
<tr>
<td><a href="{% url 'sample:detail' type='aliquot' pk=a.id %}" class="detail-link">#{{ a.id }}</a></td>
<td>{{ a.aliquot_type.name|default:"—" }}</td>
<td>{{ a.passage|default:"—" }}</td>
<td>{{ a.sample_date|date:"Y-m-d"|default:"—" }}</td>
<td>{{ a.location.box.rack.shelf.device.name }} › {{ a.location.box.name }} › ({{ a.location.row }}, {{ a.location.column }})</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
{% else %}
<p>No aliquots of this sample are currently stored.</p>
{% endif %}
<div class="detail-section-actions">
<a href="{% url 'sample:sample_list' %}?type=aliquot&sample={{ object.id }}" class="btn btn-sm btn-primary">
<span class="material-icons-round">list</span>
All aliquots of this sample
</a>
</div>
</div>
2 changes: 2 additions & 0 deletions krill/templates/sample/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ <h3><i class="material-icons-round">location_on</i> Storage Location</h3>
</div>
</div>
{% endif %}
{% include 'sample/_aliquot_lineage.html' with aliquot=object %}
<div class="detail-section">
<div class="detail-field">
<a href="{% url 'sample:aliquot_detail' pk=object.id %}" class="btn btn-sm btn-primary">
Expand Down Expand Up @@ -99,6 +100,7 @@ <h3><i class="material-icons-round">location_on</i> Storage Location</h3>
{{ object.get_access_level_display }}
</span>
</div>
{% include 'sample/_sample_remaining_aliquots.html' %}
{% endif %}
</div>

Expand Down
Loading
Loading