Пример #1
0
 def post(self, request, *args, **kwargs):
     form = ProductForm(request.POST, request.FILES)
     if form.is_valid():
         form.save()
         return self.form_valid(form)
     else:
         return self.form_invalid(form)
Пример #2
0
def product_update_view(request, pk):
    product = get_object_or_404(Product, pk=pk)
    if request.method == 'GET':
        form = ProductForm(
            data={
                "name": product.name,
                "description": product.description,
                "category": product.category,
                "remainder": product.remainder,
                "price": product.price
            })
        return render(request,
                      "update.html",
                      context={
                          "form": form,
                          "product": product
                      })
    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if form.is_valid():
            product.name = form.cleaned_data["name"]
            product.description = form.cleaned_data["description"]
            product.category = form.cleaned_data["category"]
            product.remainder = form.cleaned_data['remainder']
            product.price = form.cleaned_data['price']
            product.save()
            return redirect("product_detail", pk=product.pk)
        else:
            return render(request,
                          "update.html",
                          context={
                              "form": form,
                              "product": product
                          })
Пример #3
0
def add_product(request):
    if request.method == 'GET':
        form = ProductForm()
        return render(request,
                      'add_product.html',
                      context={
                          'form': form,
                          'category': category
                      })
    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if form.is_valid():
            data = form.cleaned_data
            product = Product.objects.create(name=data['name'],
                                             description=data['description'],
                                             category=data['category'],
                                             count=data['count'],
                                             price=data['price'])
            return redirect('product_view', pk=product.pk)
        else:
            return render(request,
                          'add_product.html',
                          context={
                              'form': form,
                              'category': category
                          })
def product_create_view(request):
    if request.method == "GET":
        form = ProductForm()
        return render(request,
                      'create.html',
                      context={
                          'category_choices': CATEGORY_CHOICES,
                          'form': form
                      })
    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if form.is_valid():
            product = Product.objects.create(
                name=form.cleaned_data['name'],
                description=form.cleaned_data['description'],
                category=form.cleaned_data['category'],
                amount=form.cleaned_data['amount'],
                price=form.cleaned_data['price'])
            return redirect('product_view', pk=product.pk)

        else:
            return render(request,
                          'create.html',
                          context={
                              'category_choices': CATEGORY_CHOICES,
                              'form': form
                          })
Пример #5
0
def product_update_view(request, pk):
    product = get_object_or_404(Product, pk=pk)
    if request.method == 'GET':
        form = ProductForm(
            data={
                'name': product.name,
                'description': product.description,
                'category': product.category,
                'amount': product.amount,
                'price': product.price
            })
        return render(request,
                      'update.html',
                      context={
                          'form': form,
                          'product': product
                      })
    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if form.is_valid():
            product.name = form.cleaned_data['name']
            product.description = form.cleaned_data['description']
            product.category = form.cleaned_data['category']
            product.amount = form.cleaned_data['amount']
            product.price = form.cleaned_data['price']
            product.save()
            return redirect('product_detail', pk=product.pk)
        else:
            return render(request,
                          'update.html',
                          context={
                              'form': form,
                              'product': product
                          })
Пример #6
0
def add_new_product(request, *args, **kwargs):

    if request.method == 'GET':

        form = ProductForm()

        return render(request, 'create.html', context={'form': form})

    elif request.method == 'POST':

        form = ProductForm(data=request.POST)

        if form.is_valid():

            Product.objects.create(
                name=form.cleaned_data['name'],
                description=form.cleaned_data['description'],
                category=form.cleaned_data['category'],
                amount=form.cleaned_data['amount'],
                price=form.cleaned_data['price'])

            return redirect('index')

        else:

            return render(request, 'create.html', context={'form': form})
def product_create_view(request, *args, **kwargs):
    if request.method == "GET":
        return render(request,
                      'product_create.html',
                      context={'form': ProductForm()})
    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            description = form.cleaned_data['description']
            category = form.cleaned_data['category']
            amount = form.cleaned_data['amount']
            price = form.cleaned_data['price']
            product = Product.objects.create(name=name,
                                             description=description,
                                             category=category,
                                             amount=amount,
                                             price=price)
            return redirect('product_view', pk=product.pk)
        else:
            return render(request,
                          'product_create.html',
                          context={'form': form})
    else:
        return HttpResponseNotAllowed(permitted_methods=['GET', 'POST'])
