Exemple #1
0
def api_parts():
    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:
            file = files['file']
            if file.filename == '':
                return redirect(request.url)
            if file and is_csv(file.filename):
                filename = os.path.join(SAVE_PATH, (file.filename))
                current_app.logger.info("Saving " + filename)
                file.save(filename)
                executor.submit(Part.import_csv, filename, {
                    "default_code": "name",
                    "barcode": "barcode"
                })

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

    return jsonify({"response": "error"})
Exemple #2
0
 def test_part_unique(self):
     x = Part(name="BARCODE", barcode="QWERTY1234")
     db.session.add(x)
     db.session.commit()
     p = Part(name="BARCODE2", barcode="QWERTY1234")
     db.session.add(p)
     self.assertRaises(sqlalchemy.exc.IntegrityError, db.session.commit)
     db.session.rollback()
Exemple #3
0
 def test_csv_import_4(self):
     """
     Test the import of file with empty field that should be passed
     """
     self.assertEqual(Part.query.count(), 0)
     csv = io.StringIO(
         "name,barcode\nhello,world\nfoo,\n,456\n,433,\nfoo,bar\n")
     Part._import_csv_content(csv)
     self.assertEqual(Part.query.count(), 2)
Exemple #4
0
 def test_csv_import_3(self):
     """
     Test the import doesn't fail when importing the same data and erase the data
     """
     self.assertEqual(Part.query.count(), 0)
     csv_content = "name,barcode\nhello,world\nfoo,bar\n123,456\n"
     Part._import_csv_content(io.StringIO(csv_content))
     self.assertEqual(Part.query.count(), 3)
     csv_content = "name,barcode\nfoo,bar\n123,456\nfoo2,bar2\n324,561\n"
     Part._import_csv_content(io.StringIO(csv_content))
     self.assertEqual(Part.query.filter(Part.hidden == False).count(), 4)
Exemple #5
0
 def test_csv_re_import(self):
     """
     Test to import a CSV StringIO in the DB
     """
     self.assertEqual(Part.query.count(), 0)
     csv1 = io.StringIO("name,barcode\nhello,world\nfoo,bar\n123,456\n")
     Part._import_csv_content(csv1)
     self.assertEqual(Part.query.count(), 3)
     csv2 = io.StringIO("name,barcode\nhello,world\nfoo,bar\n")
     Part._import_csv_content(csv2)
     self.assertEqual(Part.query.filter(Part.hidden == False).count(), 2)
     self.assertEqual(Part.query.count(), 3)
Exemple #6
0
    def test_csv_import_latin_char(self):
        self.assertEqual(Part.query.count(), 0)
        name = "hèllô"
        barcode = "wèrld"
        csv = io.StringIO(("name,barcode\n%s,%s\n" % (name, barcode)))

        filename = "/tmp/%s" % (uuid.uuid4())
        with open(filename, "wb") as f:
            f.write(csv.getvalue().encode("latin1"))

        Part.import_csv(filename)
        self.assertEqual(Part.query.count(), 1)
        p = Part.query.get(1)
        self.assertEqual(p.name, name)
        self.assertEqual(p.barcode, barcode)
Exemple #7
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
Exemple #8
0
 def test_part_delete(self):
     x = Part(name="BARCODE", barcode="QWERTY1234")
     db.session.add(x)
     db.session.commit()
     self.assertNotEqual(Part.query.count(), 0)
     Part.query.delete()
     db.session.commit()
     self.assertEqual(Part.query.count(), 0)
Exemple #9
0
 def test_part_query(self):
     x = Part(name="BARCODE", barcode="QWERTY1234")
     db.session.add(x)
     db.session.commit()
     p = Part.query.filter(Part.barcode == "QWERTY1234").first()
     self.assertEqual(p.id, 1)
     self.assertEqual(p.name, "BARCODE")
     self.assertEqual(p.barcode, "QWERTY1234")
     self.assertEqual(p.counter, 0)
Exemple #10
0
 def test_csv_import_1(self):
     """
     Test to import a CSV file in the DB
     """
     self.assertEqual(Part.query.count(), 0)
     csv = io.StringIO("name,barcode\nhello,world\nfoo,bar\n123,456\n")
     filename = "/tmp/%s" % (uuid.uuid4())
     with open(filename, "w") as f:
         print(csv.getvalue(), file=f)
     Part.import_csv(filename)
     self.assertEqual(Part.query.count(), 3)
     p = Part.query.filter(Part.barcode == "bar").first()
     self.assertEqual(p.name, "foo")
     self.assertEqual(p.barcode, "bar")
     p = Part.query.filter(Part.barcode == "world").first()
     self.assertEqual(p.name, "hello")
     self.assertEqual(p.barcode, "world")
     p = Part.query.filter(Part.barcode == "456").first()
     self.assertEqual(p.name, "123")
     self.assertEqual(p.barcode, "456")
Exemple #11
0
 def test_csv_import_with_csv_map_1(self):
     """
     Test to import a CSV StringIO in the DB
     """
     self.assertEqual(Part.query.count(), 0)
     csv = io.StringIO(
         "default_name,barcode\nhello,world\nfoo,bar\n123,456\n")
     Part._import_csv_content(csv, {
         "name": "default_name",
         "barcode": "barcode"
     })
     self.assertEqual(Part.query.count(), 3)
     p = Part.query.filter(Part.barcode == "bar").first()
     self.assertEqual(p.name, "foo")
     self.assertEqual(p.barcode, "bar")
     p = Part.query.filter(Part.barcode == "world").first()
     self.assertEqual(p.name, "hello")
     self.assertEqual(p.barcode, "world")
     p = Part.query.filter(Part.barcode == "456").first()
     self.assertEqual(p.name, "123")
     self.assertEqual(p.barcode, "456")
Exemple #12
0
 def test_csv_import_with_csv_map_2(self):
     """
     Test to import a CSV StringIO in the DB with a CSV map
     """
     self.assertEqual(Part.query.count(), 0)
     csv = io.StringIO(
         '"Référence interne","Code Barre"\n"hello","world"\n"foo","bar"\n"123","456"\n'
     )
     Part._import_csv_content(csv, {
         "name": "Référence interne",
         "barcode": "Code Barre"
     })
     self.assertEqual(Part.query.count(), 3)
     p = Part.query.filter(Part.barcode == "bar").first()
     self.assertEqual(p.name, "foo")
     self.assertEqual(p.barcode, "bar")
     p = Part.query.filter(Part.barcode == "world").first()
     self.assertEqual(p.name, "hello")
     self.assertEqual(p.barcode, "world")
     p = Part.query.filter(Part.barcode == "456").first()
     self.assertEqual(p.name, "123")
     self.assertEqual(p.barcode, "456")
Exemple #13
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"})
Exemple #14
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)
Exemple #15
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)
Exemple #16
0
 def test_part_long_name(self):
     longname = "X" * 256
     x = Part(name=longname, barcode="foo")
     db.session.add(x)
     db.session.commit()
     self.assertEqual(len(x.name), 128)
Exemple #17
0
 def test_part_creation(self):
     p = Part(name="BARCODE", barcode="QWERTY1234")
     db.session.add(p)
     db.session.commit()
     self.assertEqual(Part.query.count(), 1)