Passa al contenuto
Odoo Menu
  • Accedi
  • Provalo gratis
  • App
    Finanze
    • Contabilità
    • Fatturazione
    • Note spese
    • Fogli di calcolo (BI)
    • Documenti
    • Firma
    Vendite
    • CRM
    • Vendite
    • Punto vendita Negozio
    • Punto vendita Ristorante
    • Abbonamenti
    • Noleggi
    Siti web
    • Configuratore sito web
    • E-commerce
    • Blog
    • Forum
    • Live chat
    • E-learning
    Supply chain
    • Magazzino
    • Produzione
    • PLM
    • Acquisti
    • Manutenzione
    • Qualità
    Risorse umane
    • Dipendenti
    • Assunzioni
    • Ferie
    • Valutazioni
    • Referral dipendenti
    • Parco veicoli
    Marketing
    • Social marketing
    • E-mail marketing
    • SMS marketing
    • Eventi
    • Marketing automation
    • Sondaggi
    Servizi
    • Progetti
    • Fogli ore
    • Assistenza sul campo
    • Helpdesk
    • Pianificazione
    • Appuntamenti
    Produttività
    • Comunicazioni
    • Approvazioni
    • IoT
    • VoIP
    • Knowledge
    • WhatsApp
    App di terze parti Odoo Studio Piattaforma cloud Odoo
  • Settori
    Retail
    • Libreria
    • Negozio di abbigliamento
    • Negozio di arredamento
    • Alimentari
    • Ferramenta
    • Negozio di giocattoli
    Cibo e ospitalità
    • Bar e pub
    • Ristorante
    • Fast food
    • Pensione
    • Grossista di bevande
    • Hotel
    Agenzia immobiliare
    • Agenzia immobiliare
    • Studio di architettura
    • Edilizia
    • Gestione immobiliare
    • Impresa di giardinaggio
    • Associazione di proprietari immobiliari
    Consulenza
    • Società di contabilità
    • Partner Odoo
    • Agenzia di marketing
    • Studio legale
    • Selezione del personale
    • Audit e certificazione
    Produzione
    • Tessile
    • Metallo
    • Arredamenti
    • Alimentare
    • Birrificio
    • Ditta di regalistica aziendale
    Benessere e sport
    • Club sportivo
    • Negozio di ottica
    • Centro fitness
    • Centro benessere
    • Farmacia
    • Parrucchiere
    Commercio
    • Tuttofare
    • Hardware e assistenza IT
    • Ditta di installazione di pannelli solari
    • Calzolaio
    • Servizi di pulizia
    • Servizi di climatizzazione
    Altro
    • Organizzazione non profit
    • Ente per la tutela ambientale
    • Agenzia di cartellonistica pubblicitaria
    • Studio fotografico
    • Punto noleggio di biciclette
    • Rivenditore di software
    Carica tutti i settori
  • Community
    Apprendimento
    • Tutorial
    • Documentazione
    • Certificazioni 
    • Formazione
    • Blog
    • Podcast
    Potenzia la tua formazione
    • Programma educativo
    • Scale Up! Business Game
    • Visita Odoo
    Ottieni il software
    • Scarica
    • Versioni a confronto
    • Note di versione
    Collabora
    • Github
    • Forum
    • Eventi
    • Traduzioni
    • Diventa nostro partner
    • Servizi per partner
    • Registra la tua società di contabilità
    Ottieni servizi
    • Trova un partner
    • Trova un contabile
    • Incontra un esperto
    • Servizi di implementazione
    • Testimonianze dei clienti
    • Supporto
    • Aggiornamenti
    GitHub Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Richiedi una demo
  • Prezzi
  • Aiuto

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Contabilità
  • Magazzino
  • PoS
  • Progetti
  • MRP
All apps
È necessario essere registrati per interagire con la community.
Tutti gli articoli Persone Badge
Etichette (Mostra tutto)
odoo accounting v14 pos v15
Sul forum
È necessario essere registrati per interagire con la community.
Tutti gli articoli Persone Badge
Etichette (Mostra tutto)
odoo accounting v14 pos v15
Sul forum
Assistenza

Non-ASCII/Unicode/Cyrillic URL on Website

Iscriviti

Ricevi una notifica quando c'è un'attività per questo post

La domanda è stata contrassegnata
werkzeugurlcyrillicunicodewebsite
1 Rispondi
8966 Visualizzazioni
Avatar
Anton Chepurov

My goal is to turn the standard website URL of a Product:

/shop/product/ipad-retina-display-5

into a different format:

/shop/product/5/ipad/ipad-с-дисплеем-retina/a2323

where ID is moved to right after the `/shop/product/`, brand follows ID, Internal Reference is added to the end of the URL, and most importantly the product name is translated to the language currently selected (in this example - Russian).

I use the following code to achieve all but translations:

class my_module_website_sale(website_sale):
    @http.route(['/shop/product/<product_model:product>'])
    def product(self, product, category='', search='', **kwargs):
        return super(my_module_website_sale, self).product(product, category=category, search=search, **kwargs)

and this:

from openerp.addons.website.models import website
from openerp.addons.website.models.ir_http import RequestUID
from openerp.addons.website.models.website import slugify

old_slug = website.slug

def slug_product(value):
    if isinstance(value, tuple):
        id, brand, name, default_code = value
    else:
        id, brand, name, default_code = value.id, value.product_variant_ids[0].brand_id.name, value.name, value.default_code
    return '%s/%s/%s/%s' % (id, brand, slugify(name), default_code)

def slug(value):
    if isinstance(value, browse_record) and value._name == 'product.template':
        return slug_product(value)
    return old_slug(value)

website.slug = slug

class ProductModelConverter(werkzeug.routing.PathConverter):
    def __init__(self, url_map):
        super(ProductModelConverter, self).__init__(url_map)
        self.regex = '([0-9]+)/.*'

    def to_python(self, value):
        m = re.match(self.regex, value)
        _uid = RequestUID(value=value, match=m, converter=self)
        return request.registry['product.template'].browse(
            request.cr, _uid, int(m.group(1)), context=request.context)

    def to_url(self, value):
        return slug_product(value)

    def generate(self, cr, uid, query=None, args=None, context=None):
        brands = {b['id']: b['name'] for b in fetchdictall(cr, 'SELECT id, name FROM my_module_brand')}
        products = [(tmpl, brands.get(brand, ''), name, code)
                            for (tmpl, brand, name, code) in
                            fetchall(cr, 'SELECT product_tmpl_id, brand_id, name_template, default_code FROM product_product '
                                                'LEFT JOIN product_template t ON product_tmpl_id = t.id WHERE t.website_published = true ORDER BY product_tmpl_id')]
        templates_done = set()
        for product in products:
            if product[0] not in templates_done:
                templates_done.add(product[0])
                yield {'loc': product}

class ir_http(models.AbstractModel):
    _inherit = 'ir.http'

    def _get_converters(self):
        return dict(super(ir_http, self)._get_converters(), product_model=ProductModelConverter)

 This code works perfectly well for the ASCII URLs, e.g. this URL brings me to the required website shop page:

/shop/product/5/ipad/ipad-retina-display/a2323

However, once translated to Russian, this URL raises an error on the werkzeug level (i.e. not even reaching my code, AFAIU):

/shop/product/5/ipad/ipad-с-дисплеем-retina/a2323

The error:

2016-08-09 05:15:26,210 3902 INFO mgr werkzeug: 127.0.0.1 - - [09/Aug/2016 05:15:26] "GET /ru_RU/shop/product/5/ipad/%D0%B2%D0%BE%D0%B4%D1%8F%D0%BD%D0%BE%D0%B9-%D1%88%D0%BB%D0%B0%D0%BD%D0%B3/a2323 HTTP/1.0" 500 -
2016-08-09 05:15:26,217 3902 ERROR mgr werkzeug: Error on request:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 177, in run_wsgi
execute(self.server.app)
File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 165, in execute
application_iter = app(environ, start_response)
File "/home/a0c/programs/openerp/odoo/openerp/service/server.py", line 291, in app
return self.app(e, s)
File "/home/a0c/programs/openerp/odoo/openerp/service/wsgi_server.py", line 224, in application
return application_unproxied(environ, start_response)
File "/home/a0c/programs/openerp/odoo/openerp/service/wsgi_server.py", line 210, in application_unproxied
result = handler(environ, start_response)
File "/home/a0c/programs/openerp/odoo/openerp/http.py", line 1292, in __call__
return self.dispatch(environ, start_response)
File "/home/a0c/programs/openerp/odoo/openerp/http.py", line 1266, in __call__
return self.app(environ, start_wrapped)
File "/usr/lib/python2.7/dist-packages/werkzeug/wsgi.py", line 579, in __call__
return self.app(environ, start_response)
File "/home/a0c/programs/openerp/odoo/openerp/http.py", line 1266, in __call__
return self.app(environ, start_wrapped)
File "/usr/lib/python2.7/dist-packages/werkzeug/wsgi.py", line 579, in __call__
return self.app(environ, start_response)
File "/home/a0c/programs/openerp/odoo/openerp/http.py", line 1439, in dispatch
result = ir_http._dispatch()
File "/home/a0c/programs/openerp/odoo/addons/crm/ir_http.py", line 13, in _dispatch
response = super(ir_http, self)._dispatch()
File "/home/a0c/programs/openerp/odoo/addons/website/models/ir_http.py", line 145, in _dispatch
return self.reroute('/'.join(path) or '/')
File "/home/a0c/programs/openerp/odoo/addons/website/models/ir_http.py", line 166, in reroute
return self._dispatch()
File "/home/a0c/programs/openerp/odoo/addons/crm/ir_http.py", line 13, in _dispatch
response = super(ir_http, self)._dispatch()
File "/home/a0c/programs/openerp/odoo/addons/website/models/ir_http.py", line 72, in _dispatch
func, arguments = self._find_handler()
File "/home/a0c/programs/openerp/odoo/openerp/addons/base/ir/ir_http.py", line 65, in _find_handler
return self.routing_map().bind_to_environ(request.httprequest.environ).match(return_rule=return_rule)
File "/usr/lib/python2.7/dist-packages/werkzeug/routing.py", line 1202, in bind_to_environ
path_info = _get_wsgi_string('PATH_INFO')
File "/usr/lib/python2.7/dist-packages/werkzeug/routing.py", line 1199, in _get_wsgi_string
return wsgi_decoding_dance(val, self.charset)
File "/usr/lib/python2.7/dist-packages/werkzeug/_compat.py", line 92, in wsgi_decoding_dance
return s.decode(charset, errors)
File "/usr/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 21-27: ordinal not in range(128)


