示例#1
0
def edit_list_items(request, list_id=None):
    """
    'activates' a saved list.
    """
    if list_id:
        try:
            list_object = get_object_or_404(models.List, pk=list_id, user=request.user)
            request.session['list_object'] = list_object
        except models.List.DoesNotExist:
            return HttpResponseRedirect(reverse('create_list'))

    list_name = lists.get_list_name(request)
    
    for list_item in list_object.listitem_set.all().select_related():
        co = list_item.content_object
        item_key = lists.make_item_key(co)
        object_hash = lists.make_object_hash(co)
        lists.add_item(list_name, item_key, object_hash)
    
    request.session['list_name'] = list_name    
    request.session['list_enabled'] = True    
    request.session['list_object'] = list_object    
    request.session.modified = True

    return HttpResponseRedirect(reverse('list_detail', args=(list_object.pk,list_object.slug,)))
示例#2
0
def add_remove_item(request):
    """
    Expects POST data with the following values:
        * content_type
        * object_id
        
    Only content types (models) with a valid `LIST_ENABLED` attribute will be allowed
    """
    
    # The list we're working with
    list_name = lists.get_list_name(request)
    
    # Grab the POST data we'll need
    
    # Action is 'add' or 'remove'
    action = request.POST.get('action', 'add')

    content_type = request.POST.get('content_type', None)
    object_id = request.POST.get('object_id', None)

    # Load the object from the database
    ct = ContentType.objects.get(name=content_type)
    co = ct.get_object_for_this_type(pk=object_id)
    item_key = lists.make_item_key(co, ct)
    
    if action == "add":
        object_hash = lists.make_object_hash(co)
        lists.add_item(list_name, item_key, object_hash)

    if action == "remove":
        lists.remove_item(list_name, item_key)

    # Create the total for this list
    if hasattr(co, 'list_total_field'):
        total_field = getattr(co, co.list_total_field)
        list_total = lists.make_total(list_name, action, total_field)
    
    t = select_template(["blocks/ahah_list.html",])
    c = RequestContext(request, RequestContext(request))
    html = t.render(c)
    
    
    if request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest':
        return HttpResponse(json.dumps(
            {
            'total' : list_total,
            'html' : html, 
            'action' : action,
            'list_item_id' : item_key,
            }))
        
    res = HttpResponseRedirect(request.META['HTTP_REFERER'])
    return res