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
56 changes: 56 additions & 0 deletions krill/reports/test_sidebar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import re

from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse

User = get_user_model()

REPORT_PAGES = {
'Overview': 'reports:reports_dashboard',
'Storage Audits': 'reports:audit_list',
'Alerts': 'reports:alert_list',
'Checked Out, No Owner': 'reports:checked_out_no_owner',
}


class ReportsSidebarSectionTest(TestCase):
def setUp(self):
self.user = User.objects.create_user(username='member', password='x')
self.client.force_login(self.user)

def _item_classes(self, html, url):
match = re.search(r'<a href="%s" class="(submenu-item[^"]*)"' % re.escape(url), html)
self.assertIsNotNone(match, f'sidebar has no submenu item for {url}')
return match.group(1)

def test_every_report_page_is_reachable_from_the_sidebar_on_any_page(self):
for page in ('home', 'sample:sample_list', 'settings'):
html = self.client.get(reverse(page)).content.decode()
section = html[html.index('id="reports-section"'):]
for label, name in REPORT_PAGES.items():
with self.subTest(page=page, item=label):
self.assertIn(f'href="{reverse(name)}"', section)
self.assertIn(f'<h3>{label}</h3>', section)

def test_section_is_collapsed_away_from_reports_and_open_inside_it(self):
away = self.client.get(reverse('home')).content.decode()
self.assertIn('id="reports-section" class="sidebar-submenu "', away)
inside = self.client.get(reverse('reports:alert_list')).content.decode()
self.assertIn('id="reports-section" class="sidebar-submenu show"', inside)

def test_the_current_page_is_the_active_item(self):
for label, name in REPORT_PAGES.items():
with self.subTest(label=label):
html = self.client.get(reverse(name)).content.decode()
for other_label, other_name in REPORT_PAGES.items():
classes = self._item_classes(html, reverse(other_name))
if other_name == name:
self.assertIn('active', classes.split(), f'{other_label} should be active on {label}')
else:
self.assertNotIn('active', classes.split(), f'{other_label} should not be active on {label}')

def test_audit_detail_pages_keep_storage_audits_active(self):
url = reverse('reports:generate_audit_report')
html = self.client.get(url).content.decode()
self.assertIn('active', self._item_classes(html, reverse('reports:audit_list')).split())
93 changes: 93 additions & 0 deletions krill/sample/tests/test_dashboard_sections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from django.contrib.auth import get_user_model
from django.urls import reverse
from django.utils import timezone

from person.models import SiteConfiguration
from ..models.aliquot import Aliquot, AliquotDisposition
from .test_views import SampleViewTest

User = get_user_model()


class DashboardSectionsTest(SampleViewTest):
"""The dashboard groups its tiles under Samples, Storage and Consumables, like the sidebar."""

def setUp(self):
super().setUp()
self.client.force_login(self.user)
self.in_use = AliquotDisposition.objects.get_or_create(
name='In Use', defaults={'disposition_type': 'in_use'})[0]

def _html(self):
return self.client.get(reverse('home')).content.decode()

def _consumables(self, enabled):
config = SiteConfiguration.load()
config.consumables_enabled = enabled
config.save()

def _owned(self):
return Aliquot.objects.create(
sample=self.sample, disposition=self.in_use,
checked_out_by=self.user, checked_out_at=timezone.now())

def test_groups_appear_in_order_with_recent_activity_last(self):
self._consumables(True)
html = self._html()
positions = [html.index(f'class="dashboard-group-label">{name}<') for name in ('Samples', 'Storage', 'Consumables')]
self.assertEqual(positions, sorted(positions))
self.assertGreater(html.index('<h2>Recent Activity</h2>'), positions[-1])

def test_sample_tiles_and_checked_out_card_sit_in_the_samples_group(self):
self._owned()
html = self._html()
start, end = html.index('>Samples<'), html.index('class="dashboard-group-label">Storage<')
group = html[start:end]
for expected in ('<h3>Samples</h3>', '<h3>Aliquots</h3>', 'Checked out by you (1)'):
self.assertIn(expected, group)
for elsewhere in ('<h3>Storage Used</h3>', '<h3>Audits</h3>', '<h2>Freezers</h2>'):
self.assertNotIn(elsewhere, group)

