stephenmcd / cartridge

Ecommerce for Mezzanine
http://cartridge.jupo.org
BSD 2-Clause "Simplified" License
709 stars 299 forks source link

str() vs .encode('utf-8') ? #164

Closed Lucianovici closed 10 years ago

Lucianovici commented 10 years ago

Hi,

Great job! I really like mezzanine + cartridge. I'm kind of new to django/python world, but I'm leaning every day :)

Intro

I'm creating a simple ecommerce, adapting an existing html/css/js template. I need ajax calls to update cart and also add products to cart from product listings.

Everything seems to work, after I created my own view, bound to something like 'ajax/product/add/product-slug'.

Inside my view clp_shop.view I simply called the original shop.view.product respecting DRY.

def clp_ajax_add_inline_product(request, slug):
    """
    Handling adding the product to either the cart or the wishlist.
    via ajax.
    """
    context_json_data = []
    if request.is_ajax():

        # Call the original method from cartridge to add product
        product(request, slug)
        # Clear messages created by the original cartridge implementation
        clear_queued_message(request)

        context = list(chain([request.cart], request.cart.items.all()))

        context_json_data = serialize("json", context, extras=(
            'total_quantity',
            'total_price')
        )

    return HttpResponse(context_json_data, mimetype="application/json")

My problem

I have unicode characters (ă î â ț ș) inside product titles, therefore in shop.models.Cart.add_item I'd need to have

item.description = variation.encode('utf-8')
//, instead of the original: 
// item.description = str(variation)

If not, (title="roșii" slug="rosii") I get the obvious error:

UnicodeEncodeError at /ajax/product/add/rosii/ 'ascii' codec can't encode character u'\u0219' in position 2: ordinal not in range(128)

Questions:

  1. So is it better to have .encode('utf-8'), like these guys claim http://stackoverflow.com/questions/9942594/unicodeencodeerror-ascii-codec-cant-encode-character-u-xa0-in-position-20
  2. Is this the right approach extending mezzanine/cartridge views? For my particular case in which I need the same function, but different output: JSON, for ajax calls.

    Environment:

Mezzanine==3.0.4 Cartridge==0.9.1

SLUGIFY = "django.template.defaultfilters.slugify"

Thanks!

Lucianovici commented 10 years ago

Well, after I tested on the mezzanine demo server, and saw it's working. Now it's clear to me that I have misconfigured something.

$ locale LANG=ro_RO.utf8 LC_CTYPE="ro_RO.utf8" LC_NUMERIC="ro_RO.utf8" LC_TIME="ro_RO.utf8" LC_COLLATE="ro_RO.utf8" LC_MONETARY="ro_RO.utf8" LC_MESSAGES="ro_RO.utf8" LC_PAPER="ro_RO.utf8" LC_NAME="ro_RO.utf8" LC_ADDRESS="ro_RO.utf8" LC_TELEPHONE="ro_RO.utf8" LC_MEASUREMENT="ro_RO.utf8" LC_IDENTIFICATION="ro_RO.utf8" LC_ALL=

Testin locally on my workstation: OS: Gentoo, 3.5.7-gentoo Local Server: django dev server (manage.py runserver).

Can you please help me :) Thanks!

Lucianovici commented 10 years ago

Anyway, not something very important, since only on my local environment something is messed up. Everything seems to work on a production env (Ubuntu, Gunicorn behind nginx).

However, some resources that might help: http://stackoverflow.com/questions/11660627/python-app-import-error-in-django-with-wsgi-gunicorn http://stackoverflow.com/questions/2108824/mysql-incorrect-string-value-error-when-save-unicode-string-in-django