Does anybody have an idea how to properly configure werkzeug's PathConverters (or I dunno what else) to make them accept non-ASCII URLs?

Many thanks!

2
Avatar
Abbandona
Avatar
Anton Chepurov
Autore Risposta migliore

Solved this by patching werkzeug and by making slugify() and url_for() support unicode URLs.

Change wsgi_decoding_dance() in werkzeug/_compat.py to:

 def wsgi_decoding_dance(s, charset='utf-8', errors='replace'):
    if isinstance(s, unicode):
        return s
    return s.decode(charset, errors)

This is similar to wsgi_encoding_dance():

 def wsgi_encoding_dance(s, charset='utf-8', errors='replace'):
    if isinstance(s, bytes):
        return s
    return s.encode(charset, errors)

And in the code snippet above, instead of importing slugify() from odoo

from openerp.addons.website.models.website import slugify

define your own slugify() to be used in slug_product():

import unicodedata
from openerp.tools import ustr

def slugify(s, max_length=None):
    s = ustr(s)
    uni = unicodedata.normalize('NFKC', s)
    slug = re.sub('[\W_]', ' ', uni, flags=re.UNICODE).strip().lower()
    slug = re.sub('[-\s]+', '-', slug)
    return slug[:max_length]

Finally, override url_for():

import urlparse
from openerp.addons.web.http import request
from openerp.addons.website.models.website import is_multilang_url

def url_for(path_or_uri, lang=None):
    if isinstance(path_or_uri, unicode):
        path_or_uri = path_or_uri.encode('utf-8')
    current_path = request.httprequest.path
    if isinstance(current_path, unicode):
        current_path = current_path.encode('utf-8')
    location = path_or_uri.strip()
    force_lang = lang is not None
    url = urlparse.urlparse(location)

    if request and not url.netloc and not url.scheme and (url.path or force_lang):
        location = urlparse.urljoin(current_path, location)

        lang = lang or request.context.get('lang')
        langs = [lg[0] for lg in request.website.get_languages()]

        if (len(langs) > 1 or force_lang) and is_multilang_url(location, langs):
            ps = location.decode('utf-8').split('/')
            if ps[1] in langs:
                # Replace the language only if we explicitly provide a language to url_for
                if force_lang:
                    ps[1] = lang
                # Remove the default language unless it's explicitly provided
                elif ps[1] == request.website.default_lang_code:
                    ps.pop(1)
            # Insert the context language or the provided language
            elif lang != request.website.default_lang_code or force_lang:
                ps.insert(1, lang)
            location = '/'.join(ps)
    return location if isinstance(location, unicode) else location.decode('utf-8')

import openerp
openerp.addons.website.models.website.url_for = url_for



3
Avatar
Abbandona
Sehrish

awsome

Ti stai godendo la conversazione? Non leggere soltanto, partecipa anche tu!

Crea un account oggi per scoprire funzionalità esclusive ed entrare a far parte della nostra fantastica community!

Registrati
Post correlati Risposte Visualizzazioni Attività
Multi website with a different xxx.odoo.com address
url website
Avatar
0
gen 21
6639
How do I change my Odoo Website URL? Risolto
url website
Avatar
Avatar
1
apr 17
8438
How redirect from Website Front end to Odoo Web Back end ? Risolto
url redirect website
Avatar
Avatar
Avatar
Avatar
Avatar
5
lug 25
17601
How to include the product variant in website URL
url variants website
Avatar
0
gen 24
2297
Redirection error
url website odoo12
Avatar
0
ott 23
2292
Community
  • Tutorial
  • Documentazione
  • Forum
Open source
  • Scarica
  • Github
  • Runbot
  • Traduzioni
Servizi
  • Hosting Odoo.sh
  • Supporto
  • Aggiornamenti
  • Sviluppi personalizzati
  • Formazione
  • Trova un contabile
  • Trova un partner
  • Diventa nostro partner
Chi siamo
  • La nostra azienda
  • Branding
  • Contattaci
  • Lavora con noi
  • Eventi
  • Podcast
  • Blog
  • Clienti
  • Note legali • Privacy
  • Sicurezza
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo è un gestionale di applicazioni aziendali open source pensato per coprire tutte le esigenze della tua azienda: CRM, Vendite, E-commerce, Magazzino, Produzione, Fatturazione elettronica, Project Management e molto altro.

Il punto di forza di Odoo è quello di offrire un ecosistema unico di app facili da usare, intuitive e completamente integrate tra loro.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now