Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Fix issue with sorting

Roland Geider531c6b3649f0
diff --git a/wger/manager/models.py b/wger/manager/models.py
index dc245f294..e1c767a6b 100644
--- a/wger/manager/models.py
+++ b/wger/manager/models.py
@@ -279,6 +279,12 @@ class DaysOfWeek(models.Model):
day_of_week = models.CharField(max_length=9,
verbose_name=_('Day of the week'))
+ class Meta:
+ '''
+ Order by day-ID, this is needed for some DBs
+ '''
+ ordering = ["pk", ]
+
def __unicode__(self):
'''
Return a more human-readable representation
@@ -313,11 +319,13 @@ class Day(models.Model):
'''
return self.training
- class Meta:
+ @property
+ def get_first_day_id(self):
'''
- Order by ID. this is needed for some DBs
+ Return the PK of the first day of the week, this is used in the template
+ to order the days in the template
'''
- ordering = ["day__id", ]
+ return self.day.all()[0].pk
def save(self, *args, **kwargs):
'''
diff --git a/wger/manager/templates/workout/view.html b/wger/manager/templates/workout/view.html
index 6faac8703..bf1de6455 100644
--- a/wger/manager/templates/workout/view.html
+++ b/wger/manager/templates/workout/view.html
@@ -43,7 +43,9 @@ $(document).ready(function() {
</p>
{% endif %}
-{% for day in workout.day_set.select_related %}
+
+
+{% for day in workout.day_set.select_related|dictsort:"get_first_day_id" %}
{% cache cache_timeout day-view day.id %}
<div id="div-day-{{ day.id }}">
{% render_day day %}
diff --git a/wger/manager/tests/test_day.py b/wger/manager/tests/test_day.py
index 5259e072f..861957197 100644
--- a/wger/manager/tests/test_day.py
+++ b/wger/manager/tests/test_day.py
@@ -179,3 +179,20 @@ class WorkoutCacheTestCase(WorkoutManagerTestCase):
day1.delete()
self.assertFalse(cache.get(get_template_cache_name('day-view', 1)))
self.assertTrue(cache.get(get_template_cache_name('day-view', 2)))
+
+
+class DayTestCase(WorkoutManagerTestCase):
+ '''
+ Other tests
+ '''
+
+ def test_day_id_property(self):
+ '''
+ Test that the attribute get_first_day_id works correctly
+ '''
+
+ day = Day.objects.get(pk=5)
+ self.assertEqual(day.get_first_day_id, 3)
+
+ day = Day.objects.get(pk=3)
+ self.assertEqual(day.get_first_day_id, 1)