예제 #1
0
    def mutate(self, info, productName, productPrice, productInventory):
        errorMsg = []
        #fail if any field is invalid
        if productInventory < 0:
            errorMsg.append(
                "Unable to set productInventory to value less than zero")
        if len(productName) == 0:
            errorMsg.append("Unable to set name to empty-string")
        if productPrice < 0:
            errorMsg.append("Unable to change price to negative value")
        if (abs(productPrice) * 100) - int(abs(productPrice) * 100) > 0:
            errorMsg.append("Price cannot have more than two decimal places")

        if len(errorMsg) == 0:
            errorMsg = None
            product = Product(productName=productName,
                              productPrice=productPrice,
                              productInventory=productInventory)
            product.save()
            return CreateProduct(productId=product.productId,
                                 productName=product.productName,
                                 productPrice=product.productPrice,
                                 productInventory=product.productInventory,
                                 errors=errorMsg)
        else:  #product creation fails
            return CreateProduct(productId=None,
                                 productName=None,
                                 productPrice=None,
                                 productInventory=None,
                                 errors=errorMsg)
예제 #2
0
    def post(self, request):
        form1 = MultiImages(request.POST, request.FILES)
        form = ProductForm(request.POST)

        if (form.is_valid() and form1.is_valid()):

            category = request.POST.get("category")
            brand = request.POST.get("brand")
            name = request.POST.get("name")
            price = request.POST.get("price")
            stock = request.POST.get("stock")

            form = Product(category=category,
                           brand=brand,
                           name=name,
                           price=price,
                           stock=stock)

            form.save()
            for file in request.FILES.getlist("image"):
                form1 = MultiImage(product_id=form, image=file)
                form1.save()
            return redirect('main:home_view')

        else:
            print(form.errors)
            return HttpResponse('not valid')
예제 #3
0
def new_product():
    form = ProductForm()
    if form.validate_on_submit():
        product = Product(product_name=form.product_name.data)
        db.session.add(product)
        db.session.commit()
        flash('Your product is successfully added in the product list!', 'success')
        return redirect(url_for('main.product'))
    return render_template('create.html', form=form, legend='New Product')
예제 #4
0
def add_product():
    form = ProductForm()
    if request.method == "POST" and form.validate():
        product_id = Product(name=form.name.data)
        db.session.add(product_id)
        db.session.commit()
        flash('Entered Product ID added!', 'success')
        return redirect(url_for('products'))
    return render_template('add_product.html', form=form)
예제 #5
0
 def create(self, validated_data):
     ingredient_used = validated_data.pop('ingredient_used')
     product = Product(**validated_data)
     product.save()
     ingredient_used_objs = [
         IngredientUsed(
             ingredient=item['ingredient'],
             quantity_used=item['quantity_used'],
             product=product,
             )
         for item in ingredient_used
         ]
     IngredientUsed.objects.bulk_create(ingredient_used_objs, ignore_conflicts=True)
     return product
예제 #6
0
def newproductadd(request):
    """
    new product addition into database
    """
    if request.method == "POST":
        products = cleaner(request.POST.getlist('product'))
        price = cleaner(request.POST.getlist('price'))
        for i in range(len(products)):
            p = Product(p_name=products[i],
                        p_price=price[i],
                        stocks=0)
            p.save()
    else:
        return HttpResponse("failed")
    return render(request, 'addconfirmation.html')
예제 #7
0
def process_product(form, request, manufacturer):
    pspec_file = request.files.getlist('pspec')[0]
    pimage_file = request.files.getlist('pimage')[0]
    pspec_path = pspec.save(pspec_file)
    pimage_path = pimage.save(pimage_file)
    product = Product(
        name=form.product.data,
        manufacturer_id=manufacturer.id,
        total_samples=form.total_samples.data,
        sample_counter=0,
        spec_file_name=pspec_path,
        spec_image_name=pimage_path,
    )
    db.session.add(product)
    db.session.flush()
    return product
예제 #8
0
def product():
    form = addproduct()
    eform = editproduct()
    details = Product.query.order_by(Product.prod_name).all()
    exists = bool(Product.query.order_by(Product.prod_name).all())
    if exists == False and request.method == 'GET':
        flash(f'Add products to view', 'info')
    elif eform.validate_on_submit() and request.method == 'POST':
        pname = request.form.get("productname", "")
        details = Product.query.order_by(Product.prod_name).all()
        prod = Product.query.filter_by(prod_name=pname).first()
        prod.prod_name = eform.editname.data
        prod.prod_qty = eform.editqty.data
        prod.prod_price = eform.editprice.data
        try:
            db.session.commit()
            flash(f'Your product  has been updated!', 'success')
            return redirect('/Product')
        except IntegrityError:
            db.session.rollback()
            flash(f'This product already exists', 'danger')
            return redirect('/Product')
        return render_template('product.html', details=details, eform=eform)

    elif form.validate_on_submit():
        product = Product(prod_name=form.prodname.data,
                          prod_qty=form.prodqty.data,
                          prod_price=form.prodprice.data)
        db.session.add(product)
        try:
            db.session.commit()
            flash(f'Your product {form.prodname.data} has been added!',
                  'success')
            return redirect(url_for('product'))
        except IntegrityError:
            db.session.rollback()
            flash(f'This product already exists', 'danger')
            return redirect('/Product')
    return render_template('product.html',
                           eform=eform,
                           form=form,
                           details=details)
예제 #9
0
from django.test import TestCase
from django.urls import reverse

from users.models import CustomUser as User
from inventory.models import Partner, Product, Transaction
from inventory.views import transact_api
# Create your tests here.

# custom entries into the database
product_insert = [
    Product(
        name=prod_name_list[i],
        description=prod_des[i],
        code=code_list[i],
        quantity=qty_list[i],
        cost=cost_list[i],
        price=price_list[i],
        responsible=User.objects.get(pk=1),
    ) for i in range(len(prod_name_list[i]))
]

Product.objects.bulk_create(product_insert)


class TransactionViewTestCase(TestCase):
    partner = 1
    prod_id_list = [2, 1, 3]
    trade_list_qty = [11, 2, 4]
    unit_price_list = [100, 120, 310]

    def test_perfect_receiving(self):