wishlist_add_ajax broken in satchmo_ext.wishlist.views

Create issue
Issue #1165 resolved
Former user created an issue

I was testing the adding of a ProductWish using wishlist_add_ajax. There were two problems I encountered:

1) The contact reference was un-assigned so the invocation of ProductWish.objects.create_if_new(product, contact, details) was failing on the contact reference

2) The loading of contact was performed in an if not product: block. This meant that when a product was avilable, the contact was not loaded!

I was able to resolve these problems with a simple refactoring of the view function:

{{{

!python

def wishlist_add_ajax(request, template="shop/json.html"): data = {'errors': []} product = None contact = None formdata = request.POST.copy() productslug = formdata['productname']

log.debug('WISHLIST AJAX: slug=%s', productslug)
try:
    product, details = product_from_post(productslug, formdata)

except (Product.DoesNotExist, MultiValueDictKeyError):
    log.warn("Could not find product: %s", productslug)
product = None

if not product:
    data['errors'].append(('product', _('The product you have requested does not exist.')))
else:
    data['id'] = product.id
    data['name'] = product.translated_name()

try:
    contact = Contact.objects.from_request(request)
except Contact.DoesNotExist:
    log.warn("Could not find contact")

if not contact:
    data['errors'].append(('contact', _('The contact associated with this request does not exist.')))

if not data['errors']:
    wish = ProductWish.objects.create_if_new(product, contact, details)
    data['results'] = _('Success')
else:
    data['results'] = _('Error')

encoded = JSONEncoder().encode(data)
encoded = mark_safe(encoded)
log.debug('WISHLIST AJAX: %s', data)

return render_to_response(template, {'json' : encoded})

}}}

Comments (2)

  1. Chris Moffitt repo owner

    Thank you for the patch. In the future, a diff would be preferable so we can see exactly what the changes are.

  2. Log in to comment