Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into abog854/master
Browse files Browse the repository at this point in the history
  • Loading branch information
cdubz committed Mar 14, 2024
2 parents e94a66e + 25d3c9f commit e6d4f20
Show file tree
Hide file tree
Showing 24 changed files with 314 additions and 39 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ adding it here or reach out via GitHub Issues or Discussions or on Gitter!
- [part 1](https://lutzky.net/2021/10/03/software-parenting-1/)
- [part 2](https://lutzky.net/2021/10/05/software-parenting-2/)
- [part 3](https://lutzky.net/2021/10/10/software-parenting-3/)
- [High Level Developer Documentation (AI-Generated)](https://wiki.mutable.ai/babybuddy/babybuddy)

## Reporting Vulnerabilities

Expand Down
16 changes: 16 additions & 0 deletions babybuddy/static_src/js/babybuddy.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@ $("form").off("submit", preventDoubleSubmit);
$("form").on("submit", function () {
$(this).on("submit", preventDoubleSubmit);
});

BabyBuddy.RememberAdvancedToggle = function (ptr) {
localStorage.setItem("advancedForm", event.newState);
};

(function toggleAdvancedFields() {
window.addEventListener("load", function () {
if (localStorage.getItem("advancedForm") !== "open") {
return;
}

document.querySelectorAll(".advanced-fields").forEach(function (node) {
node.open = true;
});
});
})();
32 changes: 31 additions & 1 deletion babybuddy/static_src/scss/forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@
z-index: 1030;
}

.pill-container {
display: flex;
flex-wrap: wrap;
column-gap: 10px;
row-gap: 10px;
}

// Only grow pill buttons on when using a mobile
@include media-breakpoint-down(sm) {
.pill-container>label {
flex-grow: 1;
}
}

// Tweak padding on form field help blocks.
.help-block {
ul {
Expand All @@ -51,4 +65,20 @@
padding: 0.25rem 0.5rem;
position: relative;
}
}
}

@include media-breakpoint-down(md) {
.advanced-fields__summary {
text-align: center;
}
}

.advanced-fields__summary > * {
display: inline
}

.advanced-fields {
// removing this causes slight mis-alignment between fields in the details and the rest of the fields
//on the form at the left hand side of the box
padding-right: 0;
}
16 changes: 12 additions & 4 deletions babybuddy/templates/babybuddy/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@
<div class="container-fluid pb-5">
<form role="form" method="post" enctype="multipart/form-data">
{% csrf_token %}
{% for field in form %}
{{ field.widget }}
<div class="row">{% include 'babybuddy/form_field.html' %}</div>
{% endfor %}
{% if form.fieldsets %}
{% for fieldset in form.hydrated_fielsets %}
{% with "forms/layouts/"|add:fieldset.layout|add:".html" as template %}
{% include template %}
{% endwith %}
{% endfor %}
{% else %}
{% for field in form %}
{{ field.widget }}
<div class="row">{% include 'babybuddy/form_field.html' %}</div>
{% endfor %}
{% endif %}
<button type="submit" class="submit-primary btn btn-primary btn-lg">{% trans "Submit" %}</button>
</form>
</div>
31 changes: 1 addition & 30 deletions babybuddy/templates/babybuddy/form_field.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,6 @@
{{ field.label }}
</label>
<div class="col-sm-10{% if field|widget_type == 'childradioselect' %} overflow-auto"{% endif %}">
{% if field|field_type == "booleanfield" %}
{% if field.errors %}
{{ field|add_class:"btn-check is-invalid" }}
{% else %}
{{ field|add_class:"btn-check" }}
{% endif %}
<label for="id_{{ field.name }}" class="btn btn-outline-light btn-no-hover">{{ field.label }}</label>
{% elif 'choice' in field|field_type %}
{% if field.errors %}
{{ field|add_class:"form-select is-invalid" }}
{% else %}
{{ field|add_class:"form-select" }}
{% endif %}
{% else %}
{% if field.errors %}
{{ field|add_class:"form-control is-invalid" }}
{% else %}
{{ field|add_class:"form-control" }}
{% endif %}
{% endif %}
{% if field.help_text %}
<div class="help-block">
<small>{{ field.help_text }}</small>
</div>
{% endif %}
{% if field.errors %}
<div class="invalid-feedback">
{% for error in field.errors %}{{ error }}{% endfor %}
</div>
{% endif %}
{% include 'babybuddy/form_field_no_label.html' %}
</div>
</div>
31 changes: 31 additions & 0 deletions babybuddy/templates/babybuddy/form_field_no_label.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{% load widget_tweaks %}
{% if field|field_type == "booleanfield" %}
{% if field.errors %}
{{ field|add_class:"btn-check is-invalid" }}
{% else %}
{{ field|add_class:"btn-check" }}
{% endif %}
<label for="id_{{ field.name }}" class="btn btn-outline-light btn-no-hover">{{ field.label }}</label>
{% elif 'choice' in field|field_type %}
{% if field.errors %}
{{ field|add_class:"form-select is-invalid" }}
{% else %}
{{ field|add_class:"form-select" }}
{% endif %}
{% else %}
{% if field.errors %}
{{ field|add_class:"form-control is-invalid" }}
{% else %}
{{ field|add_class:"form-control" }}
{% endif %}
{% endif %}
{% if field.help_text %}
<div class="help-block">
<small>{{ field.help_text }}</small>
</div>
{% endif %}
{% if field.errors %}
<div class="invalid-feedback">
{% for error in field.errors %}{{ error }}{% endfor %}
</div>
{% endif %}
117 changes: 116 additions & 1 deletion core/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from babybuddy.widgets import DateInput, DateTimeInput, TimeInput
from core import models
from core.models import Timer
from core.widgets import TagsEditor, ChildRadioSelect
from core.widgets import TagsEditor, ChildRadioSelect, PillRadioSelect


def set_initial_values(kwargs, form_type):
Expand Down Expand Up @@ -107,6 +107,31 @@ def save(self, commit=True):
self.save_m2m()
return instance

@property
def hydrated_fielsets(self):
# for some reason self.fields returns defintions and not bound fields
# so until i figure out a better way we can just create a dict here
# https://github.com/django/django/blob/main/django/forms/forms.py#L52

bound_field_dict = {}
for field in self:
bound_field_dict[field.name] = field

hydrated_fieldsets = []

for fieldset in self.fieldsets:
hyrdrated_fieldset = {
"layout": fieldset["layout"],
"layout_attrs": fieldset.get("layout_attrs", {}),
"fields": [],
}
for field_name in fieldset["fields"]:
hyrdrated_fieldset["fields"].append(bound_field_dict[field_name])

hydrated_fieldsets.append(hyrdrated_fieldset)

return hydrated_fieldsets


class ChildForm(forms.ModelForm):
class Meta:
Expand Down Expand Up @@ -153,6 +178,11 @@ class TaggableModelForm(forms.ModelForm):


class PumpingForm(CoreModelForm, TaggableModelForm):
fieldsets = [
{"fields": ["child", "start", "end", "amount"], "layout": "required"},
{"layout": "advanced", "fields": ["notes", "tags"]},
]

class Meta:
model = models.Pumping
fields = ["child", "start", "end", "amount", "notes", "tags"]
Expand All @@ -165,6 +195,16 @@ class Meta:


class DiaperChangeForm(CoreModelForm, TaggableModelForm):
fieldsets = [
{
"fields": ["wet", "solid"],
"layout": "choices",
"layout_attrs": {"label": "Contents"},
},
{"fields": ["child", "time"], "layout": "required"},
{"layout": "advanced", "fields": ["notes", "tags"]},
]

class Meta:
model = models.DiaperChange
fields = ["child", "time", "wet", "solid", "color", "amount", "notes", "tags"]
Expand All @@ -176,18 +216,36 @@ class Meta:


class FeedingForm(CoreModelForm, TaggableModelForm):
fieldsets = [
{
"fields": ["child", "start", "end", "type", "method", "amount"],
"layout": "required",
},
{"layout": "advanced", "fields": ["notes", "tags"]},
]

class Meta:
model = models.Feeding
fields = ["child", "start", "end", "type", "method", "amount", "notes", "tags"]
widgets = {
"child": ChildRadioSelect,
"start": DateTimeInput(),
"end": DateTimeInput(),
"type": PillRadioSelect(),
"method": PillRadioSelect(),
"notes": forms.Textarea(attrs={"rows": 5}),
}


class BottleFeedingForm(CoreModelForm, TaggableModelForm):
fieldsets = [
{
"fields": ["child", "type", "start", "amount"],
"layout": "required",
},
{"layout": "advanced", "fields": ["notes", "tags"]},
]

def save(self):
instance = super(BottleFeedingForm, self).save(commit=False)
instance.method = "bottle"
Expand All @@ -201,6 +259,7 @@ class Meta:
widgets = {
"child": ChildRadioSelect,
"start": DateTimeInput(),
"type": PillRadioSelect(),
"notes": forms.Textarea(attrs={"rows": 5}),
}

Expand All @@ -218,6 +277,14 @@ class Meta:


class SleepForm(CoreModelForm, TaggableModelForm):
fieldsets = [
{
"fields": ["child", "start", "end", "nap"],
"layout": "required",
},
{"layout": "advanced", "fields": ["notes", "tags"]},
]

class Meta:
model = models.Sleep
fields = ["child", "start", "end", "nap", "notes", "tags"]
Expand All @@ -230,6 +297,14 @@ class Meta:


class TemperatureForm(CoreModelForm, TaggableModelForm):
fieldsets = [
{
"fields": ["child", "temperature", "time"],
"layout": "required",
},
{"layout": "advanced", "fields": ["notes", "tags"]},
]

class Meta:
model = models.Temperature
fields = ["child", "temperature", "time", "notes", "tags"]
Expand Down Expand Up @@ -261,6 +336,14 @@ def save(self, commit=True):


class TummyTimeForm(CoreModelForm, TaggableModelForm):
fieldsets = [
{
"fields": ["child", "start", "end", "milestone"],
"layout": "required",
},
{"layout": "advanced", "fields": ["tags"]},
]

class Meta:
model = models.TummyTime
fields = ["child", "start", "end", "milestone", "tags"]
Expand All @@ -272,6 +355,14 @@ class Meta:


class WeightForm(CoreModelForm, TaggableModelForm):
fieldsets = [
{
"fields": ["child", "weight", "date"],
"layout": "required",
},
{"layout": "advanced", "fields": ["notes", "tags"]},
]

class Meta:
model = models.Weight
fields = ["child", "weight", "date", "notes", "tags"]
Expand All @@ -283,6 +374,14 @@ class Meta:


class HeightForm(CoreModelForm, TaggableModelForm):
fieldsets = [
{
"fields": ["child", "height", "date"],
"layout": "required",
},
{"layout": "advanced", "fields": ["notes", "tags"]},
]

class Meta:
model = models.Height
fields = ["child", "height", "date", "notes", "tags"]
Expand All @@ -294,6 +393,14 @@ class Meta:


class HeadCircumferenceForm(CoreModelForm, TaggableModelForm):
fieldsets = [
{
"fields": ["child", "head_circumference", "date"],
"layout": "required",
},
{"layout": "advanced", "fields": ["notes", "tags"]},
]

class Meta:
model = models.HeadCircumference
fields = ["child", "head_circumference", "date", "notes", "tags"]
Expand All @@ -305,6 +412,14 @@ class Meta:


class BMIForm(CoreModelForm, TaggableModelForm):
fieldsets = [
{
"fields": ["child", "bmi", "date"],
"layout": "required",
},
{"layout": "advanced", "fields": ["notes", "tags"]},
]

class Meta:
model = models.BMI
fields = ["child", "bmi", "date", "notes", "tags"]
Expand Down
11 changes: 11 additions & 0 deletions core/templates/core/pill_radio.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% with id=widget.attrs.id %}
<div {% if id %}id="{{ id }}"{% endif %} class="pill-container">
{% for group, options, index in widget.optgroups %}
{% for option in options %}
{% if option.value != '' %}
{% include option.template_name with widget=option %}
{% endif %}
{% endfor %}
{% endfor %}
</div>
{% endwith %}
3 changes: 3 additions & 0 deletions core/templates/core/pill_radio_option.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% include "django/forms/widgets/input.html" %}
<label {% if widget.attrs.id %}for="{{ widget.attrs.id }}"{% endif %}
class="btn btn-outline-light btn-no-hover">{{ widget.label }}</label>

0 comments on commit e6d4f20

Please sign in to comment.