<commit_before>from decimal import Decimal
from django.core import urlresolvers
from django.dispatch import dispatcher
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.datastructures import MultiValueDictKeyError
from django.utils.safestring import mark_safe
from django.utils.simplejson.encoder import JSONEncoder
from django.utils.translation import ugettext as _
from satchmo.configuration import config_value
from satchmo.product.models import Product, OptionManager
from satchmo.product.views import find_product_template, optionset_from_post
from satchmo.shop.models import Cart, CartItem, NullCart
from satchmo.shop.signals import satchmo_cart_changed
from satchmo.shop.utils import trunc_decimal
from satchmo.shop.views.utils import bad_or_missing
import logging

log = logging.getLogger('shop.views.cart')

NOTSET = object()

class NullCartItem(object):
    def __init__(self, itemid):
        self.id = itemid
        self.quantity = 0
        self.line_total = 0

def _set_quantity(request, force_delete=False):
    """Set the quantity for a specific cartitem.
    Checks to make sure the item is actually in the user's cart.
    """
    cart = Cart.objects.from_request(request, create=False)
    if isinstance(cart, NullCart):
        return (False, None, None, _("No cart to update."))

    if force_delete:
        qty = 0
    else:
        try:
            qty = int(request.POST.get('quantity'))
        except (TypeError, ValueError):
            return (False, cart, None, _("Bad quantity."))
        if qty < 0:
            qty = 0

    try:
        itemid = int(request.POST.get('cartitem'))
    except (TypeError, ValueError):
        return (False, cart, None, _("Bad item number."))

    try:
        cartitem = CartItem.objects.get(pk=itemid, cart=cart)
    except CartItem.DoesNotExist:
        return (False, cart, None, _("No such item in your cart."))

    if qty == 0:
        cartitem.delete()
        cartitem = NullCartItem(itemid)
    else:
        cartitem.quantity = qty
        cartitem.save()

    dispatcher.send(signal=satchmo_cart_changed, cart=cart, request=request)
    return (True, cart, cartitem, "")

def display(request, cart=None, error_message='', default_view_tax=NOTSET):
    """Display the items in the cart."""
    
    if default_view_tax == NOTSET:
        default_view_tax = config_value('TAX', 'DEFAULT_VIEW_TAX')
            
    if not cart:
        cart = Cart.objects.from_request(request)
                
    context = RequestContext(request, {
        'cart': cart,
        'error_message': error_message,
        'default_view_tax' : default_view_tax,
        })
    return render_to_response('base_cart.html', context)

def add(request, id=0):
    """Add an item to the cart."""
    #TODO: Error checking for invalid combos
    log.debug('FORM: %s', request.POST)
    formdata = request.POST.copy()
    productslug = formdata['productname']
    try:
        product = Product.objects.get(slug=productslug)
        log.debug('found product: %s', product)
        p_types = product.get_subtypes()
        details = []<commit_msg>Cleanup a small issue where an incorrect url could cause an error.<commit_after>from decimal import Decimal
from django.core import urlresolvers
from django.dispatch import dispatcher
from django.http import HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.utils.datastructures import MultiValueDictKeyError
from django.utils.safestring import mark_safe
from django.utils.simplejson.encoder import JSONEncoder
from django.utils.translation import ugettext as _
from satchmo.configuration import config_value
from satchmo.product.models import Product, OptionManager
from satchmo.product.views import find_product_template, optionset_from_post
from satchmo.shop.models import Cart, CartItem, NullCart
from satchmo.shop.signals import satchmo_cart_changed
from satchmo.shop.utils import trunc_decimal
from satchmo.shop.views.utils import bad_or_missing
import logging

log = logging.getLogger('shop.views.cart')

NOTSET = object()

class NullCartItem(object):
    def __init__(self, itemid):
        self.id = itemid
        self.quantity = 0
        self.line_total = 0

def _set_quantity(request, force_delete=False):
    """Set the quantity for a specific cartitem.
    Checks to make sure the item is actually in the user's cart.
    """
    cart = Cart.objects.from_request(request, create=False)
    if isinstance(cart, NullCart):
        return (False, None, None, _("No cart to update."))

    if force_delete:
        qty = 0
    else:
        try:
            qty = int(request.POST.get('quantity'))
        except (TypeError, ValueError):
            return (False, cart, None, _("Bad quantity."))
        if qty < 0:
            qty = 0

    try:
        itemid = int(request.POST.get('cartitem'))
    except (TypeError, ValueError):
        return (False, cart, None, _("Bad item number."))

    try:
        cartitem = CartItem.objects.get(pk=itemid, cart=cart)
    except CartItem.DoesNotExist:
        return (False, cart, None, _("No such item in your cart."))

    if qty == 0:
        cartitem.delete()
        cartitem = NullCartItem(itemid)
    else:
        cartitem.quantity = qty
        cartitem.save()

    dispatcher.send(signal=satchmo_cart_changed, cart=cart, request=request)
    return (True, cart, cartitem, "")

def display(request, cart=None, error_message='', default_view_tax=NOTSET):
    """Display the items in the cart."""
    
    if default_view_tax == NOTSET:
        default_view_tax = config_value('TAX', 'DEFAULT_VIEW_TAX')
            
    if not cart:
        cart = Cart.objects.from_request(request)
                
    context = RequestContext(request, {
        'cart': cart,
        'error_message': error_message,
        'default_view_tax' : default_view_tax,
        })
    return render_to_response('base_cart.html', context)

def add(request, id=0):
    """Add an item to the cart."""
    #TODO: Error checking for invalid combos
    log.debug('FORM: %s', request.POST)
    formdata = request.POST.copy()
    productslug = None
    if formdata.has_key('productname'):
        productslug = formdata['productname']
    try:
        product = Product.objects.get(slug=productslug)
        log.debug('found product: %s', product)
        p_types = product.get_subtypes()
        details = []
{'fraction_non_alphanumeric': 0.09715857011915674, 'fraction_numerical': 0.0021387106630003055, 'mean_word_length': 3.254061078622482, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2103133', 'n_tokens_mistral': 1940, 'n_tokens_neox': 1916, 'n_words': 535}