From 31a0aa47415c6c3657202361b594c3fa0b171bb6 Mon Sep 17 00:00:00 2001 From: Christopher Charbonneau Wells <10456740+cdubz@users.noreply.github.com> Date: Sun, 7 May 2023 15:10:50 -0700 Subject: [PATCH] 513: Make nap user toggleable (#641) * Add `nap` field, remove `napping` field. * Default `nap` property to NULL * Update existing tests * Format the code!! * Allow `Sleep.nap` to be blank * Migrate settings from `NAP_START_MIN` AND `NAP_START_MAX` * Remove `active` class from boolean fields * Correct test method name and user config * Add forms test * Use settings for Sleep nap model tests --- api/tests.py | 12 +-- babybuddy/apps.py | 37 +++++++++ babybuddy/fixtures/tests.json | 22 ++--- babybuddy/management/commands/reset.py | 5 ++ babybuddy/settings/test.py | 5 ++ babybuddy/templates/babybuddy/form_field.html | 2 +- babybuddy/tests/tests_site_settings.py | 19 +---- babybuddy/tests/tests_templatetags.py | 4 +- core/forms.py | 17 +++- .../0015_add_nap_field_for_sleep.py | 3 +- ..._options_remove_sleep_napping_sleep_nap.py | 53 ++++++++++++ core/models.py | 25 ++---- core/tests/import/sleep.csv | 80 +++++++++---------- core/tests/tests_forms.py | 20 +++++ core/tests/tests_models.py | 38 +++++++++ dashboard/templatetags/cards.py | 6 +- dashboard/tests/tests_templatetags.py | 18 ++--- 17 files changed, 256 insertions(+), 110 deletions(-) create mode 100644 core/migrations/0028_alter_sleep_options_remove_sleep_napping_sleep_nap.py diff --git a/api/tests.py b/api/tests.py index 535d1efa..c284f78e 100644 --- a/api/tests.py +++ b/api/tests.py @@ -527,15 +527,15 @@ class SleepAPITestCase(TestBase.BabyBuddyAPITestCaseBase): def test_get(self): response = self.client.get(self.endpoint) self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual( + self.assertDictEqual( response.data["results"][0], { "id": 4, "child": 1, - "start": "2017-11-18T19:00:00-05:00", - "end": "2017-11-18T23:00:00-05:00", - "duration": "04:00:00", - "nap": False, + "start": "2017-11-19T03:00:00-05:00", + "end": "2017-11-19T04:30:00-05:00", + "duration": "01:30:00", + "nap": True, "notes": "lots of squirming", "tags": [], }, @@ -558,7 +558,7 @@ class SleepAPITestCase(TestBase.BabyBuddyAPITestCaseBase): endpoint = "{}{}/".format(self.endpoint, 4) response = self.client.get(endpoint) entry = response.data - entry["end"] = "2017-11-18T23:30:00-05:00" + entry["end"] = "2017-11-19T08:30:00-05:00" response = self.client.patch( endpoint, { diff --git a/babybuddy/apps.py b/babybuddy/apps.py index 2e9775f2..e7c533e3 100644 --- a/babybuddy/apps.py +++ b/babybuddy/apps.py @@ -1,10 +1,13 @@ # -*- coding: utf-8 -*- +import datetime import os from django.apps import AppConfig from django.conf import settings from django.db.models.signals import post_migrate +from dbsettings.loading import set_setting_value, setting_in_db + from babybuddy import VERSION @@ -14,6 +17,39 @@ def create_read_only_group(sender, **kwargs): Group.objects.get_or_create(name=settings.BABY_BUDDY["READ_ONLY_GROUP_NAME"]) +def set_default_site_settings(sender, **kwargs): + """ + Sets default values for site-wide settings. + + Based on `dbsettings.utils.set_defaults` which no longer seem to work in + the latest versions of Django. + """ + from core import models + + # Removed `NAP_START_MIN` and `NAP_START_MAX` values are referenced here + # for pre-2.0.0 migrations. + try: + nap_start_min = datetime.datetime.strptime( + os.environ.get("NAP_START_MIN"), "%H:%M" + ).time() + except (TypeError, ValueError): + nap_start_min = models.Sleep.settings.nap_start_min + try: + nap_start_max = datetime.datetime.strptime( + os.environ.get("NAP_START_MAX"), "%H:%M" + ).time() + except (TypeError, ValueError): + nap_start_max = models.Sleep.settings.nap_start_max + + defaults = ( + ("Sleep", "nap_start_min", nap_start_min), + ("Sleep", "nap_start_max", nap_start_max), + ) + for class_name, attribute_name, value in defaults: + if not setting_in_db("core.models", class_name, attribute_name): + set_setting_value("core.models", class_name, attribute_name, value) + + class BabyBuddyConfig(AppConfig): name = "babybuddy" verbose_name = "Baby Buddy" @@ -25,3 +61,4 @@ class BabyBuddyConfig(AppConfig): commit = open(".git/refs/heads/master").read() self.version_string += " ({})".format(commit[0:7]) post_migrate.connect(create_read_only_group, sender=self) + post_migrate.connect(set_default_site_settings, sender=self) diff --git a/babybuddy/fixtures/tests.json b/babybuddy/fixtures/tests.json index 711e9aa0..de8026c6 100644 --- a/babybuddy/fixtures/tests.json +++ b/babybuddy/fixtures/tests.json @@ -332,10 +332,10 @@ "fields": { "child": 1, - "start": "2017-11-18T04:30:00Z", + "start": "2017-11-17T20:30:00Z", "end": "2017-11-18T05:30:00Z", - "duration": "01:00:00", - "napping": false + "duration": "09:00:00", + "nap": false } }, { @@ -347,7 +347,7 @@ "start": "2017-11-18T15:00:00Z", "end": "2017-11-18T17:00:00Z", "duration": "02:00:00", - "napping": true + "nap": true } }, { @@ -357,9 +357,9 @@ { "child": 1, "start": "2017-11-18T19:00:00Z", - "end": "2017-11-18T19:30:00Z", - "duration": "00:30:00", - "napping": false + "end": "2017-11-19T04:30:00Z", + "duration": "09:30:00", + "nap": false } }, { @@ -368,11 +368,11 @@ "fields": { "child": 1, - "start": "2017-11-19T00:00:00Z", - "end": "2017-11-19T04:00:00Z", - "duration": "04:00:00", + "start": "2017-11-19T08:00:00Z", + "end": "2017-11-19T09:30:00Z", + "duration": "01:30:00", "notes": "lots of squirming", - "napping": false + "nap": true } }, { diff --git a/babybuddy/management/commands/reset.py b/babybuddy/management/commands/reset.py index 98ae4780..34840a75 100644 --- a/babybuddy/management/commands/reset.py +++ b/babybuddy/management/commands/reset.py @@ -8,6 +8,8 @@ from django.core.management import call_command from django.core.management.base import BaseCommand, CommandError from django.core.management.commands.flush import Command as Flush +from dbsettings.models import Setting + from .fake import Command as Fake from .migrate import Command as Migrate @@ -38,6 +40,9 @@ class Command(BaseCommand): if verbosity > 0: self.stdout.write(self.style.SUCCESS("Database flushed.")) + # Remove all site-wide settings. + Setting.objects.all().delete() + # Run migrations for all Baby Buddy apps. for config in apps.app_configs.values(): if path.split(path.split(config.path)[0])[1] == "babybuddy": diff --git a/babybuddy/settings/test.py b/babybuddy/settings/test.py index 05643c8c..14f2b59e 100644 --- a/babybuddy/settings/test.py +++ b/babybuddy/settings/test.py @@ -19,3 +19,8 @@ EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend" # See https://django-axes.readthedocs.io/en/latest/4_configuration.html AXES_ENABLED = False + +# DBSettings configuration +# See https://github.com/zlorf/django-dbsettings#a-note-about-caching + +DBSETTINGS_USE_CACHE = False diff --git a/babybuddy/templates/babybuddy/form_field.html b/babybuddy/templates/babybuddy/form_field.html index 3c4698de..9ea32c71 100644 --- a/babybuddy/templates/babybuddy/form_field.html +++ b/babybuddy/templates/babybuddy/form_field.html @@ -11,7 +11,7 @@ {% else %} {{ field|add_class:"btn-check" }} {% endif %} -