67 lines
2.7 KiB
Python
67 lines
2.7 KiB
Python
from django import forms
|
||
from django.utils import timezone
|
||
|
||
from .models import MomentumEntry
|
||
|
||
|
||
class MomentumEntryForm(forms.ModelForm):
|
||
class Meta:
|
||
model = MomentumEntry
|
||
fields = [
|
||
"title",
|
||
"category",
|
||
"entry_date",
|
||
"focus_score",
|
||
"energy_score",
|
||
"deep_work_minutes",
|
||
"takeaway",
|
||
"reflection",
|
||
]
|
||
widgets = {
|
||
"title": forms.TextInput(
|
||
attrs={"placeholder": "Shipped a habit, finished a lesson, or stayed focused"}
|
||
),
|
||
"entry_date": forms.DateInput(attrs={"type": "date"}),
|
||
"focus_score": forms.NumberInput(attrs={"min": 1, "max": 10}),
|
||
"energy_score": forms.NumberInput(attrs={"min": 1, "max": 10}),
|
||
"deep_work_minutes": forms.NumberInput(attrs={"min": 0, "max": 960, "step": 5}),
|
||
"takeaway": forms.TextInput(
|
||
attrs={"placeholder": "One sentence that captures today’s momentum"}
|
||
),
|
||
"reflection": forms.Textarea(
|
||
attrs={"rows": 4, "placeholder": "What worked, what felt hard, what should tomorrow look like?"}
|
||
),
|
||
}
|
||
labels = {
|
||
"title": "What did you move forward?",
|
||
"category": "Category",
|
||
"entry_date": "Date",
|
||
"focus_score": "Focus score",
|
||
"energy_score": "Energy score",
|
||
"deep_work_minutes": "Deep-work minutes",
|
||
"takeaway": "Main takeaway",
|
||
"reflection": "Reflection",
|
||
}
|
||
help_texts = {
|
||
"focus_score": "1 = distracted, 10 = locked in.",
|
||
"energy_score": "1 = drained, 10 = fully charged.",
|
||
"deep_work_minutes": "Minutes spent on meaningful work today.",
|
||
}
|
||
|
||
def __init__(self, *args, **kwargs):
|
||
super().__init__(*args, **kwargs)
|
||
self.fields["entry_date"].initial = timezone.localdate()
|
||
for name, field in self.fields.items():
|
||
css_class = "form-select" if isinstance(field.widget, forms.Select) else "form-control"
|
||
if name in {"focus_score", "energy_score", "deep_work_minutes", "entry_date"}:
|
||
css_class = "form-control"
|
||
field.widget.attrs["class"] = f"{field.widget.attrs.get('class', '')} {css_class}".strip()
|
||
field.widget.attrs.setdefault("autocomplete", "off")
|
||
self.fields["reflection"].required = False
|
||
|
||
def clean_takeaway(self):
|
||
takeaway = self.cleaned_data["takeaway"].strip()
|
||
if len(takeaway.split()) < 3:
|
||
raise forms.ValidationError("Write a short sentence with at least three words.")
|
||
return takeaway
|