Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Move forms to separate file

Roland Geidereeb1afc86982
diff --git a/manager/forms.py b/manager/forms.py
new file mode 100644
index 000000000..6616ac9c2
--- /dev/null
+++ b/manager/forms.py
@@ -0,0 +1,134 @@
+# -*- coding: utf-8 -*-
+
+# This file is part of Workout Manager.
+#
+# 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.
+#
+# 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
+
+'''
+This file contains forms used in the application
+'''
+
+from django.forms import Form
+from django.forms import ModelForm
+from django.forms import EmailField
+from django.forms import DateField
+from django.forms import CharField
+from django.forms import DecimalField
+from django.forms import ValidationError
+
+from django.contrib.auth.forms import UserCreationForm
+from django.contrib.auth.models import User as Django_User
+
+from django.utils.translation import ugettext as _
+
+from manager.models import UserProfile
+from manager.models import TrainingSchedule
+from manager.models import Day
+from manager.models import Set
+from manager.models import WorkoutLog
+
+from workout_manager.widgets import TranslatedSelectMultiple
+from workout_manager.widgets import ExerciseAjaxSelect
+from workout_manager.constants import DATE_FORMATS
+
+
+class UserPreferencesForm(ModelForm):
+ class Meta:
+ model = UserProfile
+ exclude = ('user',)
+
+
+class UserEmailForm(ModelForm):
+ email = EmailField(label=_("Email"),
+ help_text=_("Completely optional, but needed to reset your password "
+ "in case you forget it."),
+ required=False)
+
+ def clean_email(self):
+ # Email must be unique systemwide
+ email = self.cleaned_data["email"]
+ if not email:
+ return email
+ try:
+ Django_User.objects.get(email=email)
+ except Django_User.DoesNotExist:
+ return email
+ raise ValidationError(_("This email is already used."))
+
+
+class RegistrationForm(UserCreationForm, UserEmailForm):
+ pass
+
+
+class WorkoutForm(ModelForm):
+ class Meta:
+ model = TrainingSchedule
+ exclude = ('user',)
+
+
+class WorkoutCopyForm(Form):
+ comment = CharField(max_length=100,
+ help_text=('The goal or description of the new workout.'),
+ required=False)
+
+
+class DayForm(ModelForm):
+ class Meta:
+ model = Day
+ exclude = ('training',)
+
+ widgets = {
+ 'day': TranslatedSelectMultiple()
+ }
+
+
+class SetForm(ModelForm):
+ class Meta:
+ model = Set
+ exclude = ('exerciseday', 'order',)
+ widgets = {
+ 'exercises': ExerciseAjaxSelect(),
+ }
+
+ # We need to overwrite the init method here because otherwise Django
+ # will outut a default help text, regardless of the widget used
+ # https://code.djangoproject.com/ticket/9321
+ def __init__(self, *args, **kwargs):
+ super(SetForm, self).__init__(*args, **kwargs)
+ self.fields['exercises'].help_text = _('You can search for more than one exercise, '
+ 'they will be grouped together for a superset.')
+
+
+class HelperDateForm(Form):
+ '''
+ A helper form with only a date input
+ '''
+ date = DateField(input_formats=DATE_FORMATS)
+
+
+class WorkoutLogForm(ModelForm):
+ '''
+ Helper form for a WorkoutLog.
+
+ The field for the weight is overwritten here, activating localization (so a
+ German user can use ',' as the separator)
+ '''
+ weight = DecimalField(decimal_places=2,
+ max_digits=5,
+ localize=True)
+
+ class Meta:
+ model = WorkoutLog
+ exclude = ('user',
+ 'workout',
+ 'exercise')
diff --git a/manager/views.py b/manager/views.py
index 3d9d62cad..a4270998f 100644
--- a/manager/views.py
+++ b/manager/views.py
@@ -32,15 +32,7 @@ from django.core.urlresolvers import reverse_lazy
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy
from django.utils.formats import date_format
-from django.utils.datastructures import SortedDict
-
-from django.forms import Form
-from django.forms import ModelForm
-from django.forms import EmailField
-from django.forms import DateField
-from django.forms import CharField
-from django.forms import DecimalField
-from django.forms import ValidationError
+
from django.forms.models import modelformset_factory
@@ -49,7 +41,7 @@ from django.contrib.auth import login as django_login
from django.contrib.auth import logout as django_logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User as Django_User
-from django.contrib.auth.forms import UserCreationForm
+
from django.contrib.auth.views import login as django_loginview
from django.views.generic import DeleteView
@@ -62,7 +54,6 @@ from manager.models import TrainingSchedule
from manager.models import Day
from manager.models import Set
from manager.models import Setting
-from manager.models import UserProfile
from manager.models import WorkoutLog
from exercises.models import Exercise
@@ -71,13 +62,20 @@ from nutrition.models import NutritionPlan
from weight.models import WeightEntry
+from manager.forms import UserPreferencesForm
+from manager.forms import UserEmailForm
+from manager.forms import RegistrationForm
+from manager.forms import DayForm
+from manager.forms import WorkoutForm
+from manager.forms import WorkoutCopyForm
+from manager.forms import SetForm
+from manager.forms import HelperDateForm
+from manager.forms import WorkoutLogForm
+
from workout_manager.constants import WORKOUT_TAB
from workout_manager.constants import USER_TAB
-from workout_manager.constants import DATE_FORMATS
from workout_manager.generic_views import YamlFormMixin
from workout_manager.generic_views import YamlDeleteMixin
-from workout_manager.widgets import ExerciseAjaxSelect
-from workout_manager.widgets import TranslatedSelectMultiple
logger = logging.getLogger('workout_manager.custom')
@@ -148,12 +146,6 @@ def index(request):
# ************************
# User functions
# ************************
-class UserPreferencesForm(ModelForm):
- class Meta:
- model = UserProfile
- exclude = ('user',)
-
-
def login(request):
"""
Small wrapper around the django login view
@@ -171,28 +163,6 @@ def logout(request):
return HttpResponseRedirect(reverse('login'))
-class UserEmailForm(ModelForm):
- email = EmailField(label=_("Email"),
- help_text=_("Completely optional, but needed to reset your password "
- "in case you forget it."),
- required=False)
-
- def clean_email(self):
- # Email must be unique systemwide
- email = self.cleaned_data["email"]
- if not email:
- return email
- try:
- Django_User.objects.get(email=email)
- except Django_User.DoesNotExist:
- return email
- raise ValidationError(_("This email is already used."))
-
-
-class RegistrationForm(UserCreationForm, UserEmailForm):
- pass
-
-
def registration(request):
"""A form to allow for registration of new users
"""
@@ -302,12 +272,6 @@ def api_user_preferences(request):
# ************************
# Workout functions
# ************************
-class WorkoutForm(ModelForm):
- class Meta:
- model = TrainingSchedule
- exclude = ('user',)
-
-
@login_required
def overview(request):
"""An overview of all the user's workouts
@@ -323,6 +287,7 @@ def overview(request):
template_data,
context_instance=RequestContext(request))
+
@login_required
def view_workout(request, id):
"""Show the workout with the given ID
@@ -365,12 +330,6 @@ def view_workout(request, id):
context_instance=RequestContext(request))
-class WorkoutCopyForm(Form):
- comment = CharField(max_length=100,
- help_text=('The goal or description of the new workout.'),
- required=False)
-
-
@login_required
def copy_workout(request, pk):
"""
@@ -480,16 +439,6 @@ class WorkoutEditView(YamlFormMixin, UpdateView):
# ************************
# Day functions
# ************************
-class DayForm(ModelForm):
- class Meta:
- model = Day
- exclude = ('training',)
-
- widgets = {
- 'day': TranslatedSelectMultiple()
- }
-
-
class DayView(YamlFormMixin):
"""
Base generic view for exercise day
@@ -600,22 +549,6 @@ def view_day(request, id):
# ************************
# Set functions
# ************************
-class SetForm(ModelForm):
- class Meta:
- model = Set
- exclude = ('exerciseday', 'order',)
- widgets = {
- 'exercises': ExerciseAjaxSelect(),
- }
-
- # We need to overwrite the init method here because otherwise Django
- # will outut a default help text, regardless of the widget used
- # https://code.djangoproject.com/ticket/9321
- def __init__(self, *args, **kwargs):
- super(SetForm, self).__init__(*args, **kwargs)
- self.fields['exercises'].help_text=_('You can search for more than one exercise, '
- 'they will be grouped together for a superset.')
-
@login_required
def edit_set(request, id, day_id, set_id=None):
""" Edits/creates a set
@@ -828,12 +761,6 @@ def api_edit_set(request):
# ************************
# Settings functions
# ************************
-class SettingForm(ModelForm):
- class Meta:
- model = Setting
- exclude = ('set', 'exercise')
-
-
@login_required
def edit_setting(request, id, set_id, exercise_id, setting_id=None):
template_data = {}
@@ -943,30 +870,6 @@ def delete_setting(request, id, set_id, exercise_id):
# ************************
# Log functions
# ************************
-class HelperDateForm(Form):
- '''
- A helper form with only a date input
- '''
- date = DateField(input_formats=DATE_FORMATS)
-
-
-class WorkoutLogForm(ModelForm):
- '''
- Helper form for a WorkoutLog.
-
- The field for the weight is overwritten here, activating localization (so a
- German user can use ',' as the separator)
- '''
- weight = DecimalField(decimal_places=2,
- max_digits=5,
- localize=True)
-
- class Meta:
- model = WorkoutLog
- exclude = ('user',
- 'workout',
- 'exercise')
-
class WorkoutLogUpdateView(YamlFormMixin, UpdateView):
"""
Generic view to edit an existing workout log weight entry