Merge lp:~daker/loco-team-portal/ld-languages-menu into lp:loco-team-portal

Proposed by Adnane Belmadiaf
Status: Rejected
Rejected by: Adnane Belmadiaf
Proposed branch: lp:~daker/loco-team-portal/ld-languages-menu
Merge into: lp:loco-team-portal
Diff against target: 124 lines (+44/-7)
5 files modified
loco_directory/common/context_processors.py (+7/-0)
loco_directory/common/utils.py (+30/-3)
loco_directory/common/views.py (+4/-2)
loco_directory/settings.py (+1/-0)
loco_directory/templates/base.html (+2/-2)
To merge this branch: bzr merge lp:~daker/loco-team-portal/ld-languages-menu
Reviewer Review Type Date Requested Status
Michael Hall (community) Needs Fixing
Daniel Holbach (community) Needs Information
Review via email: mp+42760@code.launchpad.net
To post a comment you must log in.
339. By Adnane Belmadiaf

* Removed the print statement

Revision history for this message
Daniel Holbach (dholbach) wrote :

Thanks a lot for your work on this.

A few things:
 - In loco_directory/common/utils.py sys and gettext are unnecessarily imported.
 - How often is check_for_language in loco_directory/common/utils.py executed? I get the feeling that it's even for a simple page called quite often (I just added a debug print message.) and every time it reads a bunch of directories and files. Is there a way how we can make this run less often?

review: Needs Information
340. By Adnane Belmadiaf

* Removed unused gettext import

Revision history for this message
Adnane Belmadiaf (daker) wrote :

1 - i removed the gettext import
2 - i can't figure out what's the best way to do it? maybe creating a cache array when you run the init-ld command, then we have just to look if the language code is in this array instead of reading all the directories in "locale".

Revision history for this message
Michael Hall (mhall119) wrote :

Put the 'languages' dict in utils.py outside of any function def, set it to None by default, then a method to return it, populating it if it's not already there, then call that method from the context processor:

loco_directory/common/utils.py

languages = None
def get_languages():
    """
    Return a language list.
    """
    if languages is None:
        languages = {}
        Languages = Language.objects.order_by('name')
        for language in Languages:
           if check_for_language(language.code):
                languages[language.code] = language.name
        languages = sorted([(value,key) for (key,value) in languages.items()])
    return languages

loco_directory/common/context_processors.py

import common.utils
def language_menu(request):
    return {'languages': common.utils.get_languages()}

341. By Adnane Belmadiaf

* Refactoring code to reduce the number of calls

Revision history for this message
Michael Hall (mhall119) wrote :

08:35 < mhall119> hmmm, it doesn't seem to let me change language
08:35 < mhall119> the menu stays on "Arabic" but the language is still English
08:35 < mhall119> and English (United States) isn't an option

review: Needs Fixing

Unmerged revisions

341. By Adnane Belmadiaf

* Refactoring code to reduce the number of calls

340. By Adnane Belmadiaf

* Removed unused gettext import

339. By Adnane Belmadiaf

* Removed the print statement

338. By Adnane Belmadiaf

