vendredi 30 avril 2021

How to write if condition for mysql db data (condition: qty == 0 ) then email should be send to the admin

In the below code I have made a route to /editproduct in this the products can be added, edit or delete the products. I want to send an email to the admin with an alert message as the product qty is 0 . when the product is edited to zero qty then the database updated with zero. if the qty in MySQL DB is zero then an email should send. how to write that condition. please help and sorry for the bad English grammar.

@app.route('/editProduct',methods = ['POST'])
def editProduct():
   if request.method == 'POST' and 'ProductID' in request.form and 'NEWProductName' in request.form and 'NEWProductDescription' in request.form and 'NEWProductQty':
      try:
         productID = request.form['ProductID']
         productName = request.form['NEWProductName']
         productDescription=request.form['NEWProductDescription']
         ProductQty=request.form['NEWProductQty']
         cursor = mysql.connection.cursor()
         cursor.execute("UPDATE Products SET productName = % s,productDescription = % s, QTY = % s WHERE productID = % s",(productName,productDescription,ProductQty,productID) )
         #products = cursor.fetchone()
         #TEXT = "Hello "+username + ",\n\n"+ "the stock " +productName + "is empty" 
         #message  = 'Subject: {}\n\n{}'.format("Stock Alert", TEXT)
         mysql.connection.commit()
         cursor = mysql.connection.cursor()
         QTY = "SELECT * from Products where QTY == 0;"
         cursor.execute(QTY)
         if QTY == 0:
             email_alert('prakprak2002@gmail.com')
         msg = "Product Edited "
         cursor.close()
      except:
         #con.rollback()
         msg = "error in operation"
      
      finally:
         return redirect(url_for('product', msg = msg))#+"?msg="+msg)

Aucun commentaire:

Enregistrer un commentaire