def test_storage_tiles_and_freezers_sit_in_the_storage_group(self):
self._consumables(True)
html = self._html()
start = html.index('class="dashboard-group-label">Storage<')
end = html.index('class="dashboard-group-label">Consumables<')
group = html[start:end]
for expected in ('<h3>Storage Used</h3>', '<h3>Audits</h3>', '<h2>Freezers</h2>'):
self.assertIn(expected, group)

def test_alerts_tile_is_kept_and_sits_after_audits_in_the_storage_group(self):
self._consumables(True)
html = self._html()
group = html[html.index('class="dashboard-group-label">Storage<'):html.index('class="dashboard-group-label">Consumables<')]
self.assertRegex(group, r'<div class="stat-info">\s*<h3>Alerts</h3>')
self.assertLess(group.index('<h3>Audits</h3>'), group.index('<h3>Alerts</h3>'))
self.assertIn(reverse('reports:alert_list'), group)

def test_consumable_tiles_sit_in_the_consumables_group_when_enabled(self):
self._consumables(True)
html = self._html()
group = html[html.index('class="dashboard-group-label">Consumables<'):html.index('<h2>Recent Activity</h2>')]
self.assertIn('<h3>Low Stock</h3>', group)
self.assertIn('<h3>Expiring Soon</h3>', group)

def test_no_consumables_group_when_the_feature_is_off(self):
self._consumables(False)
html = self._html()
self.assertNotIn('Consumables<', html.replace('class="dashboard-group-label">Samples<', ''))
self.assertNotIn('<h3>Low Stock</h3>', html)
self.assertNotIn('<h3>Expiring Soon</h3>', html)

def test_quick_actions_come_before_the_groups(self):
html = self._html()
self.assertLess(html.index('class="quick-actions"'), html.index('class="dashboard-group-label">Samples<'))

def test_manager_tile_is_in_the_samples_group(self):
manager = User.objects.create_user(username='mgr', password='x')
manager.role.role = 'lab_manager'
manager.role.save()
self.client.force_login(manager)
html = self._html()
group = html[html.index('>Samples<'):html.index('class="dashboard-group-label">Storage<')]
self.assertIn('<h3>Lab Checkouts</h3>', group)
34 changes: 28 additions & 6 deletions krill/templates/base/left_sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,34 @@ <h3>Reorder List</h3>
</div>
{% endif %}

<a href="{% url 'reports:reports_dashboard' %}" class="{% if 'reports' in request.path %}active{% endif %}">
<span class="material-icons-round">
description
</span>
<h3>Reports</h3>
</a>
<!-- Reports section -->
<div class="sidebar-section">
<a href="{% url 'reports:reports_dashboard' %}" class="sidebar-toggle {% if '/reports/' in request.path %}active{% endif %}">
<span class="material-icons-round">
description
</span>
<h3>Reports</h3>
<span class="material-icons-round expand-icon">expand_more</span>
</a>
<div id="reports-section" class="sidebar-submenu {% if '/reports/' in request.path %}show{% endif %}">
<a href="{% url 'reports:reports_dashboard' %}" class="submenu-item {% if request.path == '/reports/' %}active{% endif %}">
<span class="material-icons-round">insights</span>
<h3>Overview</h3>
</a>
<a href="{% url 'reports:audit_list' %}" class="submenu-item {% if '/reports/audit' in request.path %}active{% endif %}">
<span class="material-icons-round">fact_check</span>
<h3>Storage Audits</h3>
</a>
<a href="{% url 'reports:alert_list' %}" class="submenu-item {% if '/reports/alerts' in request.path %}active{% endif %}">
<span class="material-icons-round">notifications</span>
<h3>Alerts</h3>
</a>
<a href="{% url 'reports:checked_out_no_owner' %}" class="submenu-item {% if '/reports/checked-out-no-owner' in request.path %}active{% endif %}">
<span class="material-icons-round">person_off</span>
<h3>Checked Out, No Owner</h3>
</a>
</div>
</div>
<a href="{% url 'settings' %}" class="{% if 'settings' in request.path %}active{% endif %}">
<span class="material-icons-round">
settings
Expand Down
125 changes: 68 additions & 57 deletions krill/templates/krill/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,26 @@
<h1>Dashboard</h1>
</div>

