39687-vm/core/forms.py
2026-04-16 11:25:44 +00:00

105 lines
4.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django import forms
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth.models import User
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 todays 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
class LoginForm(AuthenticationForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["username"].help_text = "Use the username you chose when creating your account."
self.fields["password"].help_text = "Enter the same password you used during sign up."
for field in self.fields.values():
field.widget.attrs["class"] = f"{field.widget.attrs.get('class', '')} form-control".strip()
field.widget.attrs.setdefault("autocomplete", "off")
class SignUpForm(UserCreationForm):
email = forms.EmailField(required=False)
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["username"].help_text = "Pick a simple username for your private dashboard."
self.fields["email"].help_text = "Optional, but useful if you later add notifications."
self.fields["password1"].help_text = "Use at least 8 characters so your account is harder to guess."
self.fields["password2"].help_text = "Type the same password again to confirm it."
for name, field in self.fields.items():
field.widget.attrs["class"] = f"{field.widget.attrs.get('class', '')} form-control".strip()
if name == "username":
field.widget.attrs.setdefault("autofocus", True)
field.widget.attrs.setdefault("autocomplete", "off")
def save(self, commit=True):
user = super().save(commit=False)
user.email = self.cleaned_data.get("email", "")
if commit:
user.save()
return user