Beispiel #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"})
Beispiel #2
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"})