Пример #8
0
def product_create_view(request):
    if request.method == 'GET':
        form = ProductForm()
        return render(request, 'create.html', context={'form': form})
    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if not form.is_valid():
            return render(request, 'create.html', context={'form': form})
        Product.objects.create(name=form.cleaned_data['name'],
                               description=form.cleaned_data['description'],
                               category=form.cleaned_data['category'],
                               remain=form.cleaned_data['remain'],
                               price=form.cleaned_data['price'])
        return redirect('index')
Пример #9
0
def product_create_view(request, *args, **kwargs):
    if request.method == 'GET':
        form = ProductForm()
        return render(request, 'create.html', context={"form": form})
    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if form.is_valid():
            product = Product.objects.create(
                name=form.cleaned_data["name"],
                desctiption=form.cleaned_data["description"],
                category=form.cleaned_data["category"],
                remainder=form.cleaned_data["remainder"],
                price=form.cleaned_data['price'])
            return redirect("product_detail_view", pk=product.pk)
        else:
            return render(request, "create.html", context={"form": form})
Пример #10
0
def product_update_view(request, pk):
    product = get_object_or_404(Product, pk=pk)
    if request.method == 'GET':
        form = ProductForm(
            data={
                'name': product.name,
                'description': product.description,
                'category': product.category,
                'amount': product.amount,
                'price': product.price
            })
        return render(request,
                      'product_update.html',
                      context={
                          'form': form,
                          'product': product
                      })
    elif request.method == 'POST':
        product.name = request.POST.get('name')
        product.description = request.POST.get('description')
        product.category = request.POST.get('category')
        product.amount = request.POST.get('amount')
        product.price = request.POST.get('price')

        product.save()
        return redirect('product_view', pk=product.pk)
Пример #11
0
def edit_product(product_id):
    product = Product().fetch_product(product_id, current_user.get_id())
    form = ProductForm()

    for item in Category().fetch_user_categories():
        form.categories.choices.append(
            [item.category_name, item.category_name])

    for item in Attachment().fetch_user_attachments():
        form.attachment_file.choices.append(
            [item.attachment_filename, item.original_attachment_filename])

    if not product:
        flash(['Something went wrong. Please try again later'])
        return redirect(url_for('dashboard.products'))

    form.product_name.data = product.name

    if product.product_type == 'item':
        linked_product_items = ProductItem().fetch_active_product_items(
            product.product_items)
        form.items.data = ''.join(linked_product_items)

    else:
        form.attachment_file.data = product.product_attachment.attachment_filename
        form.stock.data = product.stock

    form.price.data = float(product.price)

    return render_template('/dashboard/edit-product.html',
                           product=product,
                           form=form,
                           flashed_message=get_flashed_messages(),
                           attachments=Attachment().fetch_user_attachments())
Пример #12
0
def product_add_view(request, *args, **kwargs):
    if request.method == 'GET':
        form = ProductForm()
        return render(request, 'add.html', context={'form': form})
    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if form.is_valid():
            product = Product.objects.create(
                name=form.cleaned_data['name'],
                description=form.cleaned_data['description'],
                category=form.cleaned_data['category'],
                balance=form.cleaned_data['balance'],
                cost=form.cleaned_data['cost'],
            )
            return redirect('product_view', pk=product.pk)
        else:
            return render(request, 'add.html', context={'form': form})
def product_update_view(request, pk):
    product = get_object_or_404(Product, pk=pk)
    form = ProductForm(data=request.POST)

    if request.method == 'GET':
        form = ProductForm(
            data={
                'name_of_product': product.name_of_product,
                'description': product.description,
                'status': product.status,
                'date': product.date,
                'count': product.count
            })
        return render(request,
                      'update.html',
                      context={
                          'product': product,
                          'form': form
                      })

    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if form.is_valid():
            product.name_of_product = form.cleaned_data['name_of_product']
            product.date = form.cleaned_data['date']
            product.status = form.cleaned_data['status']
            product.description = form.cleaned_data['description']
            product.count = form.cleaned_data['count']
            product.save()
            return redirect('product_view', pk=product.pk)
Пример #14
0
def product_create_view(request, *args, **kwargs):
    if request.method == 'GET':
        form = ProductForm()
        return render(request, 'product_create.html', context={'form': form})
    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if form.is_valid():
            data = form.cleaned_data
            task = Product.objects.create(name=data['name'],
                                          description=data['description'],
                                          category=data['category'],
                                          amount=data['amount'],
                                          price=data['price'])
            return redirect('product_view', pk=task.pk)
        else:
            return render(request,
                          'product_create.html',
                          context={'form': form})
