コード例 #1
0
def add_item(request, ownerid):
    """
    Add an item to the wishlist.
    """
    O = get_object_or_404(Owner, key=ownerid)
    initial = {}

    if request.method == 'POST':
        wish = request.POST.get('wish', None)
        if 'http://' in wish:
            # If a URL was passed, use it to get information about the item.
            # Get the scraped information 
            scraped = scrape.item(wish)

            if scraped:
                initial['url'] = wish
                wish = scraped['name']
                initial['price'] = scraped['price']
        _p = request.POST.get('price', None)
        if _p is not None:
           initial['price'] = int(_p, 10)
        wish = O.wishes.create(name=wish, **initial)
        _t = [__t.strip() for __t in request.POST.get('tags', '').split(',') if len(__t.strip())]
        for __t in request.POST.getlist('tag:'):
           _t.append(__t.strip())
        if _t:
            wish.tags.add(*_t)
        return HttpResponseRedirect('/%s' % O.key)
    else:
        return wishlist(ownerid)
コード例 #2
0
ファイル: views.py プロジェクト: pigmonkey/django-wishlist
def add_item(request, url=None):
    """
    Add an item to the wishlist.
    """
    initial = {}

    # If a URL was passed, use it to get information about the item.
    if 'url' in request.GET:
        url = request.GET['url']
        initial['url'] = url

        # Get the scraped information 
        scraped = scrape.item(url)

        if scraped:
            initial['name'] = scraped['name']
            initial['price'] = scraped['price']

    if request.method == 'POST':
        form = AddItem(request.POST)
        if form.is_valid():
            new_item = form.save()
            if 'url' in request.GET:
                return HttpResponseRedirect(request.GET['url'])
            else:
                return HttpResponseRedirect(reverse('wishlist.views.wishlist'))
    else:
        form = AddItem(initial=initial)
    return render_to_response('wishlist/add_item.html',
        {'form': form, 'url': url},
        context_instance=RequestContext(request))