示例#1
0
文件: views.py 项目: rahcola/blackem
def add_content(request, pantry_id, category_id=False, product_id=False):
    """Add content to pantry or render appropriate form.

    Content is prompted in three steps:
    1. Ask for a category
    2. Ask for a product in choosen category
    3. Ask for an amount for the choosen product.

    User can only add contents to their own pantries.

    """
    if request.method == 'POST':
        form = ContentForm(request.POST)
        if form.is_valid():
            pantry = get_object_or_404(Pantry,
                                       pk=pantry_id,
                                       owner=request.user)
            product = get_object_or_404(Product, pk=product_id)
            try:
                content = Content.objects.get(pantry=pantry, product=product)
                content.amount += form.cleaned_data['amount']
            except ObjectDoesNotExist:
                content = Content(pantry=pantry,
                                  product=product,
                                  amount=form.cleaned_data['amount'])
            content.save()
        return redirect('pantries.views.detail', pantry_id)

    response_dict = {'pantry_id': pantry_id,
                     'categories': Category.objects.all(),
                     'no_logout': True}
    if category_id:
        response_dict.update(
            {'category_id': category_id,
             'category': Category.objects.get(pk=category_id),
             'products': Product.objects.filter(categories__pk=category_id)}
        )

    if product_id:
        response_dict.update(
            {'form': ContentForm(),
             'product': Product.objects.get(pk=product_id),
             'product_id': product_id}
        )
    return render_to_response('pantries/content_form.html',
                              response_dict,
                              context_instance=RequestContext(request))
示例#2
0
文件: views.py 项目: rahcola/blackem
def detail(request, shoppinglist_id):
    """Detailed view of the shoppinglist.

    List all items and give forms to check them as bought. Bought items are
    added to the pantry of this shoppinglist.

    """
    CheckItemFormSet = modelformset_factory(Item, extra=0, fields=('bought',))
    if request.method == 'POST':
        formset = CheckItemFormSet(request.POST)
        if formset.is_valid():
            for form in formset.forms:
                if form.cleaned_data['bought']:
                    item = get_object_or_404(Item,
                                             pk=form.cleaned_data['id'].pk,
                                             shoppinglist__pantry__owner=request.user)
                    if not item.bought:
                        try:
                            content = Content.objects.get(
                                product=item.product,
                                pantry=item.shoppinglist.pantry
                            )
                            content.amount += item.amount
                        except ObjectDoesNotExist:
                            content = Content(pantry=item.shoppinglist.pantry,
                                              product=item.product,
                                              amount=item.amount)
                        content.save()
                    item.delete()

    list = get_object_or_404(Shoppinglist,
                             pantry__owner=request.user,
                             pk=shoppinglist_id)
    formset = CheckItemFormSet(queryset=Item.objects.filter(shoppinglist=list))
    return render_to_response('shoppinglists/shoppinglist_detail.html',
                              {'items': zip(list.item_set.all(), formset.forms),
                               'formset': formset,
                               'list': list,
                               'logged': True},
                                context_instance=RequestContext(request))