### Start wger Demo Docker Container Source: https://github.com/wger-project/wger/blob/master/extras/docker/demo/README.md Starts a stopped Docker container named 'wger.demo' and attaches to its output. ```docker sudo docker container start --attach wger.demo ``` -------------------------------- ### Get Progress for All Trophies (Authenticated) Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Example API call to retrieve the progress for all trophies for the currently authenticated user. ```python # Get progress for all trophies (authenticated) GET /api/v2/trophy/progress/ ``` -------------------------------- ### Get All Volume-Based Trophies Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Example API call to retrieve all trophies of type 'volume'. ```python # Get all volume-based trophies GET /api/v2/trophy/?trophy_type=volume ``` -------------------------------- ### Run wger Demo Docker Image Source: https://github.com/wger-project/wger/blob/master/extras/docker/demo/README.md Starts a new Docker container for the wger demo. Access the application at http://localhost:8000 with username 'admin' and password 'adminadmin'. ```docker docker run -ti --name wger.demo --publish 8000:80 wger/demo ``` -------------------------------- ### API Key Authentication Example (curl) Source: https://github.com/wger-project/wger/blob/master/wger/core/templates/user/api_key.html Use this command to authenticate API requests using your static API key. Replace the placeholder token with your actual key. ```bash curl -X GET https://wger.de/api/v2/routine/ \ -H 'Authorization: Token {% if token %}{{ token.key }}{% else %}1234567890abcde...{% endif %}' ``` -------------------------------- ### Import Data into MongoDB using Docker Compose Source: https://github.com/wger-project/wger/blob/master/extras/open-food-facts/README.md Start the MongoDB container and then use mongorestore to import the products data. This is a manual step due to the import duration. ```shell docker compose up docker compose exec mongodb mongorestore --username off --password off-wger -d admin -c products /dump/off/products.bson ``` -------------------------------- ### Get Current User's Statistics Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Example API call to retrieve the trophy statistics for the current user. ```python # Get current user's statistics GET /api/v2/user-statistics/ ``` -------------------------------- ### User Statistics Service Examples Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Demonstrates how to use the UserStatisticsService to update user workout statistics. Use increment_workout for real-time updates after a workout, update_statistics for full recalculations, and get_or_create_statistics to ensure a statistics record exists. ```python from wger.trophies.services.statistics import UserStatisticsService from decimal import Decimal import datetime # Increment after workout UserStatisticsService.increment_workout( user=request.user, workout_date=datetime.date.today(), weight_lifted=Decimal('150.5') ) # Recalculate all statistics stats = UserStatisticsService.update_statistics(request.user) # Get statistics (create if missing) stats = UserStatisticsService.get_or_create_statistics(request.user) ``` -------------------------------- ### Create Trophy Fixture in JSON Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Example of a JSON fixture file for creating a Trophy object, suitable for loading with Django's loaddata command. ```json [ { "model": "trophies.trophy", "pk": 10, "fields": { "name": "My Trophy", "description": "Description", "trophy_type": "count", "checker_class": "count_based", "checker_params": {"count": 100}, "is_hidden": false, "is_progressive": true, "is_active": true, "order": 100 } } ] ``` -------------------------------- ### Trophy Service Examples Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Illustrates how to interact with the TrophyService for evaluating and awarding trophies. Use evaluate_all_trophies to check for new trophies, get_user_trophies to retrieve earned trophies, and get_all_trophy_progress to view progress across all trophies. reevaluate_trophies can be used for batch processing. ```python from wger.trophies.services.trophy import TrophyService # Evaluate all trophies for user newly_awarded = TrophyService.evaluate_all_trophies(request.user) for user_trophy in newly_awarded: print(f"Earned: {user_trophy.trophy.name}") # Get user's earned trophies earned = TrophyService.get_user_trophies(request.user) # Get progress for all trophies progress = TrophyService.get_all_trophy_progress(request.user) for item in progress: print(f"{item['trophy'].name}: {item['progress']}%)" # Batch re-evaluate for all active users results = TrophyService.reevaluate_trophies() print(f"Checked {results['users_checked']} users") print(f"Awarded {results['trophies_awarded']} trophies") ``` -------------------------------- ### Register Custom Trophy Checker Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Example of registering a custom trophy checker class in the CheckerRegistry. ```python from .my_checker import MyCustomChecker class CheckerRegistry: _registry: Dict[str, Type[BaseTrophyChecker]] = { # ... existing checkers ... 'my_custom': MyCustomChecker, } ``` -------------------------------- ### Create a Custom Trophy Checker Class Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Example of creating a new custom trophy checker class by inheriting from BaseTrophyChecker and implementing check, get_progress, get_current_value, and get_target_value methods. ```python # wger/trophies/checkers/my_checker.py from .base import BaseTrophyChecker class MyCustomChecker(BaseTrophyChecker): def check(self) -> bool: # Your logic here return True def get_progress(self) -> float: # Calculate progress 0-100 return 50.0 def get_current_value(self): return "current" def get_target_value(self): return "target" ``` -------------------------------- ### Add New Trophy Using Existing Checker Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Example of creating a new Trophy object using an existing checker class, such as 'volume'. ```python Trophy.objects.create( name="Heavy Lifter", description="Lift 10,000 kg total", trophy_type=Trophy.TYPE_VOLUME, checker_class='volume', checker_params={'kg': 10000}, is_progressive=True, order=50, ) ``` -------------------------------- ### Add New Trophy via Django Shell Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Example of creating a new Trophy object with a custom checker class using the Django ORM. ```python from wger.trophies.models import Trophy Trophy.objects.create( name="My Custom Trophy", description="Description of how to earn it", trophy_type=Trophy.TYPE_OTHER, checker_class='my_custom', checker_params={'param1': 'value1'}, is_hidden=False, is_progressive=True, order=100, ) ``` -------------------------------- ### Get progress for all trophies Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Retrieves the progress for all trophies for the current user. This includes earned status, progress percentage, and current/target values for progressive trophies. ```APIDOC ## GET /api/v2/trophy/progress/ ### Description Get progress for all trophies (current user). Returns earned status and progress percentage. Includes current/target values for progressive trophies. ### Method GET ### Endpoint /api/v2/trophy/progress/ ``` -------------------------------- ### Get Trophies Earned in a Specific Year Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Example API call to retrieve trophies earned by the user in the year 2024. ```python # Get trophies earned in 2024 GET /api/v2/user-trophy/?earned_at__year=2024 ``` -------------------------------- ### Build wger Demo Docker Image Source: https://github.com/wger-project/wger/blob/master/extras/docker/demo/README.md Builds the wger demo Docker image. Ensure you are in the project root directory before running this command. ```docker docker build -f extras/docker/demo/Dockerfile --tag wger/demo . ``` -------------------------------- ### Get Trophy Progress API Endpoint Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md API endpoint to get the progress for all trophies for the current user. Returns earned status and progress percentage, including current/target values for progressive trophies. ```http GET /api/v2/trophy/progress/ ``` -------------------------------- ### Download Open Food Facts Database Dump Source: https://github.com/wger-project/wger/blob/master/extras/open-food-facts/README.md Use wget to download the compressed database dump. Ensure you are in the 'dump' directory before running. ```shell cd dump wget https://static.openfoodfacts.org/data/openfoodfacts-mongodbdump.tar.gz tar xzvf openfoodfacts-mongodbdump.tar.gz ``` -------------------------------- ### Get specific trophy details Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Retrieves detailed information about a specific trophy by its ID. ```APIDOC ## GET /api/v2/trophy/{id}/ ### Description Get specific trophy details. ### Method GET ### Endpoint /api/v2/trophy/{id}/ ``` -------------------------------- ### Load Initial Trophies Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Use this command to load the default set of trophies into the system. ```bash python manage.py loaddata initial_trophies ``` -------------------------------- ### Load Initial Trophies Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Use this management command to load the predefined set of trophies into the database. This command can overwrite existing trophies if their IDs match. ```bash # Load trophies (overwrites existing ones if IDs match) python manage.py loaddata initial_trophies ``` -------------------------------- ### Filter Trophies by Type and Status Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Example usage of the TrophyFilterSet to filter trophies by type and active status. ```http /api/v2/trophy/?trophy_type=volume&is_active=true ``` -------------------------------- ### Run Author Generation Script Source: https://github.com/wger-project/wger/blob/master/extras/authors/README.md Execute the Python script to generate the author list. Consider using a GitHub token for frequent runs to avoid rate limits. ```bash uv run generate_authors_api.py ``` -------------------------------- ### Get current user's trophy statistics Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Retrieves the overall trophy statistics for the current user. ```APIDOC ## GET /api/v2/user-statistics/ ### Description Get current user's trophy statistics. ### Method GET ### Endpoint /api/v2/user-statistics/ ``` -------------------------------- ### Run wger Import Script Source: https://github.com/wger-project/wger/blob/master/extras/open-food-facts/README.md Execute the Python management script to import Open Food Facts products into wger. ```shell python manage.py import-off-products ``` -------------------------------- ### Get User Statistics API Endpoint Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md API endpoint to retrieve the current user's trophy statistics. ```http GET /api/v2/user-statistics/ ``` -------------------------------- ### Get Specific Trophy Details API Endpoint Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md API endpoint to retrieve details for a specific trophy by its ID. ```http GET /api/v2/trophy/{id}/ ``` -------------------------------- ### Update Data Fixtures and Zip Files Source: https://github.com/wger-project/wger/blob/master/extras/open-food-facts/README.md Generate updated data fixtures, filter them, and then zip the resulting ingredient, weight unit, and ingredient unit files. ```shell python manage.py dumpdata nutrition > extras/scripts/data.json cd extras/scripts python filter-fixtures.py zip ingredients.json.zip ingredients.json zip weight_units.json.zip weight_units.json zip ingredient_units.json.zip ingredient_units.json ``` -------------------------------- ### Get specific earned trophy details Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Retrieves detailed information about a specific trophy that has been earned by the current user. ```APIDOC ## GET /api/v2/user-trophy/{id}/ ### Description Get specific earned trophy details. ### Method GET ### Endpoint /api/v2/user-trophy/{id}/ ``` -------------------------------- ### Get Specific Earned Trophy Details API Endpoint Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md API endpoint to retrieve details for a specific earned trophy by its ID. ```http GET /api/v2/user-trophy/{id}/ ``` -------------------------------- ### Running Trophy Tests Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Provides the command to execute the trophy system's test suite. This is essential for verifying the functionality of the trophy and statistics services. ```bash # All trophy tests python manage.py test wger.trophies.tests ``` -------------------------------- ### Filter User Trophies by Date Range Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Example usage of the UserTrophyFilterSet to filter user's earned trophies by a date range. ```http /api/v2/user-trophy/?trophy=5&earned_at__gte=2024-01-01 ``` -------------------------------- ### Configure Trophy System Settings Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Enable or disable the trophy system globally and set inactivity thresholds for user evaluation. Add these settings to your project's settings file. ```python WGER_SETTINGS = { # Enable/disable the trophy system globally 'TROPHIES_ENABLED': True, # Number of days of inactivity before skipping trophy evaluation for a user 'TROPHIES_INACTIVE_USER_DAYS': 30, } ``` -------------------------------- ### Django Template Loops and Conditionals Source: https://github.com/wger-project/wger/blob/master/wger/gym/templates/gym/partial_user_list.html Demonstrates Django template tags for iterating through user data and conditionally displaying gym information. Includes a loop for keys, a loop for users, and conditional logic to show gym details if available. ```django {% for key in user_table.keys %}{% endfor %} ``` ```django {% for current_user in user_table.users %}{% if show_gym %}{% endif %}{% endfor %} ``` ```django {{ key }} ``` ```django {{current_user.obj.pk}} ``` ```django [{{current_user.obj}}]({% url 'core:user:overview' current_user.obj.pk %}) ``` ```django {{current_user.obj.get_full_name}} ``` ```django {{current_user.last_log|default:'-/-'}} ``` ```django {% if current_user.obj.userprofile.gym_id %} [{{ current_user.obj.userprofile.gym}}]({{ current_user.obj.userprofile.gym.get_absolute_url }}) {% else %} -/- {% endif %} ``` -------------------------------- ### Load Fixture Data Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Command to load fixture data (e.g., JSON files) into the Django application. ```bash python manage.py loaddata my_trophies ``` -------------------------------- ### Clean Up Docker Containers and Dump Files Source: https://github.com/wger-project/wger/blob/master/extras/open-food-facts/README.md Stop and remove Docker containers, and delete the downloaded dump files to free up disk space. ```shell docker compose down rm dump -r openfoodfacts-mongodbdump.tar.gz dump/dump ``` -------------------------------- ### Recalculate User Statistics Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Rebuild denormalized user statistics from workout history. This is useful for ensuring accurate trophy evaluation, especially after data changes. ```bash # Recalculate for a specific user python manage.py recalculate_statistics --user username # Recalculate for all users python manage.py recalculate_statistics --all ``` -------------------------------- ### Recalculate All Statistics Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Manually recalculate statistics for all users. Use this if signals are not connected or statistics are not updating. ```bash python manage.py recalculate_statistics --all ``` -------------------------------- ### Run Trophy Tests Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Execute tests for different modules within the wger trophies system. ```bash python manage.py test wger.trophies.tests.test_models ``` ```bash python manage.py test wger.trophies.tests.test_checkers ``` ```bash python manage.py test wger.trophies.tests.test_services ``` ```bash python manage.py test wger.trophies.tests.test_api ``` ```bash python manage.py test wger.trophies.tests.test_integration ``` -------------------------------- ### UserStatisticsService Source: https://github.com/wger-project/wger/blob/master/wger/trophies/README.md Service for managing user workout statistics. Provides methods to update, retrieve, and recalculate statistics based on workout data. ```APIDOC ## UserStatisticsService Service for managing user workout statistics. ### Methods: - `increment_workout(user, workout_date, weight_lifted)`: Incrementally update statistics after a workout. Updates streak, total workouts, weight lifted, and workout times. - `update_statistics(user)`: Recalculate all statistics from scratch by scanning workout history. Use for fixing inconsistencies. - `get_or_create_statistics(user)`: Get existing statistics or create new record with default values. - `handle_workout_deletion(user, workout_date)`: Update statistics after a workout is deleted. Recalculates streaks if needed. ### Example Usage: ```python from wger.trophies.services.statistics import UserStatisticsService from decimal import Decimal import datetime # Increment after workout UserStatisticsService.increment_workout( user=request.user, workout_date=datetime.date.today(), weight_lifted=Decimal('150.5') ) # Recalculate all statistics stats = UserStatisticsService.update_statistics(request.user) # Get statistics (create if missing) stats = UserStatisticsService.get_or_create_statistics(request.user) ``` ```