Skip to content

Commit

Permalink
Add option for hiding empty dashboard cards (#213)
Browse files Browse the repository at this point in the history
* add option for hiding empty dashboard cards

* rework add option for hiding empty dashboard cards

missed statistics.html

* don't exit early in cards

* add forms test for dashboard_hide_empty

* add tests for cards

* fix early exit in card_diaperchange_latest

* change dependency of migration

* rename migration

* introduce hiding of cards in templates

* linting

* add context to test_card_diaperchange_last

* setup MockUserRequest

* add context to all cards test cases

* add test for settings_dashboard_hide_empty_on

* change dashboard_hide_test, but it doesn't work

* add test for _user_wants_hide

* fix test_user_wants_hide user object, simpliy check for data['empty']

* add test for user_wants_hide to every card

* linting

* fix trailing whitespace

* rename user_wants_hide to hide_empty

* fix hidden statistics

* add user.refresh_from_db to test case, add test case for dashboard_refresh_rate

* Follow redirect and correct assertion

Co-authored-by: jcgoette <jcgoette@gmail.com>
Co-authored-by: Benjamin Häublein <benjaminh@debian.vm.hp>
Co-authored-by: Christopher C. Wells <git@chris-wells.net>
  • Loading branch information
4 people committed May 14, 2021
1 parent fe56887 commit 1dca1cc
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 125 deletions.
2 changes: 1 addition & 1 deletion babybuddy/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SettingsInline(admin.StackedInline):
can_delete = False
fieldsets = (
(_('Dashboard'), {
'fields': ('dashboard_refresh_rate',)
'fields': ('dashboard_refresh_rate', 'dashboard_hide_empty',)
}),
)

Expand Down
7 changes: 6 additions & 1 deletion babybuddy/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ class Meta:
class UserSettingsForm(forms.ModelForm):
class Meta:
model = Settings
fields = ['dashboard_refresh_rate', 'language', 'timezone']
fields = [
'dashboard_refresh_rate',
'dashboard_hide_empty',
'language',
'timezone'
]
18 changes: 18 additions & 0 deletions babybuddy/migrations/0014_settings_hide_empty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.1.5 on 2021-01-19 23:17

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('babybuddy', '0013_auto_20210411_1241'),
]

