コード例 #1
0
ファイル: app.py プロジェクト: pmadrid/Backend_InventoryApp
def crear_producto():
    try:
        if request.method == 'POST':
            product_name = request.form['productname']
            product_cant = request.form['productcant']
            product_cost = request.form['productcost']
            product_desc = request.form['productdesc']
            img = request.form['productimg']
            img_path = "static\\Images\\" + img
            product_img = convertToBinaryData(img_path)
            error = None

            close_db()
            db = get_db()

            if db.execute('SELECT id_pro FROM Productos WHERE nombre_pro = ?', (product_name,)).fetchone() is not None:
                error = "El correo ya existe"
                flash(error)
                return render_template('CrearProducto.html')

            db.execute(
                'INSERT INTO Productos (nombre_pro, cantidad_pro, costo_pro, descripcion_pro, image_pro) Values(?,?,?,?,?)',
                (product_name, product_cant, product_cost, product_desc, product_img))
            db.commit()

            return render_template('CrearProducto.html')
        else:
            return render_template('CrearProducto.html')
    except Exception as e:
        print("Ocurrió un error cuando intentaste crear un Producto", e)
        return render_template('CrearProducto.html')
コード例 #2
0
ファイル: app.py プロジェクト: pmadrid/Backend_InventoryApp
def mod_producto():
    try:
        if request.method == 'POST':
            product_id = request.form['productid']
            product_name = request.form['productname']
            product_cant = request.form['productcant']
            product_cost = request.form['productcost']
            product_desc = request.form['productdesc']
            error = None

            close_db()
            db = get_db()
            delete_button = request.form.get('delete_button', None)

            if delete_button == 'pressed':
                borrar_producto(int(product_id))
                return redirect(url_for('mod_producto'))
            
            img = request.form['productimg']
            img_path = "static\\Images\\" + img
            product_img = convertToBinaryData(img_path)
            

            db.execute(
                'UPDATE Productos SET nombre_pro=?, cantidad_pro=?, costo_pro=?, descripcion_pro=?, image_pro=? WHERE id_pro=?',
                (product_name, product_cant, product_cost, product_desc, product_img, product_id))
            db.commit()
            
            return redirect(url_for('productos'))
        else:
            lista_prod = ['Id Producto a Modificar', 'Nuevo Nombre Producto', 'Nueva Cantidad Producto', 'Nueva Costo Producto', 'Nueva Descripción Producto', 'Nueva Imagen Producto']
            return render_template('modificarProducto.html', data = lista_prod)

    except Exception as e:
        print("Ocurrió un error cuando intentaste modificar un usuario", e)
        return redirect(url_for('productos'))
コード例 #3
0
def mod_product(idPro=None):
    if g.user[4] != "Administrador":
        print(g.user[4])
        return redirect('/error')
    print("pasa por aqui")
    try:
        if request.method == 'POST':
            prod_id = request.form['productId']
            prod_name = request.form['productName']
            prod_quantity = request.form['productQuantity']
            prod_description = request.form['productDescription']
            # prod_mod_image = request.form['Image']

            db = get_db()

            if db.execute('SELECT id FROM productos WHERE id=?',
                          (idPro, )).fetchone() is None:
                error = 'El producto NO existe'.format(idPro)
                flash(error)
                return render_template('modificarProducto.html')
            print("Producto Modificado")
            db.execute(
                'UPDATE productos SET referencia=?, nombre=?, cantidad=?, descripcion=?  WHERE id=?',
                (prod_id, prod_name, prod_quantity, prod_description, idPro))

            db.commit()
            # DEIZY
            # IMAGE
            # check if the post request has the file part
            if 'file' not in request.files:
                print('No file part')
            else:
                file = request.files['file']
                # If the user does not select a file, the browser submits an
                # empty file without a filename.
                if file.filename == '':
                    print('No selected file')
                    product_list = db.execute(
                        'SELECT * FROM productos;').fetchall()
                    return render_template('producto.html',
                                           products=product_list)
                if file and allowed_file(file.filename):
                    filename = secure_filename(file.filename)
                    print(filename.split('.'))
                    ext_photo = filename.split('.')
                    ext_photo = ext_photo[1]
                    name_photo = prod_name + '.' + ext_photo
                    file.save(
                        os.path.join(app.config['UPLOAD_FOLDER'], filename))
                    route_photo = 'resources/' + filename
                    user_photo = convertToBinaryData(route_photo)

                    db.execute(
                        'UPDATE productos SET foto=?, foto_name=? WHERE id=?',
                        (user_photo, name_photo, idPro))

                    db.commit()

                    # crear imagen en static/fotos
                    writeTofile(user_photo,
                                'static/images/productos/' + name_photo)
                else:
                    flash('extension no permitida')
                    product = db.execute('SELECT * FROM productos WHERE id=?',
                                         (idPro, )).fetchone()
                    return render_template('modificarProducto.html',
                                           product=product)
            product_list = db.execute('SELECT * FROM productos;').fetchall()
            return render_template('producto.html', productos=product_list)

        if request.method == 'GET' and idPro:
            db = get_db()
            product = db.execute('SELECT * FROM productos WHERE id=?',
                                 (idPro, )).fetchone()
            #print(product)
            return render_template('modificarProducto.html', product=product)

        return render_template('principalAdmin.html')
    except Exception as e:
        print("Ocurrio un eror:", e)
        return render_template('principalAdmin.html')