Пример #15
0
def product_update_view(request, pk):
    product = get_object_or_404(Product, pk=pk)
    if request.method == "GET":
        form = ProductForm(
            initial={
                'name': product.name,
                'description': product.description,
                'category': product.category,
                'amount': product.amount,
                'price': product.price
                # форматирование перед выводом для DateTime.
                #'publish_at': make_naive(product.publish_at)\
                #.strftime(BROWSER_DATETIME_FORMAT)
                # для дат выглядит просто как:
                # 'publish_at': article.publish_at
            })
        return render(request,
                      'product_update.html',
                      context={
                          'form': form,
                          'product': product
                      })
    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if form.is_valid():
            # Article.objects.filter(pk=pk).update(**form.cleaned_data)
            product.name = form.cleaned_data['name']
            product.description = form.cleaned_data['description']
            product.category = form.cleaned_data['category']
            product.amount = form.cleaned_data['amount']
            product.price = form.cleaned_data['price']
            product.save()
            return redirect('product_view', pk=product.pk)
        else:
            return render(request,
                          'product_update.html',
                          context={
                              'product': product,
                              'form': form
                          })
    else:
        return HttpResponseNotAllowed(permitted_methods=['GET', 'POST'])
Пример #16
0
def submit_new_product() -> redirect:
    form = ProductForm()
    attachments = Attachment().fetch_user_attachments()

    for item in Category().fetch_user_categories():
        form.categories.choices.append(
            [item.category_name, item.category_name])

    for item in attachments:
        form.attachment_file.choices.append(
            [item.attachment_filename, item.original_attachment_filename])

    if not form.validate_on_submit():
        flash(list(form.errors.values())[0])
        return redirect(url_for('dashboard.create_product'))

    Product().add(request)

    flash(['Product Successfully Created'])
    return redirect(url_for('dashboard.products'))
Пример #17
0
def add_product(request, *args, **kwargs):
    if request.method == 'GET':
        form = ProductForm()
        return render(request,
                      'add_product.html',
                      context={
                          'form': form,
                          'product_status_choices': product_status_choices
                      })
    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if form.is_valid():
            product = Products.objects.create(
                name=form.cleaned_data['name'],
                description=form.cleaned_data['description'],
                status=form.cleaned_data['status'],
                balance=form.cleaned_data['balance'],
                price=form.cleaned_data['price'])
            return redirect('product_view', pk=product.pk)
        else:
            return render(request, 'add_product.html', context={'form': form})
def product_create_view(request, *args, **kwargs):
    if request.method == 'GET':

        form = ProductForm()

        return render(request, 'create.html', context={'form': form})

    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if form.is_valid():
            product = Product.objects.create(
                name_of_product=form.cleaned_data['name_of_product'],
                date=form.cleaned_data['date'],
                description=form.cleaned_data['description'],
                count=form.cleaned_data['count'],
                status=form.cleaned_data['status'])

            return redirect('product_view', pk=product.pk)

        else:

            return render(request, 'create.html', context={'form': form})
def form_storage_view(request, *args, **kwargs):
    if request.method == 'GET':
        form = ProductForm()
        return render(request, 'form.html', context={
            'form': form,
            'status_choices': PRODUCT_OTHER_CHOICE
        })
    elif request.method == 'POST':
        form = ProductForm(data=request.POST)

        if not form.is_valid():
            return render(request, 'form.html', context={'form': form, 'status_choices': PRODUCT_OTHER_CHOICE})

        Product.objects.create(
            name=form.cleaned_data['name'],
            description=form.cleaned_data['description'],
            category=form.cleaned_data['category'],
            amount=form.cleaned_data['amount'],
            price=form.cleaned_data['price']

        )
        return redirect('index')
def index_view(request, *args, **kwargs):
    find_form = FindProductForm(data=request.GET)
    if find_form.is_valid():
        name = find_form.cleaned_data['name']
        data = Product.objects.filter(name=name, )
        return render(request,
                      'index.html',
                      context={
                          'products': data,
                          'form': ProductForm(),
                          'find_form': find_form
                      })
    else:
        data = Product.objects.filter(amount__gte=1).order_by(
            'category', 'name')
        return render(request,
                      'index.html',
                      context={
                          'form': ProductForm(),
                          'find_form': FindProductForm(),
                          'products': data
                      })
