示例#1
0
def basket(request, **kwargs):
    # get the template
    template_name = kwargs.get('template_name', 'ccbasket/basket.html')
    if request.method == 'POST':
        # get the instance for the form
        try:
            contenttype_pk = request.POST['contenttype_pk']
            object_pk = request.POST['object_pk']
            contenttype = ContentType.objects.get(pk=contenttype_pk)
            instance = contenttype.get_object_for_this_type(pk=object_pk)
            config = BasketRegistry.get(instance)
        except Exception, e:
            raise Http404('contenttype_pk/object_id in post are incorrect')
        # now get the form for the instance
        form = config.form(request.POST, instance=instance)
        # now process as normal
        if form.is_valid():
            item = form.save()
            # get the basket name
            basket_name = getattr(settings,
                'CCBASKET_SESSION_KEY_NAME',
                ccbasket_settings.CCBASKET_SESSION_KEY_NAME)
            # now add it to the basket
            basket = request.session[basket_name]
            # prep the options if the form has an options method
            try:
                options = form.options()
            except AttributeError:
                options = None
            # now process 
            basketitem = basket.add(
                item,
                quantity=form.cleaned_data['quantity'],
                request=request,
                options=options)
            # wrap up
            if basketitem:
                messages.success(request, 'Basket Updated')
            url = kwargs.get('next') or \
                    request.POST.get('next') or \
                    reverse('ccbasket:basket')
            return HttpResponseRedirect(url)
示例#2
0
def basketform(instance):
    config = BasketRegistry.get(instance)
    form = config.form(instance=instance)
    return {
        'form': form
    }