コード例 #4
0
def add_employee():
    if g.user[4] != "Administrador":
        print(g.user[4])
        return redirect('/error')
    try:
        if request.method == 'POST':
            username = request.form['usuario']
            password = request.form['password']
            email = request.form['email']
            tipo = request.form['tipo']
            error = None

            close_db()
            db = get_db()

            if not utils.isUsernameValid(username):
                error = "El usuario debe ser alfanumerico"
                flash(error)
                return render_template('agregarUsuario.html')

            if not utils.isEmailValid(email):
                error = 'Correo inválido'
                flash(error)
                return render_template('agregarUsuario.html')

            if not utils.isPasswordValid(password):
                error = 'La contraseña debe tener por los menos una mayúcscula y una mínuscula y 8 caracteres'
                flash(error)
                return render_template('agregarUsuario.html')

            if db.execute('SELECT id FROM usuarios WHERE correo=?',
                          (email, )).fetchone() is not None:
                error = 'El correo ya existe'.format(email)
                flash(error)
                return render_template('agregarUsuario.html')

            hashpass = generate_password_hash(password)

            db.execute(
                'INSERT INTO usuarios (usuario,correo,contraseña,tipo) VALUES (?,?,?,?)',
                (username, email, hashpass, tipo))

            db.commit()
            serverEmail = yagmail.SMTP('*****@*****.**',
                                       'Karen.1234')

            serverEmail.send(
                to=email,
                subject='Activa tu cuenta',
                contents='Bienvenido, usa este link para activar tu cuenta')
            flash('Revisa tu correo para activar tu cuenta')
            # DEIZY
            # IMAGE
            # check if the post request has the file part
            if 'file' not in request.files:
                print('No file part')
            else:
                file = request.files['file']
                # If the user does not select a file, the browser submits an
                # empty file without a filename.
                if file.filename == '':
                    print('No selected file')
                    users_list = db.execute(
                        'SELECT * FROM usuarios;').fetchall()
                    return render_template('usuarios.html', users=users_list)
                if file and allowed_file(file.filename):
                    filename = secure_filename(file.filename)
                    print(filename.split('.'))
                    ext_photo = filename.split('.')
                    ext_photo = ext_photo[1]
                    name_photo = username + '.' + ext_photo
                    file.save(
                        os.path.join(app.config['UPLOAD_FOLDER'], filename))
                    route_photo = 'resources/' + filename
                    user_photo = convertToBinaryData(route_photo)

                    db.execute(
                        'UPDATE usuarios SET foto=?, foto_name=? WHERE usuario=?',
                        (user_photo, name_photo, username))

                    db.commit()

                    # crear imagen en static/fotos
                    writeTofile(user_photo,
                                'static/images/fotos/' + name_photo)
                else:
                    flash('extension no permitida')
                    return render_template('agregarUsuario.html')

            users_list = db.execute('SELECT * FROM usuarios;').fetchall()
            return render_template('usuarios.html', users=users_list)

        return render_template('agregarUsuario.html')
    except Exception as e:
        print("Ocurrio un eror:", e)
        return render_template('agregarUsuario.html')