def detailed_update_view(request, pk):
    task = get_object_or_404(Product, pk=pk)
    if request.method == 'GET':
        form = ProductForm(data={
            'name': task.name,
            'description': task.description,
            'category': task.category,
            'amunt': task.amount,
            'price': task.price
        })
        return render(request, 'update.html', context={'form': form, 'task': task})
    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if form.is_valid():
            task.name = form.cleaned_data['name']
            task.description = form.cleaned_data['description']
            task.category = form.cleaned_data['category']
            task.amount = form.cleaned_data['amount']
            task.price = form.cleaned_data['price']
            task.save()
            return redirect('detailed', pk=task.pk)

        return render(request, 'update.html', context={'form': form, 'task': task})
Пример #22
0
def product_edit_view(request, pk):
    product = get_object_or_404(Product, pk=pk)
    if request.method == 'GET':
        form = ProductForm(data={
            'name': product.name,
            'description': product.description,
            'category': product.category,
            'balance': product.balance,
            'cost': product.cost,
        })
        return render(request, 'edit.html', context={'form': form, 'product': product})
    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if form.is_valid():
            product.name = form.cleaned_data['name']
            product.description = form.cleaned_data['description']
            product.category = form.cleaned_data['category']
            product.balance = form.cleaned_data['balance']
            product.cost = form.cleaned_data['cost']
            product.save()
            return redirect('product_view', pk=product.pk)
        else:
            return render(request, 'edit.html', context={'form': form, 'product': product})
Пример #23
0
def update_product(product_id):
    form = ProductForm()
    attachments = Attachment().fetch_user_attachments()

    for item in Category().fetch_user_categories():
        form.categories.choices.append(
            [item.category_name, item.category_name])

    for item in attachments:
        form.attachment_file.choices.append(
            [item.attachment_filename, item.original_attachment_filename])

    if not form.validate_on_submit():
        flash(list(form.errors.values())[0])
        return redirect(
            url_for('dashboard.edit_product', product_id=product_id))

    if not Product().update(request, product_id):
        flash(['Something went wrong. Please try again later'])
        return redirect(url_for('dashboard.products'))

    flash(['Product Successfully Updated'])
    return redirect(url_for('dashboard.edit_product', product_id=product_id))
Пример #24
0
def update_product(request, pk):
    try:
        products = get_object_or_404(Products, pk=pk)
        if request.method == 'GET':
            form = ProductForm(
                data={
                    'name': products.name,
                    'description': products.description,
                    'status': products.status,
                    'balance': products.balance,
                    'price': products.price
                })
            return render(request,
                          'edit.html',
                          context={
                              'products': products,
                              'form': form
                          })
        elif request.method == "POST":
            form = ProductForm(data=request.POST)
            if form.is_valid():
                products.name = form.cleaned_data['name']
                products.description = form.cleaned_data['description']
                products.status = form.cleaned_data['status']
                products.balance = form.cleaned_data['balance']
                products.price = form.cleaned_data['price']
                products.save()
                return redirect('product_view', pk=products.pk)
            else:
                return render(request,
                              'edit.html',
                              context={
                                  'form': form,
                                  'products': products
                              })
    except Products.DoesNotExist:
        return HttpResponseNotFound("<h2>Article not found</h2>")
Пример #25
0
def product_update_view(request, pk):
    product = get_object_or_404(Product, pk=pk)
    if request.method == "GET":
        form = ProductForm(
            initial={
                'name': product.name,
                'description': product.description,
                'category': product.category,
                'amount': product.amount,
                'price': product.price
            })
        return render(request,
                      'product_update.html',
                      context={
                          'form': form,
                          'Products': product
                      })
    elif request.method == 'POST':
        form = ProductForm(data=request.POST)
        if form.is_valid():
            product.name = form.cleaned_data['name']
            product.description = form.cleaned_data['description']
            product.category = form.cleaned_data['category']
            product.amount = form.cleaned_data['amount']
            product.price = form.cleaned_data['price']
            product.save()
            return redirect('product_view')
        else:
            return render(request,
                          'product_update.html',
                          context={
                              'Products': product,
                              'form': form
                          })
    else:
        return HttpResponseNotAllowed(permitted_methods=['GET', 'POST'])
Пример #26
0
def create_product() -> render_template:
    form = ProductForm()
    attachments = Attachment().fetch_user_attachments()

    for item in Category().fetch_user_categories():
        form.categories.choices.append(
            [item.category_name, item.category_name])

    for item in attachments:
        form.attachment_file.choices.append(
            [item.attachment_filename, item.original_attachment_filename])

    return render_template('/dashboard/create-product.html',
                           form=form,
                           flashed_message=get_flashed_messages(),
                           attachments=attachments)