* Fixed bug 648296
* Fixed bug 648293

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'loco_directory/common/context_processors.py'
--- loco_directory/common/context_processors.py 2010-11-20 17:25:50 +0000
+++ loco_directory/common/context_processors.py 2010-12-07 16:30:21 +0000
@@ -3,6 +3,7 @@
3# to use this processor, add "common.context_processors.loco_version" to your variable TEMPLATE_CONTEXT_PROCESSORS in your settings file3# to use this processor, add "common.context_processors.loco_version" to your variable TEMPLATE_CONTEXT_PROCESSORS in your settings file
44
5from django.conf import settings5from django.conf import settings
6from utils import get_languages
67
7def loco_version(request):8def loco_version(request):
8 """9 """
@@ -48,3 +49,9 @@
48 from common.forms import SiteSearchForm49 from common.forms import SiteSearchForm
49 search_form = SiteSearchForm(data=request.GET)50 search_form = SiteSearchForm(data=request.GET)
50 return {'search_form': search_form}51 return {'search_form': search_form}
52
53def language_menu(request):
54 """
55 Return the language list.
56 """
57 return {'languages': get_languages()}
5158
=== modified file 'loco_directory/common/utils.py'
--- loco_directory/common/utils.py 2010-10-21 16:27:24 +0000
+++ loco_directory/common/utils.py 2010-12-07 16:30:21 +0000
@@ -1,10 +1,10 @@
1from django.conf import settings
1import email2import email
2import os3import os
3from copy import deepcopy
4import subprocess4import subprocess
5import datetime5import datetime
66import sys
7from django.conf import settings7from copy import deepcopy
88
9def write_log(job_name, log):9def write_log(job_name, log):
10 stamp_dir = os.path.join(settings.PROJECT_PATH, 'data')10 stamp_dir = os.path.join(settings.PROJECT_PATH, 'data')
@@ -102,4 +102,31 @@
102 self.index = -1102 self.index = -1
103 return ''103 return ''
104104
105def check_for_language(lang_code):
106 """
107 Checks whether there is a global language file for the given language code.
108 """
109 globalpath = os.path.join(settings.PROJECT_PATH, 'locale')
110 for dirnames in os.listdir(globalpath):
111 if dirnames == lang_code:
112 for dirpath_local, dirnames_local, filenames in os.walk(os.path.join(globalpath, dirnames, 'LC_MESSAGES')):
113 if 'django.mo' in filenames:
114 return True
115 else:
116 return False
105117
118languages = None
119def get_languages():
120 """
121 Return a language list.
122 """
123 global languages
124 if languages is None:
125 from teams.models import Language
126 languages = {}
127 Languages = Language.objects.order_by('name')
128 for language in Languages:
129 if check_for_language(language.code):
130 languages[language.code] = language.name
131 languages = sorted([(value,key) for (key,value) in languages.items()])
132 return languages
106133
=== modified file 'loco_directory/common/views.py'
--- loco_directory/common/views.py 2010-11-30 12:54:39 +0000
+++ loco_directory/common/views.py 2010-12-07 16:30:21 +0000
@@ -3,9 +3,10 @@
3from django.shortcuts import render_to_response3from django.shortcuts import render_to_response
4from django.template import RequestContext4from django.template import RequestContext
5from django.contrib.auth import logout5from django.contrib.auth import logout
6from django.utils.translation import check_for_language
7from django.conf import settings6from django.conf import settings
87
8from utils import check_for_language
9
9def index(request):10def index(request):
10 from events.models import GlobalEvent, TeamEvent11 from events.models import GlobalEvent, TeamEvent
11 team_event_count = TeamEvent.objects.next_events().count()12 team_event_count = TeamEvent.objects.next_events().count()
@@ -58,7 +59,8 @@
58 if check_for_language(lang):59 if check_for_language(lang):
59 if hasattr(request, 'session'):60 if hasattr(request, 'session'):
60 request.session['django_language'] = lang61 request.session['django_language'] = lang
61 response.set_cookie(settings.LANGUAGE_COOKIE_NAME,lang)62 else:
63 response.set_cookie(settings.LANGUAGE_COOKIE_NAME,lang)
62 return response64 return response
63 65
64def site_search(request):66def site_search(request):
6567
=== modified file 'loco_directory/settings.py'
--- loco_directory/settings.py 2010-12-06 16:38:37 +0000
+++ loco_directory/settings.py 2010-12-07 16:30:21 +0000
@@ -93,6 +93,7 @@
93 "common.context_processors.login_redirect",93 "common.context_processors.login_redirect",
94 "common.context_processors.url_base",94 "common.context_processors.url_base",
95 "common.context_processors.site_search",95 "common.context_processors.site_search",
96 "common.context_processors.language_menu",
96)97)
9798
98ROOT_URLCONF = 'loco_directory.urls'99ROOT_URLCONF = 'loco_directory.urls'
99100
=== modified file 'loco_directory/templates/base.html'
--- loco_directory/templates/base.html 2010-11-20 17:25:50 +0000
+++ loco_directory/templates/base.html 2010-12-07 16:30:21 +0000
@@ -54,8 +54,8 @@
54 <form name="lang-switcher" style="text-align:right;" action="/language/" method="get">54 <form name="lang-switcher" style="text-align:right;" action="/language/" method="get">
55 <input name="next" type="hidden" value="{{ request.path }}" />55 <input name="next" type="hidden" value="{{ request.path }}" />
56 <select name="lang" onchange="document.forms['lang-switcher'].submit()">56 <select name="lang" onchange="document.forms['lang-switcher'].submit()">
57 {% for lang in LANGUAGES %}57 {% for lang in languages %}
58 <option value="{{ lang.0 }}"{% ifequal LANGUAGE_CODE lang.0 %} selected="selected"{% endifequal %}>{{ lang.1 }}</option>58 <option value="{{ lang.1 }}"{% ifequal LANGUAGE_CODE lang.1 %} selected="selected"{% endifequal %}>{{ lang.0 }}</option>
59 {% endfor %}59 {% endfor %}
60 </select>60 </select>
61 </form>61 </form>

Subscribers

People subscribed via source and target branches