Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add script to delete unused exercises from the database
Roland Geidera0ae64da388e
diff --git a/wger/exercises/management/commands/delete-unused-exercises.py b/wger/exercises/management/commands/delete-unused-exercises.pynew file mode 100644index 000000000..6c2edc6f6--- /dev/null+++ b/wger/exercises/management/commands/delete-unused-exercises.py@@ -0,0 +1,51 @@+# This file is part of wger Workout Manager.+#+# wger 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.+#+# wger 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++# Standard Library+import collections++# Django+from django.core.management.base import BaseCommand++# wger+from wger.core.models import Language+from wger.exercises.models import ExerciseBase+from wger.manager.models import Setting, WorkoutLog+from wger.utils.constants import ENGLISH_SHORT_NAME+++class Command(BaseCommand):+ """+ Deletes all unused exercises+ """++ help = """Deletes all unused exercises from the database. After running this script, sync+ the database with ´python manage.py sync-exercises`+ """++ def handle(self, **options):++ out = f'{ExerciseBase.objects.all().count()} exercise bases currently in the database'+ self.stdout.write(out)++ bases = set(Setting.objects.all().values_list('exercise_base', flat=True))+ bases_logs = set(WorkoutLog.objects.all().values_list('exercise_base', flat=True))+ bases.update(bases_logs)++ out = f'-> {len(bases)} exercise bases are in use, deleting the rest'+ self.stdout.write(out)+ ExerciseBase.objects.exclude(pk__in=bases).delete()++ out = self.style.SUCCESS('Done! You can now run python manage.py sync-exercises')+ self.stdout.write(out)diff --git a/wger/exercises/models/base.py b/wger/exercises/models/base.pyindex 16ac55ff9..e7513ed5c 100644--- a/wger/exercises/models/base.py+++ b/wger/exercises/models/base.py@@ -87,7 +87,7 @@ class ExerciseBase(AbstractLicenseModel, AbstractHistoryMixin, models.Model):variations = models.ForeignKey(Variation,verbose_name=_('Variations'),- on_delete=models.CASCADE,+ on_delete=models.SET_NULL,null=True,blank=True,)