Exemple #1
0
def edit(key):
    """Edit Method."""
    try:
        element = Inventory.objects.filter(deleted=False,
                                           id=key).first()
    except Exception:
        flash("Elemento No encontrado", "error")
        return redirect(url_for("inventory.list"))
    if request.method == 'GET':
        form = InventoryForm(request.form, element)
        return render_template("inventory/create.html",
                               action="edit",
                               form=form,
                               menu=principal_menu(),
                               config=config)
    else:
        form = InventoryForm(request.form)
        if form.validate_on_submit():
            element.update(**form.data)
            flash("Elemento Actualizado", "success")
            return redirect(url_for("inventory.list"))
        return render_template("inventory/create.html",
                               action="edit",
                               form=form,
                               menu=principal_menu(),
                               config=config)
Exemple #2
0
def postimage(request):

    if request.method == 'POST':
        form = InventoryForm(request.POST, request.FILES)
        if form.is_valid():
            try:
                data = predict.run(request.FILES['image'])
            except:
                pass
            instance = Inventory(image=request.FILES['image'],
                                 prediction=data['description'])
            instance.save()
            # image = instance.image

        # data = {'data': 'otherdata'}
        # data = request.FILES
        # Inventory.objects.create(image=data)
        # Inventory.save()

        # data = predict.run(data)
        return HttpResponse(json.dumps(data), content_type='application/json')
    else:
        return HttpResponse(json.dumps(
            {"nothing to see": "this isn't happening"}),
                            content_type="application/json")
Exemple #3
0
def add(request):
	if request.POST:
		form = InventoryForm(request.POST, request.FILES)
		if form.is_valid():
			form.save()
			return HttpResponseRedirect('/inventory/all')

	else:
		form = InventoryForm()

	args = {}
	args.update(csrf(request))
	args['form'] = form
	return render_to_response('add_inventory.html', args)	
Exemple #4
0
def create():
    """Create Method."""
    form = InventoryForm(request.form)
    if form.validate_on_submit():
        obj = Inventory()
        form.populate_obj(obj)
        obj.deleted = False
        Inventory.objects.insert(obj)
        flash("Elemento creado", "success")
        return redirect(url_for("inventory.list"))
    return render_template("inventory/create.html",
                           action="create",
                           form=form,
                           menu=principal_menu(),
                           config=config)
Exemple #5
0
    def post(self, request, inventory_id=None):
        inventory = Inventory()
        if inventory_id:
            inventory = Inventory.objects.get(pk=inventory_id)
        inventory_form = InventoryForm(request.POST, request.FILES,
            instance=inventory)
        if inventory_form.is_valid():
            inventory_form.save()
            return redirect("inventory_home")

        return self.render_to_response({
            "form": inventory_form,
            'inventory': inventory,
            'inventory_id': inventory_id,
        })
Exemple #6
0
def update_items(pid):
    """
    Update inventory
    """
    item = Inventory.query.get_or_404(pid)
    form = InventoryForm(obj=Inventory)

    # update changes
    item.name = form.name.data
    item.price = form.price.data
    item.quantity = form.quantity.data
    db.session.commit()

    return json.dumps(item.serialize()), 200
Exemple #7
0
def view(key):
    """Edit Method."""
    try:
        element = Inventory.objects.filter(deleted=False,
                                           id=key).first()
    except Exception:
        flash("Elemento No encontrado", "error")
        return redirect(url_for("inventory.list"))
    form = InventoryForm(request.form, element)
    return render_template("inventory/create.html",
                           action="view",
                           form=form,
                           menu=principal_menu(),
                           config=config)
Exemple #8
0
def save_item():
    """
    Add an item
    """
    form = InventoryForm()
    if request.method == 'POST':
        data = request.get_json()
        item = Inventory(form.pid.data, form.name.data, form.price.data,
                         form.quantity.data)
        db.session.add(item)
        db.session.commit()
        return json.dumps(item.serialize()), 200

    if request.method == 'GET':
        return jsonify('Add a new item'), 200
def add_item():
    """Route and function for adding inventory item"""
    form = InventoryForm(request.form)
    message = None
    if request.method == 'POST':
        try:
            name = form.name_field.data
            quantity = form.quantity_field.data
            unit = form.unit_field.data
            inventory = InventoryClass(1, name.lower(), unit.lower(),
                                       quantity.lower())
            message = inventory.insert()
        except Exception as e:
            message = "Python error: {}".format(e)
    return render_template("add-item.html",
                           template_form=form,
                           message=message)
Exemple #10
0
def view_item(id):
    """Route and function for viewing and updating inventory item"""
    form = InventoryForm(request.form)
    message = None
    # Get item from database
    items = db_connect.get_item(id)
    # Set formfield values with db value
    form.name_field.data = items[0][1]
    form.unit_field.data = items[0][2]
    if request.method == 'POST':
        try:
            # Get formfield values and create inventory object
            name = form.name_field.data
            quantity = form.quantity_field.data
            unit = form.unit_field.data
            inventory = InventoryClass(1, name.lower(), unit, quantity)
            # Update inventory object value
            inventory.update()
            message = "Item successfully updated!"
        except Exception as e:
            message = "Python error: {}".format(e)
    return render_template("view-item.html",
                           template_form=form,
                           message=message)