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
1=== modified file 'loco_directory/common/context_processors.py'
2--- loco_directory/common/context_processors.py 2010-11-20 17:25:50 +0000
3+++ loco_directory/common/context_processors.py 2010-12-07 16:30:21 +0000
4@@ -3,6 +3,7 @@
5 # to use this processor, add "common.context_processors.loco_version" to your variable TEMPLATE_CONTEXT_PROCESSORS in your settings file
6
7 from django.conf import settings
8+from utils import get_languages
9
10 def loco_version(request):
11 """
12@@ -48,3 +49,9 @@
13 from common.forms import SiteSearchForm
14 search_form = SiteSearchForm(data=request.GET)
15 return {'search_form': search_form}
16+
17+def language_menu(request):
18+ """
19+ Return the language list.
20+ """
21+ return {'languages': get_languages()}
22
23=== modified file 'loco_directory/common/utils.py'
24--- loco_directory/common/utils.py 2010-10-21 16:27:24 +0000
25+++ loco_directory/common/utils.py 2010-12-07 16:30:21 +0000
26@@ -1,10 +1,10 @@
27+from django.conf import settings
28 import email
29 import os
30-from copy import deepcopy
31 import subprocess
32 import datetime
33-
34-from django.conf import settings
35+import sys
36+from copy import deepcopy
37
38 def write_log(job_name, log):
39 stamp_dir = os.path.join(settings.PROJECT_PATH, 'data')
40@@ -102,4 +102,31 @@
41 self.index = -1
42 return ''
43
44+def check_for_language(lang_code):
45+ """
46+ Checks whether there is a global language file for the given language code.
47+ """
48+ globalpath = os.path.join(settings.PROJECT_PATH, 'locale')
49+ for dirnames in os.listdir(globalpath):
50+ if dirnames == lang_code:
51+ for dirpath_local, dirnames_local, filenames in os.walk(os.path.join(globalpath, dirnames, 'LC_MESSAGES')):
52+ if 'django.mo' in filenames:
53+ return True
54+ else:
55+ return False
56
57+languages = None
58+def get_languages():
59+ """
60+ Return a language list.
61+ """
62+ global languages
63+ if languages is None:
64+ from teams.models import Language
65+ languages = {}
66+ Languages = Language.objects.order_by('name')
67+ for language in Languages:
68+ if check_for_language(language.code):
69+ languages[language.code] = language.name
70+ languages = sorted([(value,key) for (key,value) in languages.items()])
71+ return languages
72
73=== modified file 'loco_directory/common/views.py'
74--- loco_directory/common/views.py 2010-11-30 12:54:39 +0000
75+++ loco_directory/common/views.py 2010-12-07 16:30:21 +0000
76@@ -3,9 +3,10 @@
77 from django.shortcuts import render_to_response
78 from django.template import RequestContext
79 from django.contrib.auth import logout
80-from django.utils.translation import check_for_language
81 from django.conf import settings
82
83+from utils import check_for_language
84+
85 def index(request):
86 from events.models import GlobalEvent, TeamEvent
87 team_event_count = TeamEvent.objects.next_events().count()
88@@ -58,7 +59,8 @@
89 if check_for_language(lang):
90 if hasattr(request, 'session'):
91 request.session['django_language'] = lang
92- response.set_cookie(settings.LANGUAGE_COOKIE_NAME,lang)
93+ else:
94+ response.set_cookie(settings.LANGUAGE_COOKIE_NAME,lang)
95 return response
96
97 def site_search(request):
98
99=== modified file 'loco_directory/settings.py'
100--- loco_directory/settings.py 2010-12-06 16:38:37 +0000
101+++ loco_directory/settings.py 2010-12-07 16:30:21 +0000
102@@ -93,6 +93,7 @@
103 "common.context_processors.login_redirect",
104 "common.context_processors.url_base",
105 "common.context_processors.site_search",
106+ "common.context_processors.language_menu",
107 )
108
109 ROOT_URLCONF = 'loco_directory.urls'
110
111=== modified file 'loco_directory/templates/base.html'
112--- loco_directory/templates/base.html 2010-11-20 17:25:50 +0000
113+++ loco_directory/templates/base.html 2010-12-07 16:30:21 +0000
114@@ -54,8 +54,8 @@
115 <form name="lang-switcher" style="text-align:right;" action="/language/" method="get">
116 <input name="next" type="hidden" value="{{ request.path }}" />
117 <select name="lang" onchange="document.forms['lang-switcher'].submit()">
118- {% for lang in LANGUAGES %}
119- <option value="{{ lang.0 }}"{% ifequal LANGUAGE_CODE lang.0 %} selected="selected"{% endifequal %}>{{ lang.1 }}</option>
120+ {% for lang in languages %}
121+ <option value="{{ lang.1 }}"{% ifequal LANGUAGE_CODE lang.1 %} selected="selected"{% endifequal %}>{{ lang.0 }}</option>
122 {% endfor %}
123 </select>
124 </form>

Subscribers

People subscribed via source and target branches