Self hosted FLOSS fitness/workout, nutrition and weight tracker
Use HTML5 input types, this is specially important for mobile users
Roland Geidere473b68d6ac1
diff --git a/wger/manager/forms.py b/wger/manager/forms.pyindex f616eea49..8fd522009 100644--- a/wger/manager/forms.py+++ b/wger/manager/forms.py@@ -43,6 +43,8 @@ from wger.manager.models import WorkoutLogfrom wger.utils.widgets import TranslatedSelectMultiplefrom wger.utils.widgets import ExerciseAjaxSelectfrom wger.utils.constants import DATE_FORMATS+from wger.utils.widgets import Html5DateInput+from wger.utils.widgets import Html5NumberInputclass UserPreferencesForm(ModelForm):@@ -158,7 +160,7 @@ class HelperDateForm(Form):'''A helper form with only a date input'''- date = DateField(input_formats=DATE_FORMATS)+ date = DateField(input_formats=DATE_FORMATS, widget=Html5DateInput())class WorkoutLogForm(ModelForm):@@ -170,7 +172,8 @@ class WorkoutLogForm(ModelForm):'''weight = DecimalField(decimal_places=2,max_digits=5,- localize=True)+ localize=True,+ widget=Html5NumberInput())class Meta:model = WorkoutLogdiff --git a/wger/manager/models.py b/wger/manager/models.pyindex 9d8ba62ee..a23a44ebd 100644--- a/wger/manager/models.py+++ b/wger/manager/models.py@@ -40,6 +40,10 @@ from wger.utils.helpers import disable_for_loaddatafrom wger.utils.cache import delete_template_fragment_cachefrom wger.utils.cache import cache_mapperfrom wger.weight.models import WeightEntry+from wger.utils.fields import Html5DateField+from wger.utils.fields import Html5TimeField+from wger.utils.fields import Html5DecimalField+from wger.utils.fields import Html5IntegerFieldlogger = logging.getLogger('wger.custom')@@ -163,8 +167,8 @@ class Schedule(models.Model):"For example 'Program XYZ'."))'''Name or short description of the schedule.'''- start_date = models.DateField(verbose_name=_('Start date'),- default=datetime.date.today)+ start_date = Html5DateField(verbose_name=_('Start date'),+ default=datetime.date.today)'''The start date of this schedule'''is_active = models.BooleanField(verbose_name=_('Schedule active'),@@ -244,10 +248,10 @@ class ScheduleStep(models.Model):workout = models.ForeignKey(Workout)'''The workout this step manages'''- duration = models.IntegerField(max_length=1,- verbose_name=_('Duration'),- help_text=_('The duration in weeks'),- default=4)+ duration = Html5IntegerField(max_length=1,+ verbose_name=_('Duration'),+ help_text=_('The duration in weeks'),+ default=4)'''The duration in weeks'''order = models.IntegerField(verbose_name=_('Order'),@@ -360,14 +364,14 @@ class Set(models.Model):verbose_name=_('Exercise day'),editable=False)exercises = models.ManyToManyField(Exercise, verbose_name=_('Exercises'))- order = models.IntegerField(max_length=1,- blank=True,- null=True,- verbose_name=_('Order'),- editable=False)- sets = models.IntegerField(validators=[MaxValueValidator(MAX_SETS)],- verbose_name=_('Number of sets'),- default=DEFAULT_SETS)+ order = Html5IntegerField(max_length=1,+ blank=True,+ null=True,+ verbose_name=_('Order'),+ editable=False)+ sets = Html5IntegerField(validators=[MaxValueValidator(MAX_SETS)],+ verbose_name=_('Number of sets'),+ default=DEFAULT_SETS)# Metaclass to set some other propertiesclass Meta:@@ -411,8 +415,8 @@ class Setting(models.Model):set = models.ForeignKey(Set, verbose_name=_('Sets'), editable=False)exercise = models.ForeignKey(Exercise, verbose_name=_('Exercises'), editable=False)- reps = models.IntegerField(validators=[MaxValueValidator(100)], verbose_name=_('Repetitions'))- order = models.IntegerField(blank=True, verbose_name=_('Order'), editable=False)+ reps = Html5IntegerField(validators=[MaxValueValidator(100)], verbose_name=_('Repetitions'))+ order = Html5IntegerField(blank=True, verbose_name=_('Order'), editable=False)comment = models.CharField(max_length=100, blank=True, verbose_name=_('Comment'))# Metaclass to set some other properties@@ -461,11 +465,11 @@ class WorkoutLog(models.Model):verbose_name=_('Workout'),editable=False)- reps = models.IntegerField(verbose_name=_('Repetitions'))- weight = models.DecimalField(decimal_places=2,- max_digits=5,- verbose_name=_('Weight'))- date = models.DateField(verbose_name=_('Date'))+ reps = Html5IntegerField(verbose_name=_('Repetitions'))+ weight = Html5DecimalField(decimal_places=2,+ max_digits=5,+ verbose_name=_('Weight'))+ date = Html5DateField(verbose_name=_('Date'))# Metaclass to set some other propertiesclass Meta:@@ -585,17 +589,17 @@ by the US Department of Agriculture. It is extremely complete, with around## User statistics#- age = models.IntegerField(max_length=2,- verbose_name=_('Age'),- blank=False,- null=True)+ age = Html5IntegerField(max_length=2,+ verbose_name=_('Age'),+ blank=False,+ null=True)'''The user's age'''- height = models.IntegerField(max_length=2,- verbose_name=_('Height (cm)'),- blank=False,- validators=[MinValueValidator(140), MaxValueValidator(230)],- null=True)+ height = Html5IntegerField(max_length=2,+ verbose_name=_('Height (cm)'),+ blank=False,+ validators=[MinValueValidator(140), MaxValueValidator(230)],+ null=True)'''The user's height'''gender = models.CharField(max_length=1,@@ -605,18 +609,18 @@ by the US Department of Agriculture. It is extremely complete, with aroundnull=True)'''Gender'''- sleep_hours = models.IntegerField(verbose_name=_('Hours of sleep'),- help_text=_('The average hours of sleep per day'),- default=7,- blank=False,- null=True)+ sleep_hours = Html5IntegerField(verbose_name=_('Hours of sleep'),+ help_text=_('The average hours of sleep per day'),+ default=7,+ blank=False,+ null=True)'''The average hours of sleep per day'''- work_hours = models.IntegerField(verbose_name=_('Work'),- help_text=_('Average hours per day'),- default=8,- blank=False,- null=True)+ work_hours = Html5IntegerField(verbose_name=_('Work'),+ help_text=_('Average hours per day'),+ default=8,+ blank=False,+ null=True)'''The average hours at work per day'''work_intensity = models.CharField(verbose_name=_('Physical intensity'),@@ -628,11 +632,11 @@ by the US Department of Agriculture. It is extremely complete, with aroundnull=True)'''Physical intensity of work'''- sport_hours = models.IntegerField(verbose_name=_('Sport'),- help_text=_('Average hours per week'),- default=3,- blank=False,- null=True)+ sport_hours = Html5IntegerField(verbose_name=_('Sport'),+ help_text=_('Average hours per week'),+ default=3,+ blank=False,+ null=True)'''The average hours performing sports per week'''sport_intensity = models.CharField(verbose_name=_('Physical intensity'),@@ -644,11 +648,11 @@ by the US Department of Agriculture. It is extremely complete, with aroundnull=True)'''Physical intensity of sport activities'''- freetime_hours = models.IntegerField(verbose_name=_('Free time'),- help_text=_('Average hours per day'),- default=8,- blank=False,- null=True)+ freetime_hours = Html5IntegerField(verbose_name=_('Free time'),+ help_text=_('Average hours per day'),+ default=8,+ blank=False,+ null=True)'''The average hours of free time per day'''freetime_intensity = models.CharField(verbose_name=_('Physical intensity'),@@ -660,11 +664,11 @@ by the US Department of Agriculture. It is extremely complete, with aroundnull=True)'''Physical intensity during free time'''- calories = models.IntegerField(verbose_name=_('Total daily calories'),- help_text=_('Total caloric intake, including e.g. any surplus'),- default=2500,- blank=False,- null=True)+ calories = Html5IntegerField(verbose_name=_('Total daily calories'),+ help_text=_('Total caloric intake, including e.g. any surplus'),+ default=2500,+ blank=False,+ null=True)'''Basic caloric intake based on physical activity'''@propertydiff --git a/wger/manager/templates/mobile/template.html b/wger/manager/templates/mobile/template.htmlindex 0335fdbf4..d6d375b06 100644--- a/wger/manager/templates/mobile/template.html+++ b/wger/manager/templates/mobile/template.html@@ -38,6 +38,11 @@<script src="{% static 'js/jquery-mobile.js' %}" type="text/javascript"></script><script src="{% static 'js/d3.js' %}"></script><script src="{% static 'js/workout-manager.js' %}" type="text/javascript"></script>+ <script>+ function init_weight_datepicker()+ {+ }+ </script>{% block header %}{% endblock %}</head>diff --git a/wger/nutrition/forms.py b/wger/nutrition/forms.pyindex e87a2dc86..498ae33d6 100644--- a/wger/nutrition/forms.py+++ b/wger/nutrition/forms.py@@ -21,6 +21,7 @@ from django.utils.translation import ugettext as _from wger.nutrition.models import IngredientWeightUnitfrom wger.manager.models import UserProfile+from wger.utils.widgets import Html5NumberInputlogger = logging.getLogger('wger.custom')@@ -54,7 +55,7 @@ class UnitChooserForm(forms.Form):class BmiForm(forms.ModelForm):- weight = forms.DecimalField()+ weight = forms.DecimalField(widget=Html5NumberInput())class Meta:model = UserProfile@@ -65,7 +66,7 @@ class BmrForm(forms.ModelForm):'''Form for the basal metabolic rate'''- weight = forms.DecimalField()+ weight = forms.DecimalField(widget=Html5NumberInput())class Meta:model = UserProfile@@ -95,13 +96,15 @@ class DailyCaloriesForm(forms.ModelForm):base_calories = forms.IntegerField(label=_('Basic caloric intake'),help_text=_('Your basic caloric intake as calculated for ''your data'),- required=False)+ required=False,+ widget=Html5NumberInput())additional_calories = forms.IntegerField(label=_('Additional calories'),help_text=_('Additional calories to add to the base ''rate (to substract, enter a negative ''number)'),initial=0,- required=False)+ required=False,+ widget=Html5NumberInput())class Meta:model = UserProfilediff --git a/wger/nutrition/models.py b/wger/nutrition/models.pyindex 2639d1aa0..75d26f9e7 100644--- a/wger/nutrition/models.py+++ b/wger/nutrition/models.py@@ -32,6 +32,9 @@ from django.utils.translation import ugettextfrom wger.exercises.models import Languagefrom wger.utils.constants import EMAIL_FROMfrom wger.utils.cache import cache_mapper+from wger.utils.fields import Html5TimeField+from wger.utils.fields import Html5DecimalField+from wger.utils.fields import Html5IntegerFieldMEALITEM_WEIGHT_GRAM = '1'MEALITEM_WEIGHT_UNIT = '2'@@ -199,51 +202,51 @@ class Ingredient(models.Model):name = models.CharField(max_length=200,verbose_name=_('Name'),)- energy = models.IntegerField(verbose_name=_('Energy'),- help_text=_('In kcal per 100g'))-- protein = models.DecimalField(decimal_places=3,- max_digits=6,- verbose_name=_('Protein'),- help_text=_('In g per 100g of product'))-- carbohydrates = models.DecimalField(decimal_places=3,- max_digits=6,- verbose_name=_('Carbohydrates'),- help_text=_('In g per 100g of product'))-- carbohydrates_sugar = models.DecimalField(decimal_places=3,- max_digits=6,- blank=True,- null=True,- verbose_name=_('Sugar content in carbohydrates'),- help_text=_('In g per 100g of product'))-- fat = models.DecimalField(decimal_places=3,- max_digits=6,- verbose_name=_('Fat'),- help_text=_('In g per 100g of product'))-- fat_saturated = models.DecimalField(decimal_places=3,- max_digits=6,- blank=True,- null=True,- verbose_name=_('Saturated fat content in fats'),- help_text=_('In g per 100g of product'))-- fibres = models.DecimalField(decimal_places=3,- max_digits=6,- blank=True,- null=True,- verbose_name=_('Fibres'),- help_text=_('In g per 100g of product'))-- sodium = models.DecimalField(decimal_places=3,- max_digits=6,- blank=True,- null=True,- verbose_name=_('Sodium'),- help_text=_('In g per 100g of product'))+ energy = Html5IntegerField(verbose_name=_('Energy'),+ help_text=_('In kcal per 100g'))++ protein = Html5DecimalField(decimal_places=3,+ max_digits=6,+ verbose_name=_('Protein'),+ help_text=_('In g per 100g of product'))++ carbohydrates = Html5DecimalField(decimal_places=3,+ max_digits=6,+ verbose_name=_('Carbohydrates'),+ help_text=_('In g per 100g of product'))++ carbohydrates_sugar = Html5DecimalField(decimal_places=3,+ max_digits=6,+ blank=True,+ null=True,+ verbose_name=_('Sugar content in carbohydrates'),+ help_text=_('In g per 100g of product'))++ fat = Html5DecimalField(decimal_places=3,+ max_digits=6,+ verbose_name=_('Fat'),+ help_text=_('In g per 100g of product'))++ fat_saturated = Html5DecimalField(decimal_places=3,+ max_digits=6,+ blank=True,+ null=True,+ verbose_name=_('Saturated fat content in fats'),+ help_text=_('In g per 100g of product'))++ fibres = Html5DecimalField(decimal_places=3,+ max_digits=6,+ blank=True,+ null=True,+ verbose_name=_('Fibres'),+ help_text=_('In g per 100g of product'))++ sodium = Html5DecimalField(decimal_places=3,+ max_digits=6,+ blank=True,+ null=True,+ verbose_name=_('Sodium'),+ help_text=_('In g per 100g of product'))## Django methods@@ -438,7 +441,9 @@ class Meal(models.Model):max_length=1,blank=True,editable=False)- time = models.TimeField(null=True, blank=True, verbose_name=_('Time (approx)'))+ time = Html5TimeField(null=True,+ blank=True,+ verbose_name=_('Time (approx)'))def __unicode__(self):'''@@ -501,10 +506,10 @@ class MealItem(models.Model):max_length=1,blank=True,editable=False)- amount = models.DecimalField(decimal_places=2,- max_digits=6,- validators=[MaxValueValidator(1000)],- verbose_name=_('Amount'))+ amount = Html5DecimalField(decimal_places=2,+ max_digits=6,+ validators=[MaxValueValidator(1000)],+ verbose_name=_('Amount'))def __unicode__(self):'''diff --git a/wger/nutrition/views/meal.py b/wger/nutrition/views/meal.pyindex fc0513b8e..46ccd2d6b 100644--- a/wger/nutrition/views/meal.py+++ b/wger/nutrition/views/meal.py@@ -30,7 +30,6 @@ from wger.nutrition.models import Mealfrom wger.utils.generic_views import WgerFormMixin-logger = logging.getLogger('wger.custom')@@ -38,7 +37,6 @@ logger = logging.getLogger('wger.custom')# Meal functions# ************************-class MealCreateView(WgerFormMixin, CreateView):'''Generic view to add a new meal to a nutrition plandiff --git a/wger/utils/fields.py b/wger/utils/fields.pynew file mode 100644index 000000000..b92139dcf--- /dev/null+++ b/wger/utils/fields.py@@ -0,0 +1,95 @@+# This file is part of wger Workout Manager.+#+# wger Workout Manager is free software: you can redistribute it and/or modify+# it under the terms of the GNU Affero General Public License as published by+# the Free Software Foundation, either version 3 of the License, or+# (at your option) any later version.+#+# wger Workout Manager is distributed in the hope that it will be useful,+# but WITHOUT ANY WARRANTY; without even the implied warranty of+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+# GNU General Public License for more details.+#+# You should have received a copy of the GNU Affero General Public License++import logging++from django.db import models++from wger.utils.widgets import Html5FormDateField+from wger.utils.widgets import Html5FormTimeField+from wger.utils.widgets import Html5FormDecimalField+from wger.utils.widgets import Html5FormIntegerField+from wger.utils.widgets import Html5FormFloatField++logger = logging.getLogger('wger.custom')+++class Html5TimeField(models.TimeField):+ '''+ Custom Time field that uses the Html5TimeInput widget+ '''++ def formfield(self, **kwargs):+ '''+ Use our custom field+ '''+ defaults = {'form_class': Html5FormTimeField}+ defaults.update(kwargs)+ return super(Html5TimeField, self).formfield(**defaults)+++class Html5DateField(models.DateField):+ '''+ Custom Time field that uses the Html5DateInput widget+ '''++ def formfield(self, **kwargs):+ '''+ Use our custom field+ '''+ defaults = {'form_class': Html5FormDateField}+ defaults.update(kwargs)+ return super(Html5DateField, self).formfield(**defaults)+++class Html5DecimalField(models.DecimalField):+ '''+ Custom decimal field that uses the Html5NumberInput widget+ '''++ def formfield(self, **kwargs):+ '''+ Use our custom field+ '''+ defaults = {'form_class': Html5FormDecimalField}+ defaults.update(kwargs)+ return super(Html5DecimalField, self).formfield(**defaults)+++class Html5FloatField(models.FloatField):+ '''+ Custom decimal field that uses the Html5NumberInput widget+ '''++ def formfield(self, **kwargs):+ '''+ Use our custom field+ '''+ defaults = {'form_class': Html5FormFloatField}+ defaults.update(kwargs)+ return super(Html5FloatField, self).formfield(**defaults)+++class Html5IntegerField(models.IntegerField):+ '''+ Custom integer field that uses the Html5NumberInput widget+ '''++ def formfield(self, **kwargs):+ '''+ Use our custom field+ '''+ defaults = {'form_class': Html5FormIntegerField}+ defaults.update(kwargs)+ return super(Html5IntegerField, self).formfield(**defaults)diff --git a/wger/utils/widgets.py b/wger/utils/widgets.pyindex 8f4c6d024..7e3032985 100644--- a/wger/utils/widgets.py+++ b/wger/utils/widgets.py@@ -14,10 +14,15 @@## You should have received a copy of the GNU Affero General Public Licenseimport uuid+import loggingfrom itertools import chainfrom django.forms.widgets import SelectMultiplefrom django.forms.widgets import Select+from django.forms.widgets import DateInput+from django.forms.widgets import TextInput++from django.forms import fieldsfrom django.utils.translation import ugettext as _from django.utils.encoding import force_unicode@@ -26,11 +31,90 @@ from django.utils.html import conditional_escapefrom django.utils.safestring import mark_safe+logger = logging.getLogger('wger.custom')+++#+# Date and time related fields+#++class Html5DateInput(DateInput):+ '''+ Custom Input class that is rendered with an HTML5 type="date"++ This is specially useful in mobile devices+ '''+ input_type = 'date'+++class Html5FormDateField(fields.DateField):+ '''+ HTML5 form date field+ '''+ widget = Html5DateInput+++class Html5TimeInput(TextInput):+ '''+ Custom Input class that is rendered with an HTML5 type="time"++ This is specially useful in mobile devices and not available+ with older versions of django.+ '''+ input_type = 'time'+++class Html5FormTimeField(fields.TimeField):+ '''+ HTML5 form time field+ '''+ widget = Html5TimeInput+++#+# Number related fields+#++class Html5NumberInput(TextInput):+ '''+ Custom Input class that is rendered with an HTML5 type="number"++ This is specially useful in mobile devices and not available+ with older versions of django.+ '''+ input_type = 'number'+++class Html5FormDecimalField(fields.DecimalField):+ '''+ HTML5 form time field+ '''+ widget = Html5NumberInput+++class Html5FormIntegerField(fields.IntegerField):+ '''+ HTML5 form time field+ '''+ widget = Html5NumberInput+++class Html5FormFloatField(fields.FloatField):+ '''+ HTML5 form time field+ '''+ widget = Html5NumberInput++#+# Others+#++class ExerciseAjaxSelect(SelectMultiple):'''Custom widget that allows to select exercises from an autocompleter- This is basically modified MultipleSelect widget+ This is basically a modified MultipleSelect widget'''def render(self, name, value, attrs=None, choices=()):diff --git a/wger/weight/models.py b/wger/weight/models.pyindex 8f2032f7c..fac4b300a 100644--- a/wger/weight/models.py+++ b/wger/weight/models.py@@ -20,13 +20,15 @@ from django.db import modelsfrom django.contrib.auth.models import Userfrom django.utils.translation import ugettext_lazy as _+from wger.utils.fields import Html5FloatField+class WeightEntry(models.Model):'''Model for a weight point'''creation_date = models.DateField(verbose_name=_('Date'))- weight = models.FloatField(verbose_name=_('Weight'))+ weight = Html5FloatField(verbose_name=_('Weight'))user = models.ForeignKey(User,verbose_name=_('User'))'''diff --git a/wger/weight/views.py b/wger/weight/views.pyindex 4a4a61fd5..ce2de308d 100644--- a/wger/weight/views.py+++ b/wger/weight/views.py@@ -47,6 +47,7 @@ from wger.weight import helpersfrom wger.utils.generic_views import WgerFormMixinfrom wger.utils.constants import DATE_FORMATS+from wger.utils.widgets import Html5DateInputfrom django.forms import widgets@@ -54,12 +55,13 @@ logger = logging.getLogger('wger.custom')class WeightForm(ModelForm):- creation_date = DateField(input_formats=DATE_FORMATS)+ creation_date = DateField(input_formats=DATE_FORMATS, widget=Html5DateInput())class Meta:model = WeightEntrywidgets = {- 'user': widgets.HiddenInput()+ 'user': widgets.HiddenInput(),+ #'weight': Html5NumberInput()}