Ejemplo n.º 1
0
def save(request, product_id):
    """
    Save a product
    """
    form = ProductForm(request.POST)

    if int(product_id) > 0:
        product = Product.objects.get(pk=product_id)
        form = ProductForm(request.POST, instance=product)
        
    if not form.is_valid():
        messages.add_message(request, messages.ERROR, _(u'Tarkista tuotteen tiedot'))
        return render(request, 'products/form.html', {
            'form': form, 'product_id': product_id
            })
        
    product = form.save()
    data = form.cleaned_data

    try:
        if product.code in request.session.get('gsx_data'):
            gsx_data = request.session['gsx_data'].get(product.code)
            for k, v in gsx_data.items():
                GsxData.objects.create(key=k, value=v,
                    references="product", reference_id=product.id)
    except Exception, e:
        print form.errors
Ejemplo n.º 2
0
def edit_product(request, pk=None, code=None, group='all'):
    """
    Edits a Product! :-)
    """
    initial = {}
    product = Product()

    data = prep_list_view(request, group)

    if pk is not None:
        product = get_object_or_404(Product, pk=pk)
        form = ProductForm(instance=product)

    if not group == 'all':
        cat = get_object_or_404(ProductCategory, slug=group)
        initial = {'categories': [cat]}
        data['group'] = cat

    product.update_photo()

    if code is not None:
        product = cache.get(code)

    form = ProductForm(instance=product, initial=initial)
    InventoryFormset = inlineformset_factory(Product,
                                             Inventory,
                                             extra=1,
                                             max_num=1,
                                             exclude=[])

    formset = InventoryFormset(
        instance=product,
        initial=[{'location': request.user.location}]
    )

    if request.method == "POST":

        form = ProductForm(request.POST, request.FILES, instance=product)

        if form.is_valid():

            product = form.save()
            content_type = ContentType.objects.get(model="product")

            for a in request.POST.getlist("attachments"):
                doc = Attachment.objects.get(pk=a)
                product.attachments.add(doc)

            tags = [x for x in request.POST.getlist('tag') if x != '']

            for t in tags:
                tag, created = TaggedItem.objects.get_or_create(
                    content_type=content_type,
                    object_id=product.pk,
                    tag=t)
                tag.save()

            formset = InventoryFormset(request.POST, instance=product)

            if formset.is_valid():
                formset.save()
                messages.success(request, _(u"Product %s saved") % product.code)
                return redirect(product)
            else:
                messages.error(request, _('Error in inventory details'))
        else:
            messages.error(request, _('Error in product info'))

    data['form'] = form
    data['product'] = product
    data['formset'] = formset
    data['title'] = product.title

    return render(request, "products/form.html", data)