operations = [
migrations.AddField(
model_name='settings',
name='dashboard_hide_empty',
field=models.BooleanField(default=False, verbose_name='Hide Empty Dashboard Cards'),
),
]
5 changes: 5 additions & 0 deletions babybuddy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class Settings(models.Model):
(timezone.timedelta(minutes=15), _('15 min.')),
(timezone.timedelta(minutes=30), _('30 min.')),
])
dashboard_hide_empty = models.BooleanField(
verbose_name=_('Hide Empty Dashboard Cards'),
default=False,
editable=True
)
language = models.CharField(
choices=settings.LANGUAGES,
default=settings.LANGUAGE_CODE,
Expand Down
5 changes: 5 additions & 0 deletions babybuddy/templates/babybuddy/user_settings_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ <h1>{% trans "User Settings" %}</h1>
{% include 'babybuddy/form_field.html' %}
{% endwith %}
</div>
<div class="form-group row">
{% with form_settings.dashboard_hide_empty as field %}
{% include 'babybuddy/form_field.html' %}
{% endwith %}
</div>
</fieldset>
<fieldset>
<legend>{% trans "API" %}</legend>
Expand Down
25 changes: 25 additions & 0 deletions babybuddy/tests/tests_forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# -*- coding: utf-8 -*-
import datetime

from django.contrib.auth.models import User
from django.core.management import call_command
from django.test import Client as HttpClient, override_settings, TestCase
Expand Down Expand Up @@ -132,3 +134,26 @@ def test_user_settings_timezone(self):
self.assertEqual(page.status_code, 200)
self.assertEqual(timezone.get_current_timezone_name(),
params['timezone'])

def test_user_settings_dashboard_hide_empty_on(self):
self.c.login(**self.credentials)

params = self.settings_template.copy()
params['dashboard_hide_empty'] = 'on'

page = self.c.post('/user/settings/', data=params, follow=True)
self.assertEqual(page.status_code, 200)
self.user.refresh_from_db()
self.assertTrue(self.user.settings.dashboard_hide_empty)

def test_user_settings_dashboard_refresh_rate(self):
self.c.login(**self.credentials)

params = self.settings_template.copy()
params['dashboard_refresh_rate'] = '0:05:00'

page = self.c.post('/user/settings/', data=params, follow=True)
self.assertEqual(page.status_code, 200)
self.user.refresh_from_db()
self.assertEqual(self.user.settings.dashboard_refresh_rate,
datetime.timedelta(seconds=300))
22 changes: 12 additions & 10 deletions dashboard/templates/cards/base.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<div class="card card-dashboard card-{{ type }}">
<div class="card-header">
<i class="icon icon-{{ type }} pull-left" aria-hidden="true"></i>
{% block header %}{% endblock %}
{% if not empty or not hide_empty %}
<div class="card card-dashboard card-{{ type }}">
<div class="card-header">
<i class="icon icon-{{ type }} pull-left" aria-hidden="true"></i>
{% block header %}{% endblock %}
</div>
<div class="card-body">
<span class="card-title"><strong>{% block title %}{% endblock %}</strong></span>
<div class="card-text"> {% block content %}{% endblock %} </div>
</div>
{% block listgroup %}{% endblock %}
</div>
<div class="card-body">
<span class="card-title"><strong>{% block title %}{% endblock %}</strong></span>
<div class="card-text">{% block content %}{% endblock %}</div>
</div>
{% block listgroup %}{% endblock %}
</div>
{% endif %}
72 changes: 39 additions & 33 deletions dashboard/templates/cards/statistics.html
Original file line number Diff line number Diff line change
@@ -1,40 +1,46 @@
{% load duration i18n %}

<div class="card card-dashboard card-statistics">
<div class="card-header">
<i class="icon icon-graph pull-left" aria-hidden="true"></i>
{% trans "Statistics" %}
</div>
<div class="card-body text-center">
<div id="statistics-carousel" class="carousel slide" data-interval="false">
<div class="carousel-inner">
{% for stat in stats %}
<div class="carousel-item{% if forloop.counter == 1 %} active{% endif %}">
<span class="card-title">
{% if stat.stat %}
{% if stat.type == 'duration' %}
{{ stat.stat|duration_string:'m' }}
{% elif stat.type == 'float' %}
{{ stat.stat|floatformat }}
{% if not empty or not hide_empty %}
<div class="card card-dashboard card-statistics">
<div class="card-header">
<i class="icon icon-graph pull-left" aria-hidden="true"></i>
{% trans "Statistics" %}
</div>
<div class="card-body text-center">
{% if stats|length > 0 %}
<div id="statistics-carousel" class="carousel slide" data-interval="false">
<div class="carousel-inner">
{% for stat in stats %}
<div class="carousel-item{% if forloop.counter == 1 %} active{% endif %}">
<span class="card-title">
{% if stat.stat %}
{% if stat.type == 'duration' %}
{{ stat.stat|duration_string:'m' }}
{% elif stat.type == 'float' %}
{{ stat.stat|floatformat }}
{% else %}
{{ stat.stat }}
{% endif %}
{% else %}
{{ stat.stat }}
<em>{% trans "Not enough data" %}</em>
{% endif %}
{% else %}
<em>{% trans "Not enough data" %}</em>
{% endif %}
</span>
<div class="card-text">{{ stat.title }}</div>
</div>
{% endfor %}
</span>
<div class="card-text">{{ stat.title }}</div>
</div>
{% endfor %}
</div>
<a class="carousel-control-prev" href="#statistics-carousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">{% trans "Previous" %}</span>
</a>
<a class="carousel-control-next" href="#statistics-carousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">{% trans "Next" %}</span>
</a>
</div>
<a class="carousel-control-prev" href="#statistics-carousel" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">{% trans "Previous" %}</span>
</a>
<a class="carousel-control-next" href="#statistics-carousel" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">{% trans "Next" %}</span>
</a>
{% else %}
<span class="card-title"><strong>{% trans "No data yet" %}</strong></span>
{% endif %}
</div>
</div>
</div>
{% endif %}

0 comments on commit 1dca1cc

Please sign in to comment.