<!-- Quick Stats: every card leads somewhere useful -->
<!-- Quick Actions -->
<div class="quick-actions">
<div class="action-grid">
<a href="{% url 'sample:sample_create' %}?type=aliquot" class="action-btn">
<span class="material-icons-round">add_circle</span>
New Aliquot
</a>
<a href="{% url 'sample:sample_list' %}?type=aliquot" class="action-btn">
<span class="material-icons-round">search</span>
Find Aliquot
</a>
<a href="{% url 'reports:generate_audit_report' %}" class="action-btn">
<span class="material-icons-round">fact_check</span>
Start Audit
</a>
</div>
</div>

<!-- Samples -->
<h2 class="dashboard-group-label">Samples</h2>
<div class="stats-container">
<a href="{% url 'sample:sample_list' %}" class="stat-card clickable">
<span class="material-icons-round">science</span>
Expand All @@ -23,43 +42,6 @@ <h3>Aliquots</h3>
<p>{{ stats.aliquots|default:"0" }}</p>
</div>
</a>
<a href="{% url 'storage:storage_list' %}" class="stat-card clickable {% if stats.storage_usage > 90 %}stat-card-warning{% endif %}">
<span class="material-icons-round">inventory_2</span>
<div class="stat-info">
<h3>Storage Used</h3>
<p>{{ stats.storage_usage|default:"0" }}%</p>
</div>
</a>
<a href="{% url 'reports:audit_list' %}" class="stat-card clickable {% if audits_in_progress %}stat-card-warning{% endif %}">
<span class="material-icons-round">fact_check</span>
<div class="stat-info">
<h3>Audits</h3>
<p>{% if audits_in_progress %}{{ audits_in_progress|length }} in progress{% else %}None in progress{% endif %}</p>
</div>
</a>
<a href="{% url 'reports:alert_list' %}" class="stat-card clickable {% if stats.alerts %}stat-card-warning{% endif %}">
<span class="material-icons-round">warning</span>
<div class="stat-info">
<h3>Alerts</h3>
<p>{% if stats.alerts %}{{ stats.alerts }} active{% else %}All clear{% endif %}</p>
</div>
</a>
{% if site_config.consumables_enabled %}
<a href="{% url 'consumables:list' %}?type=consumable&low_stock=1" class="stat-card clickable {% if stats.low_stock_count %}stat-card-warning{% endif %}">
<span class="material-icons-round">production_quantity_limits</span>
<div class="stat-info">
<h3>Low Stock</h3>
<p>{% if stats.low_stock_count %}{{ stats.low_stock_count }} item{{ stats.low_stock_count|pluralize }}{% else %}All stocked{% endif %}</p>
</div>
</a>
<a href="{% url 'consumables:list' %}?type=consumable&expiring=1" class="stat-card clickable {% if stats.expiring_soon_count %}stat-card-warning{% endif %}">
<span class="material-icons-round">event_busy</span>
<div class="stat-info">
<h3>Expiring Soon</h3>
<p>{% if stats.expiring_soon_count %}{{ stats.expiring_soon_count }} item{{ stats.expiring_soon_count|pluralize }}{% else %}None in 30 days{% endif %}</p>
</div>
</a>
{% endif %}
{% if lab_checkouts is not None %}
<a href="{% url 'sample:sample_list' %}?type=aliquot&checked_out_by=all" class="stat-card clickable">
<span class="material-icons-round">groups</span>
Expand All @@ -70,25 +52,6 @@ <h3>Lab Checkouts</h3>
</a>
{% endif %}
</div>

<!-- Quick Actions -->
<div class="quick-actions">
<div class="action-grid">
<a href="{% url 'sample:sample_create' %}?type=aliquot" class="action-btn">
<span class="material-icons-round">add_circle</span>
New Aliquot
</a>
<a href="{% url 'sample:sample_list' %}?type=aliquot" class="action-btn">
<span class="material-icons-round">search</span>
Find Aliquot
</a>
<a href="{% url 'reports:generate_audit_report' %}" class="action-btn">
<span class="material-icons-round">fact_check</span>
Start Audit
</a>
</div>
</div>

