Beispiel #1
0
def items(request):
    StoreItemFormSet = inlineformset_factory(
        parent_model=StoreItem, model=StoreItemVariant, form=StoreItemVariantForm, extra=5)

    # Handle form data
    if request.method == 'POST':
        if not request.user.has_perm('store.add_storeitem'):
            raise Http403

        item_form = StoreItemForm(request.POST, request.FILES)
        variant_formset = StoreItemFormSet(request.POST, prefix="nested", instance=item_form.instance)
        if item_form.is_valid() and variant_formset.is_valid():
            item = item_form.save()
            variant_formset.save()
            logger.info('Store Item "{}" added.'.format(item.name), extra={'user': request.user})
            return HttpResponseRedirect(reverse('manage-store:items'))
    else:
        item_form = StoreItemForm()
        variant_formset = StoreItemFormSet(prefix="nested", instance=item_form.instance)

    # Get items
    m_items = StoreItem.objects.all()

    # Render response
    return admin_render(request, "admin_store/items.html", {
        'items': m_items,
        'item_form': item_form,
        'variant_formset': variant_formset
    })
Beispiel #2
0
def edit_item(request, item_id):
    if not request.user.has_perm('store.change_storeitem'):
        raise Http403

    StoreItemFormSet = inlineformset_factory(
        parent_model=StoreItem, model=StoreItemVariant, form=StoreItemVariantForm, extra=3)

    # Get Item
    item = get_object_or_404(StoreItem, pk=item_id)
        
    # Handle form data
    if request.method == 'POST':
        variant_formset = StoreItemFormSet(request.POST, instance=item)
        item_form = StoreItemForm(request.POST, request.FILES, instance=item)
        if item_form.is_valid() and variant_formset.is_valid():
            item_form.save()
            variant_formset.save()
            logger.info('Store Item "{}" edited.'.format(item.name), extra={'user': request.user})
            return HttpResponseRedirect(reverse('manage-store:edit_item', args=(item.id,)))
    else:
        item_form = StoreItemForm(instance=item)
        variant_formset = StoreItemFormSet(instance=item)

    # Render response
    return admin_render(request, "admin_store/itemedit.html", {
        'item_form': item_form,
        'variant_formset': variant_formset
    })
Beispiel #3
0
def edit_item(request, item_id):
    if not request.user.has_perm('store.change_storeitem'):
        raise Http403
        
    # Get Item
    item = get_object_or_404(StoreItem, pk=item_id)
        
    # Handle form data
    if request.method == 'POST':
        form = StoreItemForm(request.POST, request.FILES, instance=item)
        if form.is_valid():
            form.save()
            logger.info('Store Item "'+item.name+'" edited.', extra={'user': request.user})
            return HttpResponseRedirect(reverse('manage-store:items'))
    else:
        form = StoreItemForm(instance=item)

    # Render response
    return admin_render(request, "admin_store/itemedit.html", {
        'editform': form,
    })
Beispiel #4
0
def items(request):
    # Handle form data
    if request.method == 'POST':
        if not request.user.has_perm('store.add_storeitem'):
            raise Http403
        
        form = StoreItemForm(request.POST, request.FILES)
        if form.is_valid():
            item = form.save(commit=False)
            logger.info('Store Item "'+item.name+'" added.', extra={'user': request.user})
            item.save()
            return HttpResponseRedirect(reverse('manage-store:items'))
    else:
        form = StoreItemForm()

    # Get items
    items = StoreItem.objects.all()

    # Render response
    return admin_render(request, "admin_store/items.html", {
        'items': items,
        'addform': form,
    })