aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--mediagoblin/app.py7
-rw-r--r--mediagoblin/globals.py9
-rw-r--r--mediagoblin/templates/mediagoblin/root.html1
-rw-r--r--mediagoblin/templates/mediagoblin/user_pages/user.html15
-rw-r--r--mediagoblin/tests/test_util.py26
-rw-r--r--mediagoblin/translations/en/LC_MESSAGES/mediagoblin.mobin0 -> 502 bytes
-rw-r--r--mediagoblin/translations/en/LC_MESSAGES/mediagoblin.po (renamed from mediagoblin/translations/master.po)4
-rw-r--r--mediagoblin/user_pages/views.py25
-rw-r--r--mediagoblin/util.py110
9 files changed, 175 insertions, 22 deletions
diff --git a/mediagoblin/app.py b/mediagoblin/app.py
index 2a2f21cc..d124558d 100644
--- a/mediagoblin/app.py
+++ b/mediagoblin/app.py
@@ -40,7 +40,7 @@ class MediaGoblinApp(object):
email_sender_address, email_debug_mode,
user_template_path=None):
# Get the template environment
- self.template_env = util.get_jinja_env(user_template_path)
+ self.template_loader = util.get_jinja_loader(user_template_path)
# Set up storage systems
self.public_store = public_store
@@ -103,7 +103,10 @@ class MediaGoblinApp(object):
# Attach self as request.app
# Also attach a few utilities from request.app for convenience?
request.app = self
- request.template_env = self.template_env
+ request.locale = util.get_locale_from_request(request)
+
+ request.template_env = util.get_jinja_env(
+ self.template_loader, request.locale)
request.db = self.db
request.staticdirect = self.staticdirector
diff --git a/mediagoblin/globals.py b/mediagoblin/globals.py
index 59a94558..80d1f01d 100644
--- a/mediagoblin/globals.py
+++ b/mediagoblin/globals.py
@@ -2,6 +2,9 @@
In some places, we need to access the database, public_store, queue_store
"""
+import gettext
+import pkg_resources
+
#############################
# General mediagoblin globals
#############################
@@ -16,6 +19,12 @@ database = None
public_store = None
queue_store = None
+# gettext
+translations = gettext.find(
+ 'mediagoblin',
+ pkg_resources.resource_filename(
+ 'mediagoblin', 'translations'), ['en'])
+
def setup_globals(**kwargs):
from mediagoblin import globals as mg_globals
diff --git a/mediagoblin/templates/mediagoblin/root.html b/mediagoblin/templates/mediagoblin/root.html
index e833e3fe..fa78bda2 100644
--- a/mediagoblin/templates/mediagoblin/root.html
+++ b/mediagoblin/templates/mediagoblin/root.html
@@ -18,6 +18,7 @@
{% extends "mediagoblin/base.html" %}
{% block mediagoblin_content %}
+
<h2>{% trans %}Welcome to GNU MediaGoblin!{% endtrans %}</h2>
{% if request.user %}
diff --git a/mediagoblin/templates/mediagoblin/user_pages/user.html b/mediagoblin/templates/mediagoblin/user_pages/user.html
index 4fa84430..85f05e08 100644
--- a/mediagoblin/templates/mediagoblin/user_pages/user.html
+++ b/mediagoblin/templates/mediagoblin/user_pages/user.html
@@ -19,8 +19,21 @@
{% block mediagoblin_content -%}
{% if user %}
<h2>User page for '{{ user.username }}'</h2>
- {{ user }}
+
+ {#- Should we outsource such a media 'gallery' view to it's own file?
+ It could be useful for the home page and other views too -#}
+ <ul>
+ {%- for entry in media_entries %}
+ <li>
+ <a href="{{ request.urlgen('mediagoblin.user_pages.media_home',
+ user= entry.uploader.username, m_id= entry._id) }}">
+ <img src="{{ request.app.public_store.file_url(
+ entry['media_files']['thumb']) }}" /></a>
+ </li>
+ {%- endfor %}
+ </ul>
{% else %}
+ {# This *should* not occur as the view makes sure we pass in a user. #}
<p>Sorry, no such user found.<p/>
{% endif %}
{% endblock %}
diff --git a/mediagoblin/tests/test_util.py b/mediagoblin/tests/test_util.py
index 5bc31fd6..ff40a677 100644
--- a/mediagoblin/tests/test_util.py
+++ b/mediagoblin/tests/test_util.py
@@ -69,3 +69,29 @@ I hope you like unit tests JUST AS MUCH AS I DO!"""
assert mbox_message.get_payload(decode=True) == """HAYYY GUYS!
I hope you like unit tests JUST AS MUCH AS I DO!"""
+
+
+def test_locale_to_lower_upper():
+ """
+ Test cc.i18n.util.locale_to_lower_upper()
+ """
+ assert util.locale_to_lower_upper('en') == 'en'
+ assert util.locale_to_lower_upper('en_US') == 'en_US'
+ assert util.locale_to_lower_upper('en-us') == 'en_US'
+
+ # crazy renditions. Useful?
+ assert util.locale_to_lower_upper('en-US') == 'en_US'
+ assert util.locale_to_lower_upper('en_us') == 'en_US'
+
+
+def test_locale_to_lower_lower():
+ """
+ Test cc.i18n.util.locale_to_lower_lower()
+ """
+ assert util.locale_to_lower_lower('en') == 'en'
+ assert util.locale_to_lower_lower('en_US') == 'en-us'
+ assert util.locale_to_lower_lower('en-us') == 'en-us'
+
+ # crazy renditions. Useful?
+ assert util.locale_to_lower_lower('en-US') == 'en-us'
+ assert util.locale_to_lower_lower('en_us') == 'en-us'
diff --git a/mediagoblin/translations/en/LC_MESSAGES/mediagoblin.mo b/mediagoblin/translations/en/LC_MESSAGES/mediagoblin.mo
new file mode 100644
index 00000000..fb7046cd
--- /dev/null
+++ b/mediagoblin/translations/en/LC_MESSAGES/mediagoblin.mo
Binary files differ
diff --git a/mediagoblin/translations/master.po b/mediagoblin/translations/en/LC_MESSAGES/mediagoblin.po
index 0e242fee..3bec204e 100644
--- a/mediagoblin/translations/master.po
+++ b/mediagoblin/translations/en/LC_MESSAGES/mediagoblin.po
@@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PROJECT VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
-"POT-Creation-Date: 2011-05-12 10:17-0500\n"
+"POT-Creation-Date: 2011-05-12 22:28-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
@@ -17,7 +17,7 @@ msgstr ""
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 0.9.6\n"
-#: mediagoblin/templates/mediagoblin/root.html:21
+#: mediagoblin/templates/mediagoblin/root.html:22
msgid "Welcome to GNU MediaGoblin!"
msgstr ""
diff --git a/mediagoblin/user_pages/views.py b/mediagoblin/user_pages/views.py
index cc613c40..2c9792fa 100644
--- a/mediagoblin/user_pages/views.py
+++ b/mediagoblin/user_pages/views.py
@@ -14,17 +14,22 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-from webob import Response
+from webob import Response, exc
from mongokit import ObjectId
import wtforms
def user_home(request):
"""'Homepage' of a User()"""
- user = request.db.User.find_one(
- {'username': request.matchdict['user']})
+ user = request.db.User.find_one({
+ 'username': request.matchdict['user'],
+ 'status': 'active'})
+ if not user:
+ return exc.HTTPNotFound()
- medias = request.db.MediaEntry.find()
+ medias = request.db.MediaEntry.find({
+ 'uploader': user,
+ 'state': 'processed'})
template = request.template_env.get_template(
'mediagoblin/user_pages/user.html')
@@ -32,16 +37,18 @@ def user_home(request):
template.render(
{'request': request,
'user': user,
- 'medias': medias}))
+ 'media_entries': medias}))
def media_home(request):
"""'Homepage' of a MediaEntry()"""
- media = request.db.MediaEntry.find_one(
- ObjectId(request.matchdict['m_id']))
+ media = request.db.MediaEntry.find_one({
+ '_id': ObjectId(request.matchdict['m_id']),
+ 'state': 'processed'})
- #check that media uploader and user correspond
- if media['uploader'].get('username') != request.matchdict['user']:
+ # Check that media uploader and user correspond.
+ if not media or \
+ media['uploader'].get('username') != request.matchdict['user']:
return exc.HTTPNotFound()
template = request.template_env.get_template(
diff --git a/mediagoblin/util.py b/mediagoblin/util.py
index 63f0f9c5..1f568ed3 100644
--- a/mediagoblin/util.py
+++ b/mediagoblin/util.py
@@ -15,6 +15,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from email.MIMEText import MIMEText
+import gettext
+import pkg_resources
import smtplib
import sys
@@ -33,25 +35,41 @@ def _activate_testing():
TESTS_ENABLED = True
-def get_jinja_env(user_template_path=None):
+def get_jinja_loader(user_template_path=None):
"""
- Set up the Jinja environment, possibly allowing for user
+ Set up the Jinja template loaders, possibly allowing for user
overridden templates.
(In the future we may have another system for providing theming;
for now this is good enough.)
"""
if user_template_path:
- loader = jinja2.ChoiceLoader(
+ return jinja2.ChoiceLoader(
[jinja2.FileSystemLoader(user_template_path),
jinja2.PackageLoader('mediagoblin', 'templates')])
else:
- loader = jinja2.PackageLoader('mediagoblin', 'templates')
+ return jinja2.PackageLoader('mediagoblin', 'templates')
- return jinja2.Environment(
- loader=loader, autoescape=True,
+
+def get_jinja_env(template_loader, locale):
+ """
+ Set up the Jinja environment,
+
+ (In the future we may have another system for providing theming;
+ for now this is good enough.)
+ """
+ setup_gettext(locale)
+
+ template_env = jinja2.Environment(
+ loader=template_loader, autoescape=True,
extensions=['jinja2.ext.i18n'])
+ template_env.install_gettext_callables(
+ mgoblin_globals.translations.gettext,
+ mgoblin_globals.translations.ngettext)
+
+ return template_env
+
def setup_user_in_request(request):
"""
@@ -172,7 +190,7 @@ def send_email(from_addr, to_addrs, subject, message_body):
if TESTS_ENABLED:
EMAIL_TEST_INBOX.append(message)
- elif mgoblin_globals.email_debug_mode:
+ if getattr(mgoblin_globals, 'email_debug_mode', False):
print u"===== Email ====="
print u"From address: %s" % message['From']
print u"To addresses: %s" % message['To']
@@ -180,6 +198,82 @@ def send_email(from_addr, to_addrs, subject, message_body):
print u"-- Body: --"
print message.get_payload(decode=True)
+ return mhost.sendmail(from_addr, to_addrs, message.as_string())
+
+
+###################
+# Translation tools
+###################
+
+
+TRANSLATIONS_PATH = pkg_resources.resource_filename(
+ 'mediagoblin', 'translations')
+
+
+def locale_to_lower_upper(locale):
+ """
+ Take a locale, regardless of style, and format it like "en-us"
+ """
+ if '-' in locale:
+ lang, country = locale.split('-', 1)
+ return '%s_%s' % (lang.lower(), country.upper())
+ elif '_' in locale:
+ lang, country = locale.split('_', 1)
+ return '%s_%s' % (lang.lower(), country.upper())
else:
- return mhost.sendmail(from_addr, to_addrs, message.as_string())
+ return locale.lower()
+
+
+def locale_to_lower_lower(locale):
+ """
+ Take a locale, regardless of style, and format it like "en_US"
+ """
+ if '_' in locale:
+ lang, country = locale.split('_', 1)
+ return '%s-%s' % (lang.lower(), country.lower())
+ else:
+ return locale.lower()
+
+
+def get_locale_from_request(request):
+ """
+ Figure out what target language is most appropriate based on the
+ request
+ """
+ request_form = request.GET or request.POST
+
+ if request_form.has_key('lang'):
+ return locale_to_lower_upper(request_form['lang'])
+
+ accept_lang_matches = request.accept_language.best_matches()
+
+ # Your routing can explicitly specify a target language
+ if request.matchdict.has_key('locale'):
+ target_lang = request.matchdict['locale']
+ elif request.session.has_key('target_lang'):
+ target_lang = request.session['target_lang']
+ # Pull the first acceptable language
+ elif accept_lang_matches:
+ target_lang = accept_lang_matches[0]
+ # Fall back to English
+ else:
+ target_lang = 'en'
+
+ return locale_to_lower_upper(target_lang)
+
+
+def setup_gettext(locale):
+ """
+ Setup the gettext instance based on this locale
+ """
+ # Later on when we have plugins we may want to enable the
+ # multi-translations system they have so we can handle plugin
+ # translations too
+
+ # TODO: fallback nicely on translations from pt_PT to pt if not
+ # available, etc.
+ this_gettext = gettext.translation(
+ 'mediagoblin', TRANSLATIONS_PATH, [locale], fallback=True)
+ mgoblin_globals.setup_globals(
+ translations=this_gettext)