Exemple #1
0
def create_song(request, category_slug):
    """
    @param form:        the Form-object representation of product to render page   
    @param category:    the model representation of category of product     
    @param product:    the model representation of new product     
    @return:            the representation of detail category page(If the fields are filled) 
                            or create_product page(If the fields are empty)
    """
    form = ProductForm(request.POST or None, request.FILES or None)
    category = get_object_or_404(Category, slug=category_slug)
    context = {
        'category': category,
        'form': form,
    }
    if form.is_valid():
        products = category.product_set.all()
        for p in products:
            if p.name == form.cleaned_data.get("name"):
                context['error_message'] = 'You already added that product'
                return render(request, ADDRESS_CREATE_PRODUCT, context)
        product = form.save(commit=False)
        product.category = category
        product.save()
        return render(request, ADDRESS_DETAIL_CATEGORY, {'category': category})

    return render(request, ADDRESS_CREATE_PRODUCT, context)
Exemple #2
0
def sell(request):
    """ Handles adding a new product by user

    Arguments:
        request -- [standard Django request arg]

    Returns:
        redirect -- on success go back to home page
        render -- render the sell html page with correct f
    """
    if request.method == "POST":
        post_data = request.POST.dict()
        profile = UserProfile.objects.get(user=request.user)
        post_data['seller'] = profile
        product_form = ProductForm(request.POST, request.FILES, post_data)
        if product_form.is_valid():
            product = product_form.save(commit=False)
            product.seller = profile
            product.save()
            logger.info("Product registered (sell): %s", product)
            return redirect(reverse("app:index"))
        else:
            print(product_form.errors)
            logger.info("Product registered (sell) failed: %s",
                        product_form.errors)
    else:
        product_form = ProductForm()
        logger.info("Product form rendered")

    return render(request, "app/sell.html", context={
        "form": product_form,
    })
def update(request, id):
    if not request.user.is_authenticated:
        return render(request, 'app/Admin/signin.html')
    else:
        product = Product.objects.get(id=id)
        form = ProductForm(request.POST, instance=product)
    if form.is_valid():
        form.save()
        return redirect("/show")
    return render(request, 'app/Admin/datatable.html', {'product': product})
def ArticleCreate(request):
    form = ProductForm()
    if request.method == "POST":
        form = ProductForm(data=request.POST, files=request.FILES)
        if form.is_valid():
            product = form.save(commit=False)
            product.user = request.user
            product.save()
            return HttpResponseRedirect(product.get_absolute_url())
    return render(request,
                  'BlogTemplate/article_create.html',
                  context={'form': form})
def ArticleUpdate(request, slug):
    product = get_object_or_404(Product, slug=slug)
    form = ProductForm(instance=product,
                       data=request.POST or None,
                       files=request.FILES or None)
    if request.user != product.user:
        return HttpResponseForbidden()

    if form.is_valid():
        product.update()
        return HttpResponseRedirect(reverse('details', kwargs={'slug': slug}))
    context = {'form': form, 'product': product}
    return render(request, 'BlogTemplate/article_update.html', context=context)
def add_post(request):
    if request.method == 'POST':
        print("POST")
        productForm = ProductForm(request.POST, request.FILES)

        if productForm.is_valid():
            print("Valid")
            product = productForm.save(commit=False)
            product.save()
    else:
        print("GET")
        productForm = ProductForm()
    return render(request, "app/add_post.html", {'productForm': productForm})    
def emp(request):
    if not request.user.is_authenticated:
        return render(request, 'app/Admin/signin.html')
    else:
        if request.method == "POST":
            form = ProductForm(request.POST, request.FILES)
            if form.is_valid():
                try:
                    form.save()
                    return redirect('/show')
                except:
                    pass
        else:
            form = ProductForm()
        return render(request, 'app/Admin/formdatatable.html', {'form': form})
Exemple #8
0
def product_operation(request, id):
    error = 0
    form = ProductForm(request.POST or None)
    product = Product.objects.filter(id=id).first()
    if product is None:
        return HttpResponse(status=404)
    if request.method == 'POST':
        if form.is_valid():
            try:
                product.name = form.cleaned_data['name']
                product.manufacturer = form.cleaned_data['manufacturer']
                product.nutrient.kcal = form.cleaned_data['kcal']
                product.nutrient.portion = form.cleaned_data['portion']
                product.nutrient.protein = form.cleaned_data['protein']
                product.nutrient.carbohydrates = form.cleaned_data[
                    'carbohydrates']
                product.nutrient.fats = form.cleaned_data['fats']
                product.nutrient.save()
                product.save()
                return redirect(request.POST.get('next') + '?successful=1')
            except:
                error = 1
    if request.method == 'DELETE':
        if request.is_ajax():
            try:
                product.delete()
            except:
                return HttpResponse(status=500)
            return HttpResponse('OK', status=200)
    context = {
        'form': form,
        'product': product,
        'is_edit': True,
        'error': error
    }
    return render(request, 'product_create.html', context)
Exemple #9
0
def product_creation(request):
    form = ProductForm(request.POST or None)
    error = 0
    if request.method == 'POST':
        if form.is_valid():
            try:
                product = Product()
                nutrientes = Nutrientes()
                product.name = form.cleaned_data['name']
                product.manufacturer = form.cleaned_data['manufacturer']
                product.user = request.user
                nutrientes.kcal = form.cleaned_data['kcal']
                nutrientes.portion = form.cleaned_data['portion']
                nutrientes.protein = form.cleaned_data['protein']
                nutrientes.carbohydrates = form.cleaned_data['carbohydrates']
                nutrientes.fats = form.cleaned_data['fats']
                nutrientes.save()
                product.nutrient = nutrientes
                product.save()
                return redirect(request.POST.get('next') + '?successful=1')
            except:
                error = 1
    context = {'form': form, 'error': error}
    return render(request, 'product_create.html', context)