mirror of
https://github.com/wger-project/wger.git
synced 2026-02-18 00:17:51 +01:00
Merge pull request #2181 from ndimoro/fix/api-improvements
Fix: Auto-calculate SlotEntry.order when not provided via API
This commit is contained in:
@@ -213,6 +213,12 @@ class SlotEntry(models.Model):
|
||||
)
|
||||
if not self.weight_rounding:
|
||||
self.weight_rounding = self.slot.day.routine.user.userprofile.weight_rounding
|
||||
|
||||
# Auto-calculate order if not provided
|
||||
if self.order is None:
|
||||
max_order = self.slot.entries.aggregate(models.Max('order'))['order__max']
|
||||
self.order = (max_order or 0) + 1
|
||||
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
def get_owner_object(self):
|
||||
|
||||
@@ -58,6 +58,24 @@ class SlotEntryTestCase(WgerTestCase):
|
||||
)
|
||||
self.slot_entry.save()
|
||||
|
||||
def test_auto_add_order(self):
|
||||
"""
|
||||
Test that the order is automatically added if not provided
|
||||
"""
|
||||
slot_entry_2 = SlotEntry(slot_id=1, exercise_id=2, order=None)
|
||||
slot_entry_2.save()
|
||||
|
||||
slot_entry_3 = SlotEntry(slot_id=1, exercise_id=3, order=7)
|
||||
slot_entry_3.save()
|
||||
|
||||
slot_entry_4 = SlotEntry(slot_id=1, exercise_id=3)
|
||||
slot_entry_4.save()
|
||||
|
||||
self.assertEqual(self.slot_entry.order, 1)
|
||||
self.assertEqual(slot_entry_2.order, 2)
|
||||
self.assertEqual(slot_entry_3.order, 7)
|
||||
self.assertEqual(slot_entry_4.order, 8)
|
||||
|
||||
def test_weight_config(self):
|
||||
"""
|
||||
Test that the weight is correctly calculated for each step / iteration
|
||||
|
||||
Reference in New Issue
Block a user