예제 #1
0
def api_inventory_export():
    """
    API endroint to export the current inventory session as .csv.
    """
    if request.method == 'GET':
        path = os.path.join(SAVE_PATH, 'export.csv')
        Inventory.export_csv(path)
        return send_file(path, as_attachment=True)
예제 #2
0
def api_part_detail_create_inventory(part_id):
    in_db = Part.query.get(part_id)
    if in_db is None:
        return jsonify({"response": "error"})

    i = Inventory(part=in_db, quantity=0)
    db.session.add(i)
    db.session.commit()

    return jsonify(i.to_dict())
예제 #3
0
 def inventory_creation(name, barcode, quantity=0):
     p = Part(name=name, barcode=barcode)
     db.session.add(p)
     i = Inventory(part=p, quantity=quantity)
     db.session.add(i)
     db.session.commit()
     return i
예제 #4
0
    def test_csv_export_1(self):
        BARCODE = "QWERTY1234"
        NAME = "BARCODE"
        p = Part(name=NAME, barcode=BARCODE)
        db.session.add(p)
        i = Inventory(part=p)
        db.session.add(i)
        db.session.commit()

        self.assertEqual(Inventory.query.count(), 1)
        output = Inventory._export_csv().getvalue().strip()
        self.assertEqual(len(output.split("\n")), 2)
        header, content = output.split("\n")
        self.assertEqual(
            header,
            "id,part_name,part_barcode,quantity,unit,created_at,updated_at")
        self.assertTrue("BARCODE,QWERTY1234,0.0," in content)
예제 #5
0
def api_part_detail_create_inventory(part_id):
    """
    API endpoint to create and inventory table linked for a selected `part_id`.
    """
    in_db = Part.query.get(part_id)
    if in_db is None:
        return jsonify({"response": "Unknown part id"})

    if Inventory.last_session_entries().filter(
            Inventory.part == in_db).count():
        return jsonify(
            {"response": "Inventory already exist for this part id"})

    i = Inventory(part=in_db, quantity=0)
    db.session.add(i)
    db.session.commit()

    return jsonify(i.to_dict())
예제 #6
0
    def test_csv_export_2(self):
        BARCODE = "QWERTY1234"
        NAME = "BARCODE"
        p = Part(name=NAME, barcode=BARCODE)
        db.session.add(p)
        i = Inventory(part=p)
        db.session.add(i)
        db.session.commit()

        self.assertEqual(Inventory.query.count(), 1)
        Inventory.archive()
        i = Inventory(part=p, quantity=2)
        db.session.add(i)
        db.session.commit()
        self.assertEqual(Inventory.query.count(), 2)
        output = Inventory._export_csv().getvalue().strip()
        self.assertEqual(len(output.split("\n")), 2)
        header, content = output.split("\n")
        self.assertTrue("BARCODE,QWERTY1234,2.0," in content)
예제 #7
0
    def test_inventory_query_3(self):
        TestDatabaseInventory.inventory_creation("BARCODE", "QWERTY1234")
        self.assertEqual(Inventory.query.count(), 1)
        TestDatabaseInventory.inventory_creation("BARCODE 2", "HELLO1234")
        self.assertEqual(
            len(Part.query.filter(Part.barcode == "HELLO1234").all()), 1)
        self.assertEqual(Inventory.query.count(), 2)

        p = Part.query.filter(Part.barcode == "HELLO1234").first()
        i = Inventory.retrieve_inventory_from_barcode("HELLO1234")
        self.assertEqual(i.part_id, p.id)
예제 #8
0
def api_parts():
    """
    API endpoint listing every parts present in the database.
    """
    if request.method == 'POST':
        form = request.form
        files = request.files
        if form.get("name", None) and form.get("barcode", None):
            name = form["name"]
            barcode = form["barcode"]
            part = Part(name=name, barcode=barcode)
            db.session.add(part)
            db.session.commit()
            return jsonify(part.to_dict())
        elif 'file' in files:
            column_name = form.get("csv_column_name", "Référence interne")
            column_barcode = form.get("csv_column_barcode", "Code Barre")
            csv_encoding = form.get("csv_encoding", "UTF-8")
            csv_file = files['file']
            if csv_file.filename == '':
                return redirect(request.url)
            if csv_file and is_csv(csv_file.filename):
                Inventory.archive()
                filename = os.path.join(SAVE_PATH, (csv_file.filename))
                current_app.logger.info("Saving " + filename)
                csv_file.save(filename)
                executor.submit(Part.import_csv, filename, {
                    "name": column_name,
                    "barcode": column_barcode,
                }, csv_encoding)

            return jsonify({"response": "ok"})
    else:
        return jsonify([
            x.to_dict() for x in Part.query.filter(Part.hidden == False).all()
        ])

    return jsonify({"response": "error"})
예제 #9
0
def api_inventory_export():
    if request.method == 'GET':
        path = os.path.join(SAVE_PATH, 'export.csv')
        Inventory.export_csv(path)
        return send_file(path, as_attachment=True)
예제 #10
0
    def test_inventory_float(self):
        TestDatabaseInventory.inventory_creation("BARCODE", "QWERTY1234", 1.55)
        self.assertEqual(Inventory.query.count(), 1)

        i = Inventory.retrieve_inventory_from_barcode("QWERTY1234")
        self.assertEqual(i.quantity, 1.55)