Self hosted FLOSS fitness/workout, nutrition and weight tracker
Make fetching github stats more robust
Roland Geider3b58d398a28d
diff --git a/wger/software/views.py b/wger/software/views.pyindex 88e198d19..bcdac4fe1 100644--- a/wger/software/views.py+++ b/wger/software/views.py@@ -38,21 +38,37 @@ logger = logging.getLogger(__name__)CACHE_KEY = 'landing-page-context'+def fetch_github_stats() -> dict:+ context = cache.get(CACHE_KEY)+ if context:+ return context++ context = {+ 'nr_users': 1,+ 'nr_exercises': 1,+ 'nr_ingredients': 1,+ 'nr_stars': 1,+ }++ try:+ result_github_api = requests.get('https://api.github.com/repos/wger-project/wger').json()+ context['nr_users'] = User.objects.count()+ context['nr_exercises'] = Exercise.objects.count()+ context['nr_ingredients'] = Ingredient.objects.count()+ context['nr_stars'] = result_github_api.get('stargazers_count', '2000')+ cache.set(CACHE_KEY, context, 60 * 60 * 24 * 7) # one week+ except Exception as e:+ logger.error(f'Error fetching github stats: {e}')++ return context++def features(request):"""Render the landing page"""- context = cache.get(CACHE_KEY)- if not context:- result_github_api = requests.get('https://api.github.com/repos/wger-project/wger').json()- context = {- 'nr_users': User.objects.count(),- 'nr_exercises': Exercise.objects.count(),- 'nr_ingredients': Ingredient.objects.count(),- 'nr_stars': result_github_api.get('stargazers_count', '2000'),- }- cache.set(CACHE_KEY, context, 60 * 60 * 24 * 7) # one week+ context = fetch_github_stats()FormClass = (RegistrationForm if settings.WGER_SETTINGS['USE_RECAPTCHA'] else RegistrationFormNoCaptchadiff --git a/wger/utils/tests/test_middleware.py b/wger/utils/tests/test_middleware.pyindex 0ad6555f8..64f0bec24 100644--- a/wger/utils/tests/test_middleware.py+++ b/wger/utils/tests/test_middleware.py@@ -12,6 +12,13 @@## You should have received a copy of the GNU Affero General Public License++# Standard Library+from unittest.mock import (+ MagicMock,+ patch,+)+# Djangofrom django.urls import reverse@@ -38,10 +45,17 @@ class RobotsExclusionMiddlewareTestCase(WgerTestCase):response = self.client.get(reverse('core:imprint'))self.assertFalse(response.get('X-Robots-Tag'))- def test_middleware_software(self):+ @patch('wger.software.views.fetch_github_stats', autospec=True)+ def test_middleware_software(self, mock_fetch: MagicMock):"""Test the middleware on URLs from software app"""+ mock_fetch.return_value = {+ 'nr_users': 1,+ 'nr_exercises': 1,+ 'nr_ingredients': 1,+ 'nr_stars': 1,+ }for i in ('features', 'tos', 'about-us'):response = self.client.get(reverse(f'software:{i}'))