diff --git a/wger/manager/api/serializers.py b/wger/manager/api/serializers.py index 4873edeea5..5d72f0b805 100644 --- a/wger/manager/api/serializers.py +++ b/wger/manager/api/serializers.py @@ -350,11 +350,11 @@ class SetConfigDataSerializer(serializers.Serializer): max_sets = serializers.IntegerField(allow_null=True) weight = DecimalOrIntegerField(max_digits=6, decimal_places=2) max_weight = DecimalOrIntegerField(max_digits=6, decimal_places=2) - weight_unit = serializers.IntegerField(allow_null=True) + weight_unit = serializers.IntegerField() weight_rounding = serializers.DecimalField(max_digits=4, decimal_places=2) repetitions = DecimalOrIntegerField(max_digits=6, decimal_places=2) max_repetitions = DecimalOrIntegerField(max_digits=6, decimal_places=2) - repetitions_unit = serializers.IntegerField(allow_null=True) + repetitions_unit = serializers.IntegerField() repetitions_rounding = serializers.DecimalField(max_digits=4, decimal_places=2) rir = DecimalOrIntegerField(max_digits=2, decimal_places=1) max_rir = DecimalOrIntegerField(max_digits=2, decimal_places=1) diff --git a/wger/manager/migrations/0024_fill_null_weight_repetitions_units.py b/wger/manager/migrations/0024_fill_null_weight_repetitions_units.py new file mode 100644 index 0000000000..21cfacf4b6 --- /dev/null +++ b/wger/manager/migrations/0024_fill_null_weight_repetitions_units.py @@ -0,0 +1,42 @@ +from django.db import migrations +from django.db.migrations.state import StateApps + + +def fill_null_units_slot_entry(apps: StateApps, schema_editor): + """ + Backfill all null unit values in SlotEntry with their defaults. + REP_UNIT_REPETITIONS = 1 , WEIGHT_UNIT_KG = 1 + """ + SlotEntry = apps.get_model("manager", "SlotEntry") + + SlotEntry.objects.filter(repetition_unit__isnull=True).update(repetition_unit_id=1) + SlotEntry.objects.filter(weight_unit__isnull=True).update(weight_unit_id=1) + + +def fill_null_units_workout_log(apps: StateApps, schema_editor): + """ + Backfill all null unit values in WorkoutLog with their defaults. + """ + WorkoutLog = apps.get_model("manager", "WorkoutLog") + + # Only fill repetitions_unit where repetitions is also not null + WorkoutLog.objects.filter( + repetitions_unit__isnull=True, + repetitions__isnull=False, + ).update(repetitions_unit_id=1) + + # Only fill weight_unit where weight is also not null + WorkoutLog.objects.filter( + weight_unit__isnull=True, + weight__isnull=False, + ).update(weight_unit_id=1) + + +class Migration(migrations.Migration): + dependencies = [ + ("manager", "0023_change_validators"), + ] + + operations = [ + migrations.RunPython(fill_null_units_slot_entry, fill_null_units_workout_log), + ] diff --git a/wger/manager/migrations/0025_make_weight_repetitions_units_non_nullable.py b/wger/manager/migrations/0025_make_weight_repetitions_units_non_nullable.py new file mode 100644 index 0000000000..9e47ba1dee --- /dev/null +++ b/wger/manager/migrations/0025_make_weight_repetitions_units_non_nullable.py @@ -0,0 +1,56 @@ +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('manager', '0024_fill_null_weight_repetitions_units'), + ('core', '0022_move_email_verified_to_emailaddress'), + ] + + operations = [ + # SlotEntry: make repetition_unit non-nullable + migrations.AlterField( + model_name='slotentry', + name='repetition_unit', + field=models.ForeignKey( + default=1, + on_delete=django.db.models.deletion.CASCADE, + to='core.repetitionunit', + ), + ), + # SlotEntry: make weight_unit non-nullable + migrations.AlterField( + model_name='slotentry', + name='weight_unit', + field=models.ForeignKey( + default=1, + on_delete=django.db.models.deletion.CASCADE, + to='core.weightunit', + verbose_name='Unit', + ), + ), + # WorkoutLog: make repetition_unit non-nullable + migrations.AlterField( + model_name='workoutlog', + name='repetitions_unit', + field=models.ForeignKey( + default=1, + on_delete=django.db.models.deletion.CASCADE, + to='core.repetitionunit', + verbose_name='Repetitions unit', + ), + ), + # WorkoutLog: make weight_unit non-nullable + migrations.AlterField( + model_name='workoutlog', + name='weight_unit', + field=models.ForeignKey( + default=1, + on_delete=django.db.models.deletion.CASCADE, + to='core.weightunit', + verbose_name='Weight unit', + ), + ), + ] diff --git a/wger/manager/models/log.py b/wger/manager/models/log.py index 5370e84c95..08eff3a115 100644 --- a/wger/manager/models/log.py +++ b/wger/manager/models/log.py @@ -113,8 +113,6 @@ class WorkoutLog(models.Model): verbose_name='Repetitions unit', default=REP_UNIT_REPETITIONS, on_delete=models.CASCADE, - null=True, - blank=True, ) """ The repetition unit of the log. This can be e.g. a repetition, a minute, etc. @@ -148,8 +146,6 @@ class WorkoutLog(models.Model): verbose_name='Weight unit', default=WEIGHT_UNIT_KG, on_delete=models.CASCADE, - null=True, - blank=True, ) """ The weight unit of the log. This can be e.g. kg, lb, km/h, etc. @@ -238,12 +234,6 @@ def clean(self): if self.repetitions is None and self.weight is None: raise ValidationError('Both repetitions and weight cannot be null at the same time.') - if self.repetitions is not None and self.repetitions_unit is None: - raise ValidationError('Repetitions unit must be present if repetitions have a value.') - - if self.weight is not None and self.weight_unit is None: - raise ValidationError('Weight unit must be present if weight has a value.') - def save(self, *args, **kwargs): """ Plumbing diff --git a/wger/manager/models/slot_entry.py b/wger/manager/models/slot_entry.py index 63829f2a49..9ad340c9de 100644 --- a/wger/manager/models/slot_entry.py +++ b/wger/manager/models/slot_entry.py @@ -110,8 +110,6 @@ class SlotEntry(models.Model): RepetitionUnit, default=REP_UNIT_REPETITIONS, on_delete=models.CASCADE, - null=True, - blank=True, ) """ The repetition unit of a set. This can be e.g. a repetition, a minute, etc. @@ -132,8 +130,6 @@ class SlotEntry(models.Model): verbose_name='Unit', default=WEIGHT_UNIT_KG, on_delete=models.CASCADE, - null=True, - blank=True, ) """ The weight unit of a set. This can be e.g. kg, lb, km/h, etc.