Skip to content

Commit

Permalink
feat(make-translation-easier) Make translations of exercises and ingr…
Browse files Browse the repository at this point in the history
…edients easier

This commit makes translations of exercises and ingredients easier

Before, the lists of exercises and ingredients are displayed
according to the user preferred language, so this commit adds
options for a user to filter lists of exercises and ingredients
according to the various language they are created in.

[Delivers #157731616]
  • Loading branch information
hariclerry committed Jun 26, 2018
1 parent b4a29cc commit 052592a
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 9 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: 300px; font-size: 20px">View exercise 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' %}>Select</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">
{% for item in exercise_list %}
Expand Down
2 changes: 1 addition & 1 deletion wger/exercises/tests/test_exercise.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,4 +552,4 @@ def test_exercise_read_only(self):
client = APIClient()
response = client.post('/api/v2/exercises/', data=data)
# Test for method not allowed
self.assertEqual(response.status_code, 405)
self.assertEqual(response.status_code, 405)
16 changes: 16 additions & 0 deletions wger/exercises/views/exercises.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
)
from wger.config.models import LanguageConfig
from wger.weight.helpers import process_log_entries
from wger.core.models import Language


logger = logging.getLogger(__name__)
Expand All @@ -78,6 +79,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 @@ -89,6 +104,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 ingredient 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' %}>Select</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 052592a

Please sign in to comment.