<!-- Checked out by you -->
{% url 'home' as home_url %}
{% if checked_out.total %}
Expand Down Expand Up @@ -127,6 +90,31 @@ <h2>Checked out by you ({{ checked_out.total }})</h2>
<p class="checked-out-empty">Nothing checked out. <a href="{% url 'sample:sample_list' %}?type=aliquot" class="detail-link">Find an aliquot</a> to check one out.</p>
{% endif %}

<!-- Storage -->
<h2 class="dashboard-group-label">Storage</h2>
<div class="stats-container">
<a href="{% url 'storage:storage_list' %}" class="stat-card clickable {% if stats.storage_usage > 90 %}stat-card-warning{% endif %}">
<span class="material-icons-round">inventory_2</span>
<div class="stat-info">
<h3>Storage Used</h3>
<p>{{ stats.storage_usage|default:"0" }}%</p>
</div>
</a>
<a href="{% url 'reports:audit_list' %}" class="stat-card clickable {% if audits_in_progress %}stat-card-warning{% endif %}">
<span class="material-icons-round">fact_check</span>
<div class="stat-info">
<h3>Audits</h3>
<p>{% if audits_in_progress %}{{ audits_in_progress|length }} in progress{% else %}None in progress{% endif %}</p>
</div>
</a>
<a href="{% url 'reports:alert_list' %}" class="stat-card clickable {% if stats.alerts %}stat-card-warning{% endif %}">
<span class="material-icons-round">warning</span>
<div class="stat-info">
<h3>Alerts</h3>
<p>{% if stats.alerts %}{{ stats.alerts }} active{% else %}All clear{% endif %}</p>
</div>
</a>
</div>
<!-- Freezers -->
<div class="recent-activity">
<div class="section-header">
Expand Down Expand Up @@ -180,6 +168,27 @@ <h2>In progress</h2>
</div>
{% endif %}

<!-- Consumables (only when the feature is on) -->
{% if site_config.consumables_enabled %}
<h2 class="dashboard-group-label">Consumables</h2>
<div class="stats-container">
<a href="{% url 'consumables:list' %}?type=consumable&low_stock=1" class="stat-card clickable {% if stats.low_stock_count %}stat-card-warning{% endif %}">
<span class="material-icons-round">production_quantity_limits</span>
<div class="stat-info">
<h3>Low Stock</h3>
<p>{% if stats.low_stock_count %}{{ stats.low_stock_count }} item{{ stats.low_stock_count|pluralize }}{% else %}All stocked{% endif %}</p>
</div>
</a>
<a href="{% url 'consumables:list' %}?type=consumable&expiring=1" class="stat-card clickable {% if stats.expiring_soon_count %}stat-card-warning{% endif %}">
<span class="material-icons-round">event_busy</span>
<div class="stat-info">
<h3>Expiring Soon</h3>
<p>{% if stats.expiring_soon_count %}{{ stats.expiring_soon_count }} item{{ stats.expiring_soon_count|pluralize }}{% else %}None in 30 days{% endif %}</p>
</div>
</a>
</div>
{% endif %}

<!-- Recent Activity -->
<div class="recent-activity">
<div class="section-header">
Expand Down Expand Up @@ -218,6 +227,8 @@ <h2>Recent Activity</h2>
.checked-out-card { margin-top: 16px; }
.checked-out-empty { margin: 8px 0 16px; color: var(--color-dark-variant); }
.dashboard-header { margin-bottom: 20px; }
.dashboard-group-label { margin: 24px 0 10px; font-size: 0.75rem; font-weight: 600; letter-spacing: 0.08em;
text-transform: uppercase; color: var(--color-dark-variant); }
.freezer-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.freezer-card { display: flex; flex-direction: column; gap: 10px; padding: 16px; text-decoration: none;
background: var(--color-white); color: var(--color-dark); border: 1px solid var(--color-light);
Expand Down
Loading