Skip to content

Commit

Permalink
Merge pull request #1623 from akvo/#1617-google-translate
Browse files Browse the repository at this point in the history
#1617 google translate
  • Loading branch information
kardan committed Jun 22, 2015
2 parents fbb3064 + 42e0df3 commit 7a4156a
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 11 deletions.
24 changes: 14 additions & 10 deletions akvo/rsr/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,31 +1035,33 @@ class PaymentGatewaySelectorAdmin(admin.ModelAdmin):


class PartnerSiteAdmin(TimestampsAdminDisplayMixin, admin.ModelAdmin):

"""Defines the RSR Pages admin."""

fieldsets = (
(u'General', dict(fields=('organisation', 'enabled',))),
(u'HTTP', dict(fields=('hostname', 'cname', 'custom_return_url', 'custom_return_url_text',
'piwik_id',))),
(u'Style and content',
dict(fields=('all_maps', 'about_box', 'about_image', 'custom_css', 'custom_logo',
'custom_favicon',))),
(u'Languages and translation', dict(fields=('default_language', 'ui_translation',
'google_translation',))),
(u'Languages and translation', dict(fields=('google_translation',))),
(u'Social', dict(fields=('twitter_button', 'facebook_button', 'facebook_app_id',))),
(_(u'Project selection'), {
'description': u'{}'.format(
u'<p style="margin-left:0; padding-left:0; margin-top:1em; width:75%%;">'
u'Select the projects to be shown on your Site.'
u'</p>'
u'<p style="margin-left:0; padding-left:0; margin-top:1em; width:75%%;">'
u'The default setting selects all projects associated with the organisation of the Site. '
u'</p>'
u'The default setting selects all projects associated with the organisation of '
u'the Site.</p>'
u'<p style="margin-left:0; padding-left:0; margin-top:1em; width:75%%;">'
u'De-selecting the "Show only projects of partner" check-box shows all projects in RSR. '
u'This is meant to be used with the keywords below, '
u'De-selecting the "Show only projects of partner" check-box shows all projects '
u'in RSR. This is meant to be used with the keywords below, '
u'thus selecting only projects associated with the selected keywords. '
u'<br/>If keywords are added to a Site showing only projects of a partner, '
u'the selection will be further filtered by only showing associated projects with those keywords.'
u'</p>'
u'the selection will be further filtered by only showing associated projects '
u'with those keywords.</p>'
u'<p style="margin-left:0; padding-left:0; margin-top:1em; width:75%%;">'
u'When "Exclude projects with selected keyword is checked" '
u'projects with the chosen keywords are instead excluded from the list.'
Expand All @@ -1075,9 +1077,11 @@ class PartnerSiteAdmin(TimestampsAdminDisplayMixin, admin.ModelAdmin):
readonly_fields = ('created_at', 'last_modified_at',)

def get_fieldsets(self, request, obj=None):
# don't show the notes field unless you are superuser or admin
"""Don't show the notes field unless you are superuser or admin.
# note that this is somewhat fragile as it relies on adding/removing from the _first_
# fieldset
"""
if request.user.is_superuser or request.user.is_admin:
self.fieldsets[0][1]['fields'] = ('organisation', 'enabled', 'notes',)
else:
Expand All @@ -1091,7 +1095,7 @@ def get_form(self, request, obj=None, **kwargs):
)

def get_list_display(self, request):
# see the notes fields in the change list if you are superuser or admin
""""See the notes fields in the change list if you are superuser or admin."""
if request.user.is_superuser or request.user.is_admin:
return list(self.list_display) + ['notes']
return super(PartnerSiteAdmin, self).get_list_display(request)
Expand Down
56 changes: 56 additions & 0 deletions akvo/rsr/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
from django.conf import settings
from django.core.exceptions import DisallowedHost
from django.db.models import Q
from django.core.urlresolvers import (is_valid_path, get_resolver, LocaleRegexURLResolver)
from django.shortcuts import redirect
from akvo.rsr.context_processors import extra_context
from akvo.rsr.models import PartnerSite
from django.utils import translation
from django.http import HttpResponseRedirect


def _is_rsr_host(hostname):
Expand All @@ -36,6 +39,59 @@ def _partner_site(netloc):
)


class DefaultLanguageMiddleware(object):

"""A non working (BROKEN) default language middleware.
A try in supporting default languages, but since this will redirect all url_patterns and
not only i18n ones it's broken.
"""

def __init__(self):
"""."""
self._is_language_prefix_patterns_used = False
for url_pattern in get_resolver(None).url_patterns:
if isinstance(url_pattern, LocaleRegexURLResolver):
self._is_language_prefix_patterns_used = True
break

def is_language_prefix_patterns_used(self):
"""."""
return self._is_language_prefix_patterns_used

def is_i18n_path(self, path):
"""."""
from akvo.urls import localised_patterns
print "=> {}".format(localised_patterns)
from django.core.urlresolvers import resolve, Resolver404
try:
resolve(path, localised_patterns)
return True
except Resolver404:
return False
except TypeError, e:
return False
return False

def process_request(self, request):
"""Redirect to selected language."""
if not request.rsr_page:
return None

if self.is_i18n_path(request.path):
print "{} was i18n path".format(request.path)
else:
print "{} was not 18n path".format(request.path)

language_from_path = translation.get_language_from_path(request.path_info)
if not language_from_path:
if request.rsr_page.default_language:
lang = request.rsr_page.default_language
return HttpResponseRedirect('/{}{}'.format(lang, request.path))
return HttpResponseRedirect('/en{}'.format(request.path))
return None


class HostDispatchMiddleware(object):

"""Simple test for middleware testing."""
Expand Down
6 changes: 6 additions & 0 deletions akvo/rsr/static/styles-src/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,12 @@ dd.currencyAmount {
.updateText {
white-space: pre-line; }

/* Google translation bar styles */
body.translationBarActive .nav {
padding-top: 39px; }
body.translationBarActive .navbar-brand {
margin-top: 39px; }

/* Cookie */
#cookie-law {
position: fixed;
Expand Down
14 changes: 14 additions & 0 deletions akvo/rsr/static/styles-src/main.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2017,6 +2017,20 @@ dd {
white-space: pre-line;
}

/* Google translation bar styles */

body {
&.translationBarActive {
.nav {
padding-top:39px;
}

.navbar-brand {
margin-top:39px
}
}
}

/* Cookie */

#cookie-law {
Expand Down
1 change: 1 addition & 0 deletions akvo/settings/10-base.conf
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ AUTH_USER_MODEL = 'rsr.User'

MIDDLEWARE_CLASSES = (
'akvo.rsr.middleware.HostDispatchMiddleware',
# 'akvo.rsr.middleware.DefaultLanguageMiddleware',
# 'akvo.rsr.middleware_old.PagesLocaleMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
Expand Down
2 changes: 1 addition & 1 deletion akvo/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
{% block head_js %}{% endblock %}
</head>

<body>
<body {% if rsr_page.google_translation %} class="translationBarActive" {% endif %}>
{% include 'navigation/header.html' %}

{% block maincontent %}{% endblock %}
Expand Down
20 changes: 20 additions & 0 deletions akvo/templates/navigation/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,26 @@ <h1>Akvo RSR</h1> {# Text is just a placeholder, svg from css #}
</ul>
</div>
</li>
{% if rsr_page and rsr_page.google_translation = True %}
<li>
<div class="dropdown langDropdown">
<div id="google_translate_element"></div>
<script type="text/javascript">
function googleTranslateElementInit() {
new google.translate.TranslateElement(
{
pageLanguage: '{{LANGUAGE_CODE}}',
layout: google.translate.TranslateElement.InlineLayout.SIMPLE,
autoDisplay: false,
multilanguagePage: true
},
'google_translate_element');
}
</script>
<script type="text/javascript" src="//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script>
</div>
</li>
{% endif %}
</ul>
</div>
</div>
Expand Down

0 comments on commit 7a4156a

Please sign in to comment.