Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Allow Django to check the uniqueness of date-user pair in weight entries

Roland Geidere69592b5b421
diff --git a/wger/manager/templates/mobile/tags/yaml_form_element.html b/wger/manager/templates/mobile/tags/yaml_form_element.html
index 090a658a7..f15c19159 100644
--- a/wger/manager/templates/mobile/tags/yaml_form_element.html
+++ b/wger/manager/templates/mobile/tags/yaml_form_element.html
@@ -1,4 +1,5 @@
{% load i18n %}
+{% if not field.is_hidden %}
<tr>
<td colspan="2">
<label for="{{field.auto_id}}">
@@ -26,3 +27,6 @@
</div>
</td>
</tr>
+{% else %}
+ {{field}}
+{% endif %}
diff --git a/wger/manager/templates/tags/yaml_form_element.html b/wger/manager/templates/tags/yaml_form_element.html
index a3d5a2953..24bbaf737 100644
--- a/wger/manager/templates/tags/yaml_form_element.html
+++ b/wger/manager/templates/tags/yaml_form_element.html
@@ -1,4 +1,5 @@
{% load i18n %}
+{% if not field.is_hidden %}
<tr id="form-{{field.name}}">
<td style="text-align:right;">
{% if field.errors %}<div class="ym-fbox-text ym-error"><span class="ym-message">{% endif %}
@@ -24,3 +25,10 @@
{% if field.help_text %}<p class="dimmed">{% trans field.help_text %}</p>{% endif %}
</td>
</tr>
+{% else %}
+ {# #}
+ {# TODO: the resulting HTML is not valid #}
+ {# and all the form rendering functions #}
+ {# should be made more flexible anyway #}
+ {{field}}
+{% endif %}
\ No newline at end of file
diff --git a/wger/weight/models.py b/wger/weight/models.py
index 88a95608d..8f2032f7c 100644
--- a/wger/weight/models.py
+++ b/wger/weight/models.py
@@ -28,16 +28,25 @@ class WeightEntry(models.Model):
creation_date = models.DateField(verbose_name=_('Date'))
weight = models.FloatField(verbose_name=_('Weight'))
user = models.ForeignKey(User,
- verbose_name=_('User'),
- editable=False)
+ verbose_name=_('User'))
+ '''
+ The user the weight entry belongs to.
- # Metaclass to set some other properties
- class Meta:
+ NOTE: this field is neither marked as editable false nor is it excluded in
+ the form. This is done intentionally because otherwise it's *very* difficult
+ and ugly to validate the uniqueness of unique_together fields and one field
+ is excluded from the form. This does not pose any security risk because the
+ value from the form is ignored and the request's user always used.
+ '''
- # Order by creation_date, ascending (oldest last), better for diagram
+ class Meta:
+ '''
+ Metaclass to set some other properties
+ '''
+ verbose_name = _('Weight entry')
ordering = ["creation_date", ]
get_latest_by = "creation_date"
- unique_together = (("creation_date", "user"))
+ unique_together = ("creation_date", "user")
def __unicode__(self):
'''
diff --git a/wger/weight/tests/test_entry.py b/wger/weight/tests/test_entry.py
index decf098e4..476d35207 100644
--- a/wger/weight/tests/test_entry.py
+++ b/wger/weight/tests/test_entry.py
@@ -14,12 +14,8 @@
import datetime
-from django.core.urlresolvers import reverse
-
from wger.weight.models import WeightEntry
-from wger.manager.tests.testcase import WorkoutManagerTestCase
-from wger.manager.tests.testcase import WorkoutManagerDeleteTestCase
from wger.manager.tests.testcase import WorkoutManagerEditTestCase
from wger.manager.tests.testcase import WorkoutManagerAddTestCase
from wger.manager.tests.testcase import ApiBaseResourceTestCase
@@ -35,7 +31,8 @@ class AddWeightEntryTestCase(WorkoutManagerAddTestCase):
pk = 8
user_fail = False
data = {'weight': 81.1,
- 'creation_date': datetime.date(2013, 2, 1)}
+ 'creation_date': datetime.date(2013, 2, 1),
+ 'user': 1}
class EditWeightEntryTestCase(WorkoutManagerEditTestCase):
@@ -47,7 +44,8 @@ class EditWeightEntryTestCase(WorkoutManagerEditTestCase):
url = 'weight-edit'
pk = 1
data = {'weight': 100,
- 'creation_date': datetime.date(2013, 2, 1)}
+ 'creation_date': datetime.date(2013, 2, 1),
+ 'user': 1}
user_success = 'test'
user_fail = 'admin'
diff --git a/wger/weight/views.py b/wger/weight/views.py
index bb0964b52..4a4a61fd5 100644
--- a/wger/weight/views.py
+++ b/wger/weight/views.py
@@ -17,6 +17,7 @@
import logging
import csv
import json
+import datetime
from django.template import RequestContext
@@ -46,6 +47,7 @@ from wger.weight import helpers
from wger.utils.generic_views import WgerFormMixin
from wger.utils.constants import DATE_FORMATS
+from django.forms import widgets
logger = logging.getLogger('wger.custom')
@@ -56,6 +58,9 @@ class WeightForm(ModelForm):
class Meta:
model = WeightEntry
+ widgets = {
+ 'user': widgets.HiddenInput()
+ }
class WeightAddView(WgerFormMixin, CreateView):
@@ -71,11 +76,20 @@ class WeightAddView(WgerFormMixin, CreateView):
form_action = reverse_lazy('weight-add')
success_url = reverse_lazy('wger.weight.views.overview')
+ def get_initial(self):
+ '''
+ Set the initial data for the form.
+
+ Read the comment on weight/models.py WeightEntry about why we need
+ to pass the user here.
+ '''
+ return {'user': self.request.user,
+ 'creation_date': datetime.date.today()}
+
def form_valid(self, form):
'''
Set the owner of the entry here
'''
-
form.instance.user = self.request.user
return super(WeightAddView, self).form_valid(form)