Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Remove uploadable image path

Roland Geider853ca50bfcc9
diff --git a/wger/trophies/api/serializers.py b/wger/trophies/api/serializers.py
index 6bd3a0033..f94e62ddf 100644
--- a/wger/trophies/api/serializers.py
+++ b/wger/trophies/api/serializers.py
@@ -14,6 +14,12 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+# Standard Library
+import logging
+
+# Django
+from django.templatetags.static import static
+
# Third Party
from rest_framework import serializers
@@ -24,6 +30,8 @@ from wger.trophies.models import (
UserTrophy,
)
+logger = logging.getLogger(__name__)
+
class TrophySerializer(serializers.ModelSerializer):
"""
@@ -32,6 +40,8 @@ class TrophySerializer(serializers.ModelSerializer):
Shows trophy information for listing active trophies.
"""
+ image = serializers.SerializerMethodField()
+
class Meta:
model = Trophy
fields = (
@@ -47,6 +57,18 @@ class TrophySerializer(serializers.ModelSerializer):
)
read_only_fields = fields
+ def get_image(self, obj: Trophy):
+ """Build absolute URL to trophy image, if possible."""
+
+ static_url = static(obj.image_rel_path)
+ request = self.context.get('request')
+ if request is not None:
+ return request.build_absolute_uri(static_url)
+
+ # no host available
+ logger.info('Cannot build absolute URL for trophy image without request context')
+ return None
+
class UserTrophySerializer(serializers.ModelSerializer):
"""
diff --git a/wger/trophies/fixtures/initial_trophies.json b/wger/trophies/fixtures/initial_trophies.json
index cbeef12ff..1cb61b93c 100644
--- a/wger/trophies/fixtures/initial_trophies.json
+++ b/wger/trophies/fixtures/initial_trophies.json
@@ -3,6 +3,7 @@
"model": "trophies.trophy",
"pk": 1,
"fields": {
+ "uuid": "5362e55b-eaf1-4e34-9ef8-661538a3bdd9",
"name": "Beginner",
"description": "Complete your first workout",
"trophy_type": "count",
@@ -11,13 +12,16 @@
"is_hidden": false,
"is_progressive": false,
"is_active": true,
- "order": 1
+ "order": 1,
+ "created": "2025-12-19T00:00:00Z",
+ "updated": "2025-12-19T00:00:00Z"
}
},
{
"model": "trophies.trophy",
"pk": 2,
"fields": {
+ "uuid": "b605b6a1-953d-41fb-87c9-a2f88b5f5907",
"name": "Unstoppable",
"description": "Maintain a 30-day workout streak",
"trophy_type": "sequence",
@@ -26,13 +30,16 @@
"is_hidden": false,
"is_progressive": true,
"is_active": true,
- "order": 2
+ "order": 2,
+ "created": "2025-12-19T00:00:00Z",
+ "updated": "2025-12-19T00:00:00Z"
}
},
{
"model": "trophies.trophy",
"pk": 3,
"fields": {
+ "uuid": "bf60e051-a9a5-4bf5-ac4b-1aa713febca7",
"name": "Weekend Warrior",
"description": "Work out on Saturday and Sunday for 4 consecutive weekends",
"trophy_type": "sequence",
@@ -41,13 +48,16 @@
"is_hidden": false,
"is_progressive": true,
"is_active": true,
- "order": 3
+ "order": 3,
+ "created": "2025-12-19T00:00:00Z",
+ "updated": "2025-12-19T00:00:00Z"
}
},
{
"model": "trophies.trophy",
"pk": 4,
"fields": {
+ "uuid": "5353989b-adc0-481b-a9bc-64365a9179e8",
"name": "Lifter",
"description": "Lift a cumulative total of 5,000 kg",
"trophy_type": "volume",
@@ -56,13 +66,16 @@
"is_hidden": false,
"is_progressive": true,
"is_active": true,
- "order": 4
+ "order": 4,
+ "created": "2025-12-19T00:00:00Z",
+ "updated": "2025-12-19T00:00:00Z"
}
},
{
"model": "trophies.trophy",
"pk": 5,
"fields": {
+ "uuid": "3c53887b-feba-4d73-8022-e446f46395c8",
"name": "Atlas",
"description": "Lift a cumulative total of 100,000 kg",
"trophy_type": "volume",
@@ -71,13 +84,16 @@
"is_hidden": false,
"is_progressive": true,
"is_active": true,
- "order": 5
+ "order": 5,
+ "created": "2025-12-19T00:00:00Z",
+ "updated": "2025-12-19T00:00:00Z"
}
},
{
"model": "trophies.trophy",
"pk": 6,
"fields": {
+ "uuid": "cc833c0b-1fd9-4cde-baa5-3bff9cd97d0c",
"name": "Early Bird",
"description": "Complete a workout before 6:00 AM",
"trophy_type": "time",
@@ -86,13 +102,16 @@
"is_hidden": false,
"is_progressive": false,
"is_active": true,
- "order": 6
+ "order": 6,
+ "created": "2025-12-19T00:00:00Z",
+ "updated": "2025-12-19T00:00:00Z"
}
},
{
"model": "trophies.trophy",
"pk": 7,
"fields": {
+ "uuid": "2b00ff1e-69f8-47f0-b8df-976a4127a425",
"name": "Night Owl",
"description": "Complete a workout after 9:00 PM",
"trophy_type": "time",
@@ -101,13 +120,16 @@
"is_hidden": false,
"is_progressive": false,
"is_active": true,
- "order": 7
+ "order": 7,
+ "created": "2025-12-19T00:00:00Z",
+ "updated": "2025-12-19T00:00:00Z"
}
},
{
"model": "trophies.trophy",
"pk": 8,
"fields": {
+ "uuid": "31a71d9a-bf26-4f18-b82f-afefe6f50df2",
"name": "New Year, New Me",
"description": "Work out on January 1st",
"trophy_type": "date",
@@ -116,13 +138,16 @@
"is_hidden": false,
"is_progressive": false,
"is_active": true,
- "order": 8
+ "order": 8,
+ "created": "2025-12-19T00:00:00Z",
+ "updated": "2025-12-19T00:00:00Z"
}
},
{
"model": "trophies.trophy",
"pk": 9,
"fields": {
+ "uuid": "32bb12da-b25f-4e18-81e4-b695eb65283e",
"name": "Phoenix",
"description": "Return to training after being inactive for 30 days",
"trophy_type": "other",
@@ -131,7 +156,27 @@
"is_hidden": true,
"is_progressive": false,
"is_active": true,
- "order": 9
+ "order": 9,
+ "created": "2025-12-19T00:00:00Z",
+ "updated": "2025-12-19T00:00:00Z"
+ }
+ },
+ {
+ "model": "trophies.trophy",
+ "pk": 10,
+ "fields": {
+ "uuid": "d4a5dbe7-7e15-4f7c-8560-a19f5f27eb2e",
+ "name": "Personal Record",
+ "description": "Repeatable Personal Record (PR) trophy",
+ "trophy_type": "pr",
+ "checker_class": "personal_record",
+ "checker_params": {},
+ "is_hidden": true,
+ "is_progressive": false,
+ "is_repeatable": true,
+ "order": 10,
+ "created": "2025-12-19T00:00:00Z",
+ "updated": "2025-12-19T00:00:00Z"
}
}
]
diff --git a/wger/trophies/migrations/0001_initial.py b/wger/trophies/migrations/0001_initial.py
index 8670de75a..5f10ceca3 100644
--- a/wger/trophies/migrations/0001_initial.py
+++ b/wger/trophies/migrations/0001_initial.py
@@ -1,9 +1,9 @@
# Generated by Django 5.2.8 on 2025-11-25 10:54
+import uuid
+
import django.core.validators
import django.db.models.deletion
-import uuid
-import wger.trophies.models.trophy
from django.conf import settings
from django.db import migrations, models
@@ -41,15 +41,6 @@ class Migration(migrations.Migration):
verbose_name='Description',
),
),
- (
- 'image',
- models.ImageField(
- blank=True,
- null=True,
- upload_to=wger.trophies.models.trophy.trophy_image_upload_path,
- verbose_name='Image',
- ),
- ),
(
'trophy_type',
models.CharField(
@@ -59,6 +50,7 @@ class Migration(migrations.Migration):
('count', 'Count-based'),
('sequence', 'Sequence-based'),
('date', 'Date-based'),
+ ('pr', 'Personal Record'),
('other', 'Other'),
],
default='other',
diff --git a/wger/trophies/migrations/0003_load_initial_trophies.py b/wger/trophies/migrations/0003_load_initial_trophies.py
index 67be18aa0..5b02d67fc 100644
--- a/wger/trophies/migrations/0003_load_initial_trophies.py
+++ b/wger/trophies/migrations/0003_load_initial_trophies.py
@@ -1,9 +1,11 @@
# Generated by Django 5.2.9 on 2025-12-03 18:33
from django.db import migrations
+from django.db.backends.base.schema import BaseDatabaseSchemaEditor
+from django.db.migrations.state import StateApps
-def load_initial_trophies(apps, schema_editor):
+def load_initial_trophies(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor):
"""
Load the initial set of 9 trophies.
@@ -15,6 +17,7 @@ def load_initial_trophies(apps, schema_editor):
# Define the initial trophies (same as in load_trophies management command)
trophies_data = [
{
+ 'uuid': '5362e55b-eaf1-4e34-9ef8-661538a3bdd9',
'name': 'Beginner',
'description': 'Complete your first workout',
'trophy_type': 'count',
@@ -25,6 +28,7 @@ def load_initial_trophies(apps, schema_editor):
'order': 1,
},
{
+ 'uuid': 'b605b6a1-953d-41fb-87c9-a2f88b5f5907',
'name': 'Unstoppable',
'description': 'Maintain a 30-day workout streak',
'trophy_type': 'sequence',
@@ -35,6 +39,7 @@ def load_initial_trophies(apps, schema_editor):
'order': 2,
},
{
+ 'uuid': 'bf60e051-a9a5-4bf5-ac4b-1aa713febca7',
'name': 'Weekend Warrior',
'description': 'Work out on Saturday and Sunday for 4 consecutive weekends',
'trophy_type': 'sequence',
@@ -45,6 +50,7 @@ def load_initial_trophies(apps, schema_editor):
'order': 3,
},
{
+ 'uuid': '5353989b-adc0-481b-a9bc-64365a9179e8',
'name': 'Lifter',
'description': 'Lift a cumulative total of 5,000 kg',
'trophy_type': 'volume',
@@ -55,6 +61,7 @@ def load_initial_trophies(apps, schema_editor):
'order': 4,
},
{
+ 'uuid': '3c53887b-feba-4d73-8022-e446f46395c8',
'name': 'Atlas',
'description': 'Lift a cumulative total of 100,000 kg',
'trophy_type': 'volume',
@@ -65,6 +72,7 @@ def load_initial_trophies(apps, schema_editor):
'order': 5,
},
{
+ 'uuid': 'cc833c0b-1fd9-4cde-baa5-3bff9cd97d0c',
'name': 'Early Bird',
'description': 'Complete a workout before 6:00 AM',
'trophy_type': 'time',
@@ -75,6 +83,7 @@ def load_initial_trophies(apps, schema_editor):
'order': 6,
},
{
+ 'uuid': '2b00ff1e-69f8-47f0-b8df-976a4127a425',
'name': 'Night Owl',
'description': 'Complete a workout after 9:00 PM',
'trophy_type': 'time',
@@ -85,6 +94,7 @@ def load_initial_trophies(apps, schema_editor):
'order': 7,
},
{
+ 'uuid': '31a71d9a-bf26-4f18-b82f-afefe6f50df2',
'name': 'New Year, New Me',
'description': 'Work out on January 1st',
'trophy_type': 'date',
@@ -95,6 +105,7 @@ def load_initial_trophies(apps, schema_editor):
'order': 8,
},
{
+ 'uuid': '32bb12da-b25f-4e18-81e4-b695eb65283e',
'name': 'Phoenix',
'description': 'Return to training after being inactive for 30 days',
'trophy_type': 'other',
@@ -117,11 +128,9 @@ def load_initial_trophies(apps, schema_editor):
else:
skipped_count += 1
- if created_count > 0:
- print(f'Trophy migration: Created {created_count} trophies, skipped {skipped_count}')
-def reverse_load_trophies(apps, schema_editor):
+def reverse_load_trophies(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor):
"""
Reverse migration - delete the initial trophies.
diff --git a/wger/trophies/migrations/0004_trophy_is_repeatable_usertrophy_context_data.py b/wger/trophies/migrations/0004_trophy_is_repeatable_usertrophy_context_data.py
index f4e709477..cab623678 100644
--- a/wger/trophies/migrations/0004_trophy_is_repeatable_usertrophy_context_data.py
+++ b/wger/trophies/migrations/0004_trophy_is_repeatable_usertrophy_context_data.py
@@ -1,6 +1,31 @@
# Generated by Django 5.2.9 on 2025-12-16 16:14
from django.db import migrations, models
+from django.db.backends.base.schema import BaseDatabaseSchemaEditor
+from django.db.migrations.state import StateApps
+
+
+def create_personal_record_trophy(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor):
+ Trophy = apps.get_model('trophies', 'Trophy')
+
+ if not Trophy.objects.filter(name='Personal Record').exists():
+ Trophy.objects.create(
+ uuid='d4a5dbe7-7e15-4f7c-8560-a19f5f27eb2e',
+ name='Personal Record',
+ description='Achieve a new Personal Record on any exercise',
+ trophy_type='pr',
+ checker_class='personal_record',
+ checker_params={},
+ is_hidden=True,
+ is_progressive=False,
+ is_repeatable=True,
+ order=10,
+ )
+
+
+def remove_personal_record_trophy(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor):
+ Trophy = apps.get_model('trophies', 'Trophy')
+ Trophy.objects.filter(name='Personal Record').delete()
class Migration(migrations.Migration):
@@ -8,28 +33,6 @@ class Migration(migrations.Migration):
('trophies', '0003_load_initial_trophies'),
]
- def create_personal_record_trophy(apps, schema_editor):
- Trophy = apps.get_model('trophies', 'Trophy')
-
- if not Trophy.objects.filter(name='Personal Record').exists():
- Trophy.objects.create(
- name='Personal Record',
- description='Achieve a new Personal Record on any exercise',
- trophy_type='other',
- checker_class='personal_record',
- checker_params={},
- is_hidden=True,
- is_progressive=False,
- is_repeatable=True,
- order=10,
- )
-
- print(f"Trophy migration: Created trophy 'Personal Record'")
-
- def remove_personal_record_trophy(apps, schema_editor):
- Trophy = apps.get_model('trophies', 'Trophy')
- Trophy.objects.filter(name='Personal Record').delete()
-
operations = [
migrations.AddField(
model_name='trophy',
diff --git a/wger/trophies/models/trophy.py b/wger/trophies/models/trophy.py
index c12d5c91a..d19acc901 100644
--- a/wger/trophies/models/trophy.py
+++ b/wger/trophies/models/trophy.py
@@ -22,14 +22,6 @@ from django.db import models
from django.utils.translation import gettext_lazy as _
-def trophy_image_upload_path(instance, filename):
- """
- Returns the upload path for trophy images
- """
- ext = filename.split('.')[-1]
- return f'trophies/{instance.uuid}.{ext}'
-
-
class Trophy(models.Model):
"""
Model representing a trophy/achievement that users can earn
@@ -40,6 +32,7 @@ class Trophy(models.Model):
TYPE_COUNT = 'count'
TYPE_SEQUENCE = 'sequence'
TYPE_DATE = 'date'
+ TYPE_PR = 'pr'
TYPE_OTHER = 'other'
TROPHY_TYPES = (
@@ -48,6 +41,7 @@ class Trophy(models.Model):
(TYPE_COUNT, _('Count-based')),
(TYPE_SEQUENCE, _('Sequence-based')),
(TYPE_DATE, _('Date-based')),
+ (TYPE_PR, _('Personal Record')),
(TYPE_OTHER, _('Other')),
)
@@ -56,14 +50,14 @@ class Trophy(models.Model):
editable=False,
unique=True,
)
- """Unique identifier for the trophy"""
+ """Unique identifier for the trophy (also used for image filenames)"""
name = models.CharField(
max_length=100,
verbose_name=_('Name'),
help_text=_('The name of the trophy'),
)
- """The name of the trophy"""
+ """The user-facing name of the trophy"""
description = models.TextField(
verbose_name=_('Description'),
@@ -73,14 +67,6 @@ class Trophy(models.Model):
)
"""Description of the trophy and how to earn it"""
- image = models.ImageField(
- verbose_name=_('Image'),
- upload_to=trophy_image_upload_path,
- blank=True,
- null=True,
- )
- """Optional image for the trophy"""
-
trophy_type = models.CharField(
max_length=20,
choices=TROPHY_TYPES,
@@ -165,3 +151,10 @@ class Trophy(models.Model):
Trophies don't have an owner - they are global
"""
return None
+
+ @property
+ def image_rel_path(self):
+ """
+ Returns the relative path to the trophy image
+ """
+ return f'trophies/{self.trophy_type}/{self.uuid}.png'