예제 #1
0
    def process_request(self, request):
        # Find locale, app
        prefixer = urlresolvers.Prefixer(request)
        urlresolvers.set_url_prefix(prefixer)
        full_path = prefixer.fix(prefixer.shortened_path)

        if 'lang' in request.GET:
            # Blank out the locale so that we can set a new one.  Remove lang
            # from query params so we don't have an infinite loop.
            prefixer.locale = ''
            new_path = prefixer.fix(prefixer.shortened_path)
            query = dict((smart_str(k), request.GET[k]) for k in request.GET)
            query.pop('lang')
            return HttpResponsePermanentRedirect(urlparams(new_path, **query))

        if full_path != request.path:
            query_string = request.META.get('QUERY_STRING', '')
            if query_string:
                full_path = "%s?%s" % (full_path, query_string)

            full_path = urllib.quote(full_path.encode('utf-8'))
            response = HttpResponsePermanentRedirect(full_path)

            # Vary on Accept-Language if we changed the locale.
            old_locale = prefixer.locale
            new_locale, _, _ = prefixer.split_path(full_path)
            if old_locale != new_locale:
                response['Vary'] = 'Accept-Language'

            return response

        request.path_info = '/' + prefixer.shortened_path
        l10n.activate(prefixer.locale)
        request.APP = amo.APPS.get(prefixer.app)
예제 #2
0
파일: tests.py 프로젝트: mccammos/zamboni
def setup():
    if not os.path.isdir(os.path.join(LOCALEDIR, 'LC_MESSAGES')):
        os.makedirs(os.path.join(LOCALEDIR, 'LC_MESSAGES'))
    fp = open(MOFILE, 'wb')
    fp.write(base64.decodestring(MO_DATA))
    fp.close()

    l10n.activate('xx')
예제 #3
0
파일: tests.py 프로젝트: mccammos/zamboni
def test_activate():
    l10n.deactivate_all()
    l10n.activate('fr')
    # This string is from the AMO .po file
    a_text = "My Account"
    p_text = "Mon compte"
    eq_(p_text, _(a_text))
    l10n.deactivate_all()
예제 #4
0
파일: app.py 프로젝트: tomachalek/kontext
 def __call__(self, environ, start_response):
     from controller.maintenance import MaintenanceController
     ui_lang = self.get_lang(environ)
     translation.activate(ui_lang)
     l10n.activate(ui_lang)
     request = Request(environ)
     app = MaintenanceController(request=request, ui_lang=ui_lang)
     status, headers, sid_is_valid, body = app.run()
     response = Response(response=body, status=status, headers=headers)
     return response(environ, start_response)
예제 #5
0
파일: app.py 프로젝트: petrduda/kontext
 def __call__(self, environ, start_response):
     from controller.maintenance import MaintenanceController
     ui_lang = self.get_lang(environ)
     translation.activate(ui_lang)
     l10n.activate(ui_lang)
     request = Request(environ)
     app = MaintenanceController(request=request, ui_lang=ui_lang)
     status, headers, sid_is_valid, body = app.run()
     response = Response(response=body, status=status, headers=headers)
     return response(environ, start_response)
예제 #6
0
    def __call__(self, environ, start_response):
        ui_lang = self.get_lang(environ)
        translation.activate(ui_lang)
        l10n.activate(ui_lang)
        environ['REQUEST_URI'] = wsgiref.util.request_uri(
            environ)  # TODO remove?
        app_url_prefix = settings.get_str('global', 'action_path_prefix', '')
        if app_url_prefix and environ['PATH_INFO'].startswith(app_url_prefix):
            environ['PATH_INFO'] = environ['PATH_INFO'][len(app_url_prefix):]

        sessions = plugins.runtime.SESSIONS.instance
        request = Request(environ)
        sid = request.cookies.get(sessions.get_cookie_name())
        if sid is None:
            request.session = sessions.new()
        else:
            request.session = sessions.get(sid)

        sid_is_valid = True
        if environ['PATH_INFO'] in ('/', ''):
            url = environ['REQUEST_URI'].split('?')[0]
            if not url.endswith('/'):
                url += '/'
            status = '303 See Other'
            headers = [('Location', '%sfirst_form' % url)]
            body = ''
        # old-style (CGI version) URLs are redirected to new ones
        elif '/run.cgi/' in environ['REQUEST_URI']:
            status = '301 Moved Permanently'
            headers = [('Location',
                        environ['REQUEST_URI'].replace('/run.cgi/', '/'))]
            body = ''
        else:
            controller_class = self.load_controller_class(environ['PATH_INFO'])
            app = controller_class(request=request, ui_lang=ui_lang)
            status, headers, sid_is_valid, body = app.run()
        response = Response(response=body, status=status, headers=headers)
        if not sid_is_valid:
            curr_data = dict(request.session)
            request.session = sessions.new()
            request.session.update(curr_data)
            request.session.modified = True
        if request.session.should_save:
            sessions.save(request.session)
            cookie_path = settings.get_str('global', 'cookie_path_prefix', '/')
            response.set_cookie(sessions.get_cookie_name(),
                                request.session.sid,
                                path=cookie_path)
        return response(environ, start_response)
예제 #7
0
파일: app.py 프로젝트: anukat2015/kontext
    def __call__(self, environ, start_response):
        """
        Works as specified by the WSGI
        """
        ui_lang = get_lang(environ)
        translation.activate(ui_lang)
        l10n.activate(ui_lang)
        environ['REQUEST_URI'] = wsgiref.util.request_uri(
            environ)  # TODO remove?

        sessions = plugins.get('sessions')
        request = Request(environ)
        sid = request.cookies.get(sessions.get_cookie_name())
        if sid is None:
            request.session = sessions.new()
        else:
            request.session = sessions.get(sid)

        sid_is_valid = True
        if environ['PATH_INFO'] in ('/', ''):
            url = environ['REQUEST_URI'].split('?')[0]
            if not url.endswith('/'):
                url += '/'
            status = '303 See Other'
            headers = [('Location', '%sfirst_form' % url)]
            body = ''
        elif '/run.cgi/' in environ[
                'REQUEST_URI']:  # old-style (CGI version) URLs are redirected to new ones
            status = '301 Moved Permanently'
            headers = [('Location',
                        environ['REQUEST_URI'].replace('/run.cgi/', '/'))]
            body = ''
        else:
            controller_class = load_controller_class(environ['PATH_INFO'])
            app = controller_class(request=request, ui_lang=ui_lang)
            status, headers, sid_is_valid, body = app.run()
        response = Response(response=body, status=status, headers=headers)
        if not sid_is_valid:
            curr_data = dict(request.session)
            request.session = sessions.new()
            request.session.update(curr_data)
            request.session.modified = True
        if request.session.should_save:
            sessions.save(request.session)
            response.set_cookie(sessions.get_cookie_name(),
                                request.session.sid)
        return response(environ, start_response)
예제 #8
0
파일: app.py 프로젝트: tomachalek/kontext
    def __call__(self, environ, start_response):
        ui_lang = self.get_lang(environ)
        translation.activate(ui_lang)
        l10n.activate(ui_lang)
        environ['REQUEST_URI'] = wsgiref.util.request_uri(environ)  # TODO remove?
        app_url_prefix = settings.get_str('global', 'action_path_prefix', '')
        if app_url_prefix and environ['PATH_INFO'].startswith(app_url_prefix):
            environ['PATH_INFO'] = environ['PATH_INFO'][len(app_url_prefix):]

        sessions = plugins.runtime.SESSIONS.instance
        request = Request(environ)
        sid = request.cookies.get(sessions.get_cookie_name())
        if sid is None:
            request.session = sessions.new()
        else:
            request.session = sessions.get(sid)

        sid_is_valid = True
        if environ['PATH_INFO'] in ('/', ''):
            url = environ['REQUEST_URI'].split('?')[0]
            if not url.endswith('/'):
                url += '/'
            status = '303 See Other'
            headers = [('Location', '%sfirst_form' % url)]
            body = ''
        # old-style (CGI version) URLs are redirected to new ones
        elif '/run.cgi/' in environ['REQUEST_URI']:
            status = '301 Moved Permanently'
            headers = [('Location', environ['REQUEST_URI'].replace('/run.cgi/', '/'))]
            body = ''
        else:
            controller_class = self.load_controller_class(environ['PATH_INFO'])
            app = controller_class(request=request, ui_lang=ui_lang)
            status, headers, sid_is_valid, body = app.run()
        response = Response(response=body, status=status, headers=headers)
        if not sid_is_valid:
            curr_data = dict(request.session)
            request.session = sessions.new()
            request.session.update(curr_data)
            request.session.modified = True
        if request.session.should_save:
            sessions.save(request.session)
            response.set_cookie(sessions.get_cookie_name(), request.session.sid)
        return response(environ, start_response)
예제 #9
0
파일: app.py 프로젝트: simar0at/kontext
    def __call__(self, environ, start_response):
        """
        Works as specified by the WSGI
        """
        ui_lang = get_lang(environ)
        translation.activate(ui_lang)
        l10n.activate(ui_lang)
        environ['REQUEST_URI'] = wsgiref.util.request_uri(environ)  # TODO remove?

        sessions = plugins.get('sessions')
        request = Request(environ)
        sid = request.cookies.get(sessions.get_cookie_name())
        if sid is None:
            request.session = sessions.new()
        else:
            request.session = sessions.get(sid)

        sid_is_valid = True
        if environ['PATH_INFO'] in ('/', ''):
            url = environ['REQUEST_URI']
            if not url.endswith('/'):
                url += '/'
            status = '303 See Other'
            headers = [('Location', '%sfirst_form' % url)]
            body = ''
        elif '/run.cgi/' in environ['REQUEST_URI']:  # old-style (CGI version) URLs are redirected to new ones
            status = '301 Moved Permanently'
            headers = [('Location', environ['REQUEST_URI'].replace('/run.cgi/', '/'))]
            body = ''
        else:
            controller_class = load_controller_class(environ['PATH_INFO'])
            app = controller_class(request=request, ui_lang=ui_lang)
            status, headers, sid_is_valid, body = app.run(request)
        response = Response(response=body, status=status, headers=headers)
        if not sid_is_valid:
            curr_data = dict(request.session)
            request.session = sessions.new()
            request.session.update(curr_data)
            request.session.modified = True
        if request.session.should_save:
            sessions.save(request.session)
            response.set_cookie(sessions.get_cookie_name(), request.session.sid)
        start_response(status, headers)
        return response(environ, start_response)
예제 #10
0
파일: tests.py 프로젝트: mccammos/zamboni
def test_cached_activate():
    """
    Make sure the locale is always activated properly, even when we hit a
    cached version.
    """
    l10n.deactivate_all()
    l10n.activate('fr')
    eq_(translation.get_language(), 'fr')
    l10n.activate('vi')
    eq_(translation.get_language(), 'vi')
    l10n.activate('fr')
    eq_(translation.get_language(), 'fr')
    l10n.activate('de')
    eq_(translation.get_language(), 'de')
    l10n.activate('fr')
    eq_(translation.get_language(), 'fr')
    l10n.activate('vi')
    eq_(translation.get_language(), 'vi')
예제 #11
0
파일: models.py 프로젝트: mccammos/zamboni
    def mail_thankyou(self, request=None):
        """
        Mail a thankyou note for a completed contribution.

        Raises a ``ContributionError`` exception when the contribution
        is not complete or email addresses are not found.
        """

        # Setup l10n before loading addon.
        if self.source_locale:
            lang = self.source_locale
        else:
            lang = self.addon.default_locale
        l10n.activate(lang)

        # Thankyous must be enabled.
        if not self.addon.enable_thankyou:
            # Not an error condition, just return.
            return

        # Contribution must be complete.
        if not self.transaction_id:
            raise ContributionError('Transaction not complete')

        # Send from support_email, developer's email, or default.
        if self.addon.support_email:
            from_email = str(self.addon.support_email)
        else:
            try:
                author = self.addon.listed_authors[0]
                if not author.emailhidden:
                    from_email = author.email
            except IndexError:
                from_email = None
        if not from_email:
            from_email = settings.EMAIL_FROM_DEFAULT

        # We need the contributor's email.
        to_email = self.post_data['payer_email']
        if not to_email:
            raise ContributionError('Empty payer email')

        # Make sure the url uses the right language.
        # Setting a prefixer would be nicer, but that requires a request.
        url_parts = self.addon.meet_developers_url.split('/')
        url_parts[1] = lang

        # Buildup the email components.
        t = loader.get_template('stats/contribution-thankyou-email.ltxt')
        c = {
            'thankyou_note': self.addon.thankyou_note,
            'addon_name': self.addon.name,
            'learn_url': settings.SITE_URL + '/'.join(url_parts),
            'hostname': settings.HOSTNAME,
        }
        body = t.render(Context(c))
        subject = _('Thanks for contributing to {addon_name}').format(
                    addon_name=self.addon.name)

        # Send the email
        if amo_send_mail(subject, body, from_email, [to_email],
                     fail_silently=True):
            # Clear out contributor identifying information.
            del(self.post_data['payer_email'])
            self.save()