django-fitlog / fitlog / forms.py

import datetime

from django import forms
from django.forms.formsets import BaseFormSet

from fitlog.models import (
    FitLog, ExerciseType, CardioFitLogDetail, BodyWeightLog, WeightLogSet,
    WeightFitLogDetail)


class FitLogForm(forms.ModelForm):
    log_date = forms.DateField(initial=datetime.date.today,
                               help_text="YYYY-MM-DD")
    total_time = forms.TimeField(
        help_text="HH:MM:SS",
        error_messages={
            'invalid': 'Please enter Time in this format HH:MM:SS (example: '
                       '01:25:30 would be 1 hour 25 min and 30 seconds)'})
    comments = forms.CharField(widget=forms.Textarea, required=False)

    class Meta:
        model = FitLog
        exclude = ('member', 'create_date', 'last_update')

    def save(self, member):
        fit_log = super(FitLogForm, self).save(commit=False)
        fit_log.member = member
        fit_log.save()
        return fit_log


class CardioLogForm(forms.ModelForm):
    exercise_type = forms.ModelChoiceField(
        queryset=ExerciseType.cardio_objects.all(),
        empty_label=" -- Select One --")
    distance = forms.DecimalField(help_text="Miles (ie 1.0, 3.1,etc)")
    total_time = forms.TimeField(
        help_text="HH:MM:SS",
        error_messages={'invalid': 'Please enter Time in correct format '
                                   '"1:30:00" would be 1 hour, 30 minutes and '
                                   'no seconds'})
    comments = forms.CharField(widget=forms.Textarea, required=False)

    class Meta:
        model = CardioFitLogDetail
        fields = ('exercise_type', 'total_time', 'distance', 'comments')

    def save(self, fit_log):
        cardio_log = super(CardioLogForm, self).save(commit=False)
        cardio_log.fit_log = fit_log
        cardio_log.save()
        return cardio_log


class WeightLogForm(forms.ModelForm):
    exercise_type = forms.ModelChoiceField(
        queryset=ExerciseType.weight_objects.all(),
        empty_label=" -- Select One --")
    comments = forms.CharField(widget=forms.Textarea, required=False)

    class Meta:
        model = WeightFitLogDetail
        exclude = ('fit_log',)

    def save(self, fit_log):
        weight_log = super(WeightLogForm, self).save(commit=False)
        weight_log.fit_log = fit_log
        weight_log.save()
        return weight_log


class WeightLogSetForm(forms.ModelForm):
    class Meta:
        model = WeightLogSet
        exclude = ('weight_log_detail', 'sequence')

    def save(self, weight_log, sequence=None):
        weight_log_set = super(WeightLogSetForm, self).save(commit=False)
        weight_log_set.weight_log_detail = weight_log
        if sequence:
            weight_log_set.sequence = sequence
        weight_log_set.save()
        return weight_log_set


class WeightLogSetFormSet(BaseFormSet):

    def save(self, weight_log):
        count = 1
        for form in self.forms:
            if (('weight' in form.cleaned_data) and (
                    'reps' in form.cleaned_data)):
                form.save(weight_log, count)
                count = count + 1


class BodyWeightForm(forms.ModelForm):
    weight_date = forms.DateField(initial=datetime.date.today,
                                  help_text="YYYY-MM-DD")

    class Meta:
        model = BodyWeightLog
        exclude = ('member')

    def save(self, member):
        weight_log = super(BodyWeightForm, self).save(commit=False)
        weight_log.member = member
        weight_log.save()
        return weight_log
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.