Skip to content

Commit

Permalink
Merge 1f3e1ca into 5b07631
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubz committed May 3, 2023
2 parents 5b07631 + 1f3e1ca commit 215ec1f
Show file tree
Hide file tree
Showing 277 changed files with 21,825 additions and 28,714 deletions.
2 changes: 1 addition & 1 deletion .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ runs:
run: pipenv graph
- uses: actions/setup-node@v3
with:
node-version: 16
node-version: 18
6 changes: 4 additions & 2 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
name: "CodeQL"
on:
push:
branches: [ master ]
branches:
- master
pull_request:
branches: [ master ]
branches:
- master
schedule:
- cron: '33 0 * * 3'
jobs:
Expand Down
4 changes: 3 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name = "pypi"
[packages]
boto3 = "*"
dj-database-url = "*"
django = ">=4.0.6"
django = "*"
django-axes = "*"
django-filter = "*"
django-imagekit = "*"
Expand All @@ -25,6 +25,7 @@ uritemplate = "*"
whitenoise = "*"
django-taggit = "*"
django-qr-code = "*"
django-dbsettings = "*"

[dev-packages]
coveralls = "*"
Expand All @@ -33,3 +34,4 @@ mkdocs = "*"
mkdocs-material = "*"
tblib = "*"
black = "*"
pysnooper = "*"
2 changes: 1 addition & 1 deletion api/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class Meta(TimeFieldFilter.Meta):
class TimerFilter(StartEndFieldFilter):
class Meta(StartEndFieldFilter.Meta):
model = models.Timer
fields = sorted(StartEndFieldFilter.Meta.fields + ["active", "user"])
fields = sorted(StartEndFieldFilter.Meta.fields + ["user"])


class TummyTimeFilter(StartEndFieldFilter, TagsFieldFilter):
Expand Down
11 changes: 4 additions & 7 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,12 @@ def validate(self, attrs):
timer = attrs["timer"]
attrs.pop("timer")

if timer.end:
end = timer.end
else:
end = timezone.now()
if timer.child:
attrs["child"] = timer.child

# Overwrites values provided directly!
attrs["start"] = timer.start
attrs["end"] = end
attrs["end"] = timezone.now()

# The "child", "start", and "end" field should all be set at this
# point. If one is not, model validation will fail because they are
Expand All @@ -103,7 +99,7 @@ def validate(self, attrs):

# Only actually stop the timer if all validation passed.
if timer:
timer.stop(attrs["end"])
timer.stop()

return attrs

Expand Down Expand Up @@ -232,10 +228,11 @@ class TimerSerializer(CoreModelSerializer):
queryset=get_user_model().objects.all(),
required=False,
)
duration = serializers.DurationField(read_only=True, required=False)

class Meta:
model = models.Timer
fields = ("id", "child", "name", "start", "end", "duration", "active", "user")
fields = ("id", "child", "name", "start", "duration", "user")

def validate(self, attrs):
attrs = super(TimerSerializer, self).validate(attrs)
Expand Down
41 changes: 6 additions & 35 deletions api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,16 @@ def test_post_with_timer(self):
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
timer.refresh_from_db()
self.assertTrue(timer.active)
child = models.Child.objects.first()

self.timer_test_data["child"] = child.id
response = self.client.post(
self.endpoint, self.timer_test_data, format="json"
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
timer.refresh_from_db()
self.assertFalse(timer.active)
obj = self.model.objects.get(pk=response.data["id"])
self.assertEqual(obj.start, start)
self.assertEqual(obj.end, timer.end)
self.assertIsNotNone(obj.end)

def test_post_with_timer_with_child(self):
if not self.timer_test_data:
Expand All @@ -73,12 +70,10 @@ def test_post_with_timer_with_child(self):
self.endpoint, self.timer_test_data, format="json"
)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
timer.refresh_from_db()
self.assertFalse(timer.active)
obj = self.model.objects.get(pk=response.data["id"])
self.assertEqual(obj.child, timer.child)
self.assertIsNotNone(obj.child)
self.assertEqual(obj.start, start)
self.assertEqual(obj.end, timer.end)
self.assertIsNotNone(obj.end)


class BMIAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
Expand Down Expand Up @@ -703,19 +698,7 @@ class TimerAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
def test_get(self):
response = self.client.get(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(
response.data["results"][0],
{
"id": 1,
"child": None,
"name": "Fake timer",
"start": "2017-11-17T23:30:00-05:00",
"end": "2017-11-18T00:30:00-05:00",
"duration": "01:00:00",
"active": False,
"user": 1,
},
)
self.assertEqual(response.data["results"][0]["id"], 1)

def test_post(self):
data = {"name": "New fake timer", "user": 1}
Expand Down Expand Up @@ -743,31 +726,19 @@ def test_patch(self):
},
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, entry)
self.assertEqual(response.data["name"], entry["name"])

def test_start_stop_timer(self):
def test_start_restart_timer(self):
endpoint = "{}{}/".format(self.endpoint, 1)
response = self.client.get(endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertFalse(response.data["active"])

response = self.client.patch(f"{endpoint}restart/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue(response.data["active"])

# Restart twice is allowed
response = self.client.patch(f"{endpoint}restart/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertTrue(response.data["active"])

response = self.client.patch(f"{endpoint}stop/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertFalse(response.data["active"])

# Stopping twice is allowed, too
response = self.client.patch(f"{endpoint}stop/")
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertFalse(response.data["active"])


class TummyTimeAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
Expand Down
6 changes: 0 additions & 6 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,6 @@ class TimerViewSet(viewsets.ModelViewSet):
ordering_fields = ("duration", "end", "start")
ordering = "-start"

@action(detail=True, methods=["patch"])
def stop(self, request, pk=None):
timer = self.get_object()
timer.stop()
return Response(self.serializer_class(timer).data)

@action(detail=True, methods=["patch"])
def restart(self, request, pk=None):
timer = self.get_object()
Expand Down
3 changes: 0 additions & 3 deletions babybuddy/fixtures/tests.json
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,6 @@
{
"name": "Fake timer",
"start": "2017-11-18T04:30:00Z",
"end": "2017-11-18T05:30:00Z",
"duration": "01:00:00",
"active": false,
"user": 1
}
},
Expand Down
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions babybuddy/formats/en/formats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-

# Add custom "short" version of `MONTH_DAY_FORMAT`. This customization will
# only work with the locale format locale specified by this file.
SHORT_MONTH_DAY_FORMAT = "M j"
11 changes: 0 additions & 11 deletions babybuddy/formats/pt/formats.py

This file was deleted.

9 changes: 0 additions & 9 deletions babybuddy/formats/tr/formats.py

This file was deleted.

71 changes: 3 additions & 68 deletions babybuddy/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,68 +5,9 @@

from django.conf import settings
from django.utils import timezone, translation
from django.conf.locale.en import formats as formats_en_us
from django.conf.locale.en_GB import formats as formats_en_gb
from django.contrib.auth.middleware import RemoteUserMiddleware


def update_en_us_date_formats():
"""
Update the datetime formats for the en-US locale. This is handled here and
not using `FORMAT_MODULE_PATH` because the processing of format modules
does not allow us to distinguish appropriately between en-US and en-GB
based on user settings.
"""
if settings.USE_24_HOUR_TIME_FORMAT:
formats_en_us.DATETIME_FORMAT = "N j, Y, H:i:s"
custom_input_formats = [
"%m/%d/%Y %H:%M:%S", # '10/25/2006 14:30:59'
"%m/%d/%Y %H:%M", # '10/25/2006 14:30'
]
formats_en_us.SHORT_DATETIME_FORMAT = "m/d/Y G:i:s"
formats_en_us.TIME_FORMAT = "H:i:s"
else:
# These formats are added to support the locale style of Baby Buddy's
# frontend library, which uses momentjs.
custom_input_formats = [
"%m/%d/%Y %I:%M:%S %p", # '10/25/2006 2:30:59 PM'
"%m/%d/%Y %I:%M %p", # '10/25/2006 2:30 PM'
]

# Add custom "short" version of `MONTH_DAY_FORMAT`.
formats_en_us.SHORT_MONTH_DAY_FORMAT = "M j"

# Append all other input formats from the base locale.
formats_en_us.DATETIME_INPUT_FORMATS = (
custom_input_formats + formats_en_us.DATETIME_INPUT_FORMATS
)


def update_en_gb_date_formats():
if settings.USE_24_HOUR_TIME_FORMAT:
# 25 October 2006 14:30:00
formats_en_gb.DATETIME_FORMAT = "j F Y H:i:s"
custom_input_formats = [
"%d/%m/%Y %H:%M:%S", # '25/10/2006 14:30:59'
"%d/%m/%Y %H:%M", # '25/10/2006 14:30'
]
formats_en_gb.SHORT_DATETIME_FORMAT = "d/m/Y H:i"
formats_en_gb.TIME_FORMAT = "H:i"
else:
formats_en_gb.DATETIME_FORMAT = "j F Y f a" # 25 October 2006 2:30 p.m
# These formats are added to support the locale style of Baby Buddy's
# frontend library, which uses momentjs.
custom_input_formats = [
"%d/%m/%Y %I:%M:%S %p", # '25/10/2006 2:30:59 PM'
"%d/%m/%Y %I:%M %p", # '25/10/2006 2:30 PM'
]

# Append all other input formats from the base locale.
formats_en_gb.DATETIME_INPUT_FORMATS = (
custom_input_formats + formats_en_gb.DATETIME_INPUT_FORMATS
)


class UserLanguageMiddleware:
"""
Customizes settings based on user language setting.
Expand All @@ -85,11 +26,6 @@ def __call__(self, request):
language = settings.LANGUAGE_CODE

if language:
if language == "en-US":
update_en_us_date_formats()
elif language == "en-GB":
update_en_gb_date_formats()

# Set the language before generating the response.
translation.activate(language)

Expand All @@ -104,10 +40,9 @@ def __call__(self, request):

class UserTimezoneMiddleware:
"""
Sets the timezone based on a user specific setting that falls back on
`settings.TIME_ZONE`. This middleware must run after
`django.contrib.auth.middleware.AuthenticationMiddleware` because it uses
the request.user object.
Sets the timezone based on a user specific setting. This middleware must run after
`django.contrib.auth.middleware.AuthenticationMiddleware` because it uses the
request.user object.
"""

def __init__(self, get_response):
Expand Down
17 changes: 3 additions & 14 deletions babybuddy/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"storages",
"import_export",
"qr_code",
"dbsettings",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
Expand Down Expand Up @@ -78,7 +79,7 @@
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": ["babybuddy/templates/error"],
"DIRS": ["babybuddy/templates", "babybuddy/templates/error"],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
Expand Down Expand Up @@ -158,7 +159,7 @@

USE_TZ = True

TIME_ZONE = os.environ.get("TIME_ZONE") or "UTC"
TIME_ZONE = "UTC"

# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/
Expand Down Expand Up @@ -200,16 +201,6 @@

FORMAT_MODULE_PATH = ["babybuddy.formats"]

# Custom setting that can be used to override the locale-based time set by
# USE_L10N _for specific locales_ to use 24-hour format. In order for this to
# work with a given locale it must be set at the FORMAT_MODULE_PATH with
# conditionals on this setting. See babybuddy/forms/en/formats.py for an example
# implementation for the English locale.

USE_24_HOUR_TIME_FORMAT = bool(
strtobool(os.environ.get("USE_24_HOUR_TIME_FORMAT") or "False")
)


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
Expand Down Expand Up @@ -363,7 +354,5 @@

BABY_BUDDY = {
"ALLOW_UPLOADS": bool(strtobool(os.environ.get("ALLOW_UPLOADS") or "True")),
"NAP_START_MAX": os.environ.get("NAP_START_MAX") or "18:00",
"NAP_START_MIN": os.environ.get("NAP_START_MIN") or "06:00",
"READ_ONLY_GROUP_NAME": "read_only",
}

0 comments on commit 215ec1f

Please sign in to comment.