Skip to content

Commit

Permalink
Merge d9845be into 721dc10
Browse files Browse the repository at this point in the history
  • Loading branch information
hariclerry committed Jun 28, 2018
2 parents 721dc10 + d9845be commit d93c0cf
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 8 deletions.
36 changes: 34 additions & 2 deletions wger/exercises/templates/exercise/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,39 @@
<!--
Title
-->
{% block title %}{% trans "Exercises" %}{% endblock %}

{% block title %}{% trans "Exercises" %}
<span style="margin-left: 260px; font-size: 20px">View exercises by language:</span>
<div class="btn-group" style="float:right; display: inline-block;">
<button id="filter-lang" type="button" class="btn btn-primary dropdown-toggle btn-xs" data-toggle="dropdown" style="padding: 5px">
{% if active_lang %}
{{active_lang.full_name}}
{% else %}
Select
{% endif %}
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href={% url 'exercise:exercise:overview' %}></a></li>
{% for short_name, full_name in languages %}
<li style="margin-left: 15px;">
<img src="/static/images/icons/flag-{{ short_name }}.svg"
width="18"
height="11"
alt="{% trans full_name %}"
title="{% trans full_name %}"
style="border: 1px solid #151515;">
<span>
<a href="{% url 'exercise:exercise:overview' %}?lang={{ short_name }}">
{% trans full_name %}
</a>
</span>
<li class="divider"></li>
</li>
{% endfor %}
</ul>
</div>
{% endblock %}



Expand Down Expand Up @@ -38,7 +70,7 @@
-->
{% block content %}

{% cache cache_timeout exercise-overview language.id %}
{% cache 1 exercise-overview language.id %}
{% regroup exercises by category as exercise_list %}

<ul class="nav nav-tabs">
Expand Down
12 changes: 12 additions & 0 deletions wger/exercises/tests/test_exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,18 @@ def test_canonical_form_cache_delete(self):
for workout_id in workout_ids:
self.assertFalse(cache.get(cache_mapper.get_workout_canonical(workout_id)))

def test_exercise_language_filter_works(self):
"""
Test exercise filter language works
"""
self.user_login()
response = self.client.get(
reverse('exercise:exercise:overview'), {'active_lang': 'en'})
self.assertEqual(5, len(response.context['exercises']))
response = self.client.get(
reverse('exercise:exercise:overview'), {'active_lang': 'de'})
self.assertEqual(5, len(response.context['exercises']))


class ExerciseApiTest(WorkoutManagerTestCase):
def test_exercise_read_only_endpoint(self):
Expand Down
16 changes: 16 additions & 0 deletions wger/exercises/views/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
from wger.weight.helpers import process_log_entries
from wger.core.views.user import fetch_exercise_data
from wger.utils.helpers import check_access
from wger.core.models import Language


logger = logging.getLogger(__name__)
Expand All @@ -80,6 +81,20 @@ def get_queryset(self):
Filter to only active exercises in the configured languages
'''
languages = load_item_languages(LanguageConfig.SHOW_ITEM_EXERCISES)
query_language = self.request.GET.get('lang', None)
language = None
self.active_lang = None

if query_language:
lang = Language.objects.filter(short_name=query_language)
if lang.exists():
self.active_lang = lang.first()
language = self.active_lang.id
if language:
return Exercise.objects.accepted() \
.filter(language=language) \
.order_by('category__id') \
.select_related()
return Exercise.objects.accepted() \
.filter(language__in=languages) \
.order_by('category__id') \
Expand All @@ -91,6 +106,7 @@ def get_context_data(self, **kwargs):
'''
context = super(ExerciseListView, self).get_context_data(**kwargs)
context['show_shariff'] = True
context['active_lang'] = self.active_lang
return context


Expand Down
34 changes: 33 additions & 1 deletion wger/nutrition/templates/ingredient/overview.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,39 @@
</script>
{% endblock %}

{% block title %}{% trans "Ingredient overview" %}{% endblock %}
{% block title %}{% trans "Ingredient overview" %}
<span style="margin-left: 100px; font-size: 20px">View ingredients by language:</span>
<div class="btn-group" style="float:right;">
<button id="filter-lang" type="button" class="btn btn-primary dropdown-toggle btn-xs" data-toggle="dropdown" style="padding: 5px">
{% if active_lang %}
{{active_lang.full_name}}
{% else %}
Select
{% endif %}
<span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href={% url 'nutrition:ingredient:list' %}></a></li>
{% for short_name, full_name in languages %}
<li style="margin-left: 15px;">
<img src="/static/images/icons/flag-{{ short_name }}.svg"
width="18"
height="11"
alt="{% trans full_name %}"
title="{% trans full_name %}"
style="border: 1px solid #151515;"
>

<span>
<a href="{% url 'nutrition:ingredient:list' %}?lang={{ short_name }}">
{% trans full_name %}
</a>
</span>
<li class="divider"></li>
{% endfor %}
</ul>
</div>
{% endblock %}


{% block content %}
Expand Down
21 changes: 16 additions & 5 deletions wger/nutrition/views/ingredient.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
from wger.utils.constants import PAGINATION_OBJECTS_PER_PAGE
from wger.utils.language import load_language, load_ingredient_languages
from wger.utils.cache import cache_mapper

from wger.core.models import Language

logger = logging.getLogger(__name__)

Expand All @@ -64,17 +64,28 @@ def get_queryset(self):
(the user can also want to see ingredients in English, in addition to his
native language, see load_ingredient_languages)
'''
languages = load_ingredient_languages(self.request)
return (Ingredient.objects.filter(language__in=languages)
.filter(status__in=Ingredient.INGREDIENT_STATUS_OK)
.only('id', 'name'))
query_language = self.request.GET.get('lang', None)
language = None
self.active_lang = None

if query_language:
lang = Language.objects.filter(short_name=query_language)
if lang.exists():
self.active_lang = lang.first()
language = self.active_lang.id
if language:
return (Ingredient.objects.filter(language=language)
.filter(status__in=Ingredient.INGREDIENT_STATUS_OK).only('id', 'name'))
return (Ingredient.objects.filter(
status__in=Ingredient.INGREDIENT_STATUS_OK).only('id', 'name'))

def get_context_data(self, **kwargs):
'''
Pass additional data to the template
'''
context = super(IngredientListView, self).get_context_data(**kwargs)
context['show_shariff'] = True
context['active_lang'] = self.active_lang
return context


Expand Down

0 comments on commit d93c0cf

Please sign in to comment.