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
24 changes: 24 additions & 0 deletions krill/consumables/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from .models.consumable_type import ConsumableType
from .models.location import ConsumableRoom, ConsumableLocation
from .models.vendor import Vendor
from sample.widgets import mark_comboboxes
from sample.widgets import mark_comboboxes


class ConsumableForm(forms.ModelForm):
Expand Down Expand Up @@ -37,6 +39,12 @@ def __init__(self, *args, **kwargs):
self.fields['access_level'].required = False
self.fields['vendor'].required = False
self.fields['location'].required = False
# A location is named "Room / Name"; load the room with it instead of one query per option
self.fields['location'].queryset = ConsumableLocation.objects.select_related('room')
mark_comboboxes(self)
# A location is named "Room / Name"; load the room with it instead of one query per option
self.fields['location'].queryset = ConsumableLocation.objects.select_related('room')
mark_comboboxes(self)

ctype = self._resolve_type(args, kwargs)
self.spec_field_names = []
Expand Down Expand Up @@ -124,6 +132,14 @@ class Meta:
'icon': 'Material icon name (e.g. biotech, science)',
}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
mark_comboboxes(self)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
mark_comboboxes(self)


class VendorForm(forms.ModelForm):
class Meta:
Expand Down Expand Up @@ -164,3 +180,11 @@ class Meta:
'name': 'Location name (e.g. Cabinet B, Shelf 3)',
'kind': 'Type of storage location',
}

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
mark_comboboxes(self)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
mark_comboboxes(self)
31 changes: 31 additions & 0 deletions krill/sample/templatetags/sample_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,34 @@ def is_samples_list_active(context):
if 'detail/sample/' in path:
return True
return False


_IRREGULAR_SINGULARS = {'shelves': 'shelf'}


@register.filter
def singular(value):
"""'Boxes' -> 'Box', 'Shelves' -> 'Shelf', 'Aliquot Types' -> 'Aliquot Type', 'Categories' -> 'Category'.

Only the last word changes. Chopping the final letter turns 'Boxes' into 'Boxe', so use this instead.
"""
text = str(value or '').strip()
if not text:
return ''
head, _, word = text.rpartition(' ')
lower = word.lower()
if lower in _IRREGULAR_SINGULARS:
result = _IRREGULAR_SINGULARS[lower]
elif lower.endswith('ies') and len(lower) > 3:
result = lower[:-3] + 'y'
elif lower.endswith(('xes', 'ches', 'shes', 'sses', 'zes')):
result = lower[:-2]
elif lower.endswith('s') and not lower.endswith(('ss', 'us', 'is')):
result = lower[:-1]
else:
result = lower
if word.isupper() and len(word) > 1:
result = result.upper()
elif word[:1].isupper():
result = result.capitalize()
return f'{head} {result}' if head else result
7 changes: 7 additions & 0 deletions krill/sample/tests/test_combobox.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,10 @@ def test_the_layout_loads_the_script_and_the_file_exists(self):
def test_the_box_clearing_script_notifies_the_combobox(self):
html = self.client.get(reverse('sample:sample_create'), {'type': 'aliquot'}).content.decode()
self.assertIn("field.dispatchEvent(new Event('change'", html)

def test_the_text_box_takes_the_selects_classes_and_modals_do_not_clip_the_list(self):
source = open(finders.find('js/combobox.js')).read()
self.assertIn('var originalClasses = select.className;', source)
self.assertIn("input.className = ('combobox-input ' + originalClasses).trim();", source)
css = open(finders.find('style.css') or finders.find('krill/style.css')).read()
self.assertIn('.modal-body:has(.combobox)', css)
3 changes: 2 additions & 1 deletion krill/static/krill/js/combobox.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
var searchUrl = select.getAttribute('data-search-url');
if (!searchUrl && select.options.length - 1 < MIN_OPTIONS) { return; }
select.dataset.comboboxReady = '1';
var originalClasses = select.className; // the text box takes the select's look (for example form-control)

var id = 'combobox-' + (++counter);
var wrapper = document.createElement('div');
Expand All @@ -66,7 +67,7 @@
var input = document.createElement('input');
input.type = 'text';
input.id = id + '-input';
input.className = 'combobox-input';
input.className = ('combobox-input ' + originalClasses).trim();
input.autocomplete = 'off';
input.spellcheck = false;
input.disabled = select.disabled;
Expand Down
6 changes: 6 additions & 0 deletions krill/static/krill/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2677,6 +2677,12 @@ main table tbody tr:last-child td{
position: relative;
}

/* A modal body scrolls and would clip the open list, so let it show in full */
.modal-body:has(.combobox) {
max-height: none;
overflow: visible;
}

/* Keep the real <select> for form submission and validation, but out of sight */
.combobox select.combobox-native {
position: absolute;
Expand Down
22 changes: 22 additions & 0 deletions krill/storage/forms.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from django import forms
from .models.storage import Device, Shelf, Rack, Box
from .models.site import Site
from sample.widgets import mark_comboboxes
from sample.widgets import mark_comboboxes

class SiteForm(forms.ModelForm):
class Meta:
Expand Down Expand Up @@ -33,6 +35,8 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Make access_level optional by not requiring it
self.fields['access_level'].required = False
mark_comboboxes(self)
mark_comboboxes(self)

class ShelfForm(forms.ModelForm):
class Meta:
Expand All @@ -52,6 +56,8 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Make access_level optional by not requiring it
self.fields['access_level'].required = False
mark_comboboxes(self)
mark_comboboxes(self)

class RackForm(forms.ModelForm):
class Meta:
Expand All @@ -71,6 +77,14 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Make access_level optional by not requiring it
self.fields['access_level'].required = False
# "Device › Shelf": shelves in different devices can share a name
self.fields['shelf'].queryset = Shelf.objects.select_related('device')
self.fields['shelf'].label_from_instance = lambda shelf: f'{shelf.device.name} › {shelf.name}'
mark_comboboxes(self)
# "Device › Shelf": shelves in different devices can share a name
self.fields['shelf'].queryset = Shelf.objects.select_related('device')
self.fields['shelf'].label_from_instance = lambda shelf: f'{shelf.device.name} › {shelf.name}'
mark_comboboxes(self)

class BoxForm(forms.ModelForm):
class Meta:
Expand All @@ -92,3 +106,11 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Make access_level optional by not requiring it
self.fields['access_level'].required = False
# "Device › Shelf › Rack": racks in different shelves can share a name
self.fields['rack'].queryset = Rack.objects.select_related('shelf__device')
self.fields['rack'].label_from_instance = lambda rack: f'{rack.shelf.device.name} › {rack.shelf.name} › {rack.name}'
mark_comboboxes(self)
# "Device › Shelf › Rack": racks in different shelves can share a name
self.fields['rack'].queryset = Rack.objects.select_related('shelf__device')
self.fields['rack'].label_from_instance = lambda rack: f'{rack.shelf.device.name} › {rack.shelf.name} › {rack.name}'
mark_comboboxes(self)
Loading
Loading