def search(name): token = request.headers.get('token') if (verifyToken(token)): conn = connection.db_connection() cursor = conn.cursor() if request.method == 'GET': cursor.execute("SELECT * FROM beer where name = ?", (str(name), )) rows = cursor.fetchall() if (len(rows) > 0): for r in rows: beer = r if beer is not None: cursor.close() conn.close() return jsonify(beer), 200 else: cursor.close() conn.close() return "Something wrong", 404 else: cursor.close() conn.close() return "Something wrong", 404 else: return "Your token is expired"
def beers(): token = request.headers.get('token') if (verifyToken(token)): conn = connection.db_connection() cursor = conn.cursor() if request.method == 'GET': cursor.execute("SELECT * FROM beer") beers = [ dict(id=row[0], name=row[1], percentageAlcohol=row[2], category=row[3], stock=row[4], image=row[5]) for row in cursor.fetchall() ] if beers is not None: cursor.close() conn.close() return jsonify(beers) if request.method == 'POST': data = request.get_json() new_name = data['name'] new_percentageAlcohol = data['percentageAlcohol'] new_category = data['category'] new_stock = data['stock'] new_image = data['image'] sql = """INSERT INTO beer (name,percentageAlcohol,category,stock,image) VALUES (?,?,?,?,?) """ cursor.execute(sql, (new_name, new_percentageAlcohol, new_category, new_stock, new_image)) conn.commit() cursor.close() conn.close() return f"Beer with the id {cursor.lastrowid} created successful" else: return "Your token is expired"
def list_update(code_id,user_id) : token = request.headers.get('token') if(verifyToken(token)) : conn = connection.db_connection() cursor= conn.cursor() if request.method=='DELETE' : sqllist = """DELETE FROM codelist where code_id = %s and user_id=%s """ cursor.execute(sqllist,(int(code_id),int(user_id))) conn.commit() cursor.close() conn.close() return code_id if request.method == 'PUT' : sql = """UPDATE codelist SET status = ? WHERE user_id=%s and code_id=%s """ data = request.get_json() status = data['status'] updated_code = { 'user_id' : user_id, 'code_id' : code_id, 'status' : bool(status), } cursor.execute(sql,(status,int(code_id),int(user_id))) conn.commit() cursor.close() conn.close() return jsonify(updated_code),200 else : return "Your token is expired"
def changePanier(beer_id, user_id): token = request.headers.get('token') if (verifyToken(token)): conn = connection.db_connection() cursor = conn.cursor() if request.method == 'DELETE': sqllist = """DELETE FROM beerlist where user_id = ? and beer_id=? """ cursor.execute(sqllist, (int(user_id), int(beer_id))) conn.commit() cursor.close() conn.close() return f"Beer {int(beer_id)} remove from your favlist" if request.method == 'PUT': sql = """UPDATE beerlist SET quantite = ? WHERE user_id=? and beer_id=? """ data = request.get_json() quantite = data["quantite"] updated_beer_quantite = { 'user_id': int(user_id), 'beer_id': int(beer_id), 'quantite': quantite, } cursor.execute(sql, (quantite, int(user_id), int(beer_id))) conn.commit() cursor.close() conn.close() return jsonify(updated_beer_quantite) else: return "Your token is expired"
def saveQrCode(QrCode_Value): token = request.headers.get('token') if(verifyToken(token)): conn = connection.db_connection() cursor = conn.cursor() codeName = None if request.method == 'GET': sql = ("SELECT name FROM code WHERE identifiant_QRCode =%s") cursor.execute(sql, (QrCode_Value,)) rows = cursor.fetchall() for r in rows: codeName = r if codeName is None: cursor.close() conn.close() return "Something wrong", 404 else: username = getpass.getuser() if(os.path.isdir("/home/"+username+"/Documents/GoStyle") == False): os.mkdir("/home/"+username+"/Documents/GoStyle") img = qrcode.make(QrCode_Value) img.save( "/home/"+username+"/Documents/GoStyle/Datamatrix_"+str(codeName[0])+".png") return "OK", 200 else: return "Your token is expired", 500
def remove_from_favorite(beer_id, user_id): token = request.headers.get('token') if (verifyToken(token)): conn = connection.db_connection() cursor = conn.cursor() if request.method == 'DELETE': sqllist = """DELETE FROM beerlist where user_id = ? and beer_id=? """ cursor.execute(sqllist, (int(user_id), int(beer_id))) conn.commit() cursor.close() conn.close() return f"Beer {beer_id} remove from your favlist" else: return "Your token is expired"
def single_beer(id): token = request.headers.get('token') if(verifyToken(token)) : conn = connection.db_connection() cursor = conn.cursor() beer = None if request.method == 'GET': cursor.execute("SELECT * FROM beer WHERE id =?",(int(id),)) rows = cursor.fetchall() for r in rows : beer = r if beer is not None : cursor.close() conn.close() return jsonify(beer),200 else : cursor.close() conn.close() return "Something wrong",404 if request.method == 'PUT' : sql = """UPDATE beer SET name = ?, percentageAlcohol=?, category=? WHERE id=? """ data = request.get_json() name= data["name"] percentageAlcohol = data["percentageAlcohol"] category = data["category"] updated_beer = { 'id':id, 'name' : name, 'percentageAlcohol' : percentageAlcohol, 'category' : category } cursor.execute(sql,(name,percentageAlcohol,category,int(id))) conn.commit() cursor.close() conn.close() return jsonify(updated_beer) if request.method == 'DELETE': sql = """ DELETE FROM beer WHERE id=? """ cursor.execute(sql,(int(id),)) conn.commit() cursor.close() conn.close() return "Beer with the id {} has been deleted".format(id),200 else : return "Your token is expired"
def codes(): token = request.headers.get('token') if(verifyToken(token)): conn = connection.db_connection() cursor = conn.cursor() if request.method == 'GET': cursor.execute("SELECT * FROM code") codes = [ dict(id=row[0], name=row[1], expiration_date=row[2], image=row[3], description=row[4], value=row[5], identifiant_QRCode=row[6], is_unique=row[7], category=row[8]) for row in cursor.fetchall() ] if codes is not None: cursor.close() conn.close() return jsonify(codes), 200 if request.method == 'POST': data = request.get_json() cursor.execute("SELECT name from code") names= cursor.fetchall() if(any(data['name'] in sublist for sublist in names)) : return "Nom de code déja existant",406 new_name = data['name'] new_expiration_date = data['expiration_date'] new_image = data['image'] new_description = data['description'] new_value = data['value'] new_identifiantQRCode = new_val = hashlib.md5(b"GoStyle").hexdigest()+";"+ datetime.now().strftime("%Y%m%d%H%M%S") new_is_unique = data['is_unique'] new_category = data['category'] sql = """INSERT INTO code (name,expiration_date,image,description,value,identifiant_QRCode,is_unique,category) VALUES (%s,%s,%s,%s,%s,%s,%s,%s) """ cursor.execute(sql, (new_name, new_expiration_date, new_image, new_description, new_value, new_identifiantQRCode, new_is_unique, new_category)) created_code = { 'name': new_name, 'expiration_date': new_expiration_date, 'image': new_image, 'description': new_description, 'value': new_value, 'identifiant_QRCode': new_identifiantQRCode, 'is_unique': new_is_unique, 'catgeory': new_category } conn.commit() cursor.close() conn.close() return jsonify(created_code), 201 else: return "Your token is expired"
def code_by_user(user_id) : token = request.headers.get('token') if(verifyToken(token)) : conn = connection.db_connection() cursor= conn.cursor() if request.method=='GET' : cursor.execute("SELECT code.id, code.name, code.expiration_date, code.image, code.description, code.value, code.identifiant_QRCode, code.is_unique, code.category, codelist.status from code INNER JOIN codelist ON code.id = codelist.code_id where codelist.user_id =%s",(int(user_id),)) list_codes = [ dict(id=row[0],name = row[1], expiration_date=row[2], image=row[3],description=row[4], value=row[5],identifiant_QRCode=row[6],is_unique = row[7],category=row[8],status=row[9]) for row in cursor.fetchall() ] cursor.close() conn.close() return jsonify(list_codes),200 else : return "Your token is expired"
def favorite(): token = request.headers.get('token') if (verifyToken(token)): conn = connection.db_connection() cursor = conn.cursor() if request.method == 'POST': data = request.get_json() beer_id = data['beer_id'] user_id = data['user_id'] sqllist = """INSERT INTO beerlist (user_id,beer_id) VALUES (?,?) """ cursor.execute(sqllist, (user_id, beer_id)) conn.commit() cursor.close() conn.close() return f"Beer {beer_id} add to your favlist" else: return "Your token is expired"
def beer_by_user(user_id): token = request.headers.get('token') if (verifyToken(token)): conn = connection.db_connection() cursor = conn.cursor() if request.method == 'GET': cursor.execute( "SELECT beer_id,quantite from beerlist where user_id = ?", (int(user_id), )) beers = [ dict(beer_id=row[0], quantite=row[1]) for row in cursor.fetchall() ] cursor.close() conn.close() return jsonify(beers) else: return "Your token is expired"
def beer_by_user(user_id): token = request.headers.get('token') if (verifyToken(token)): conn = connection.db_connection() cursor = conn.cursor() if request.method == 'GET': cursor.execute( "SELECT * FROM beer WHERE id in (SELECT beer_id from beerlist where user_id=?)", (str(user_id), )) beers = [ dict(id=row[0], name=row[1], percentageAlcohol=row[2], category=row[3]) for row in cursor.fetchall() ] cursor.close() conn.close() return jsonify(beers) else: return "Your token is expired"
def code_by_qrCode(identifiant_QRCode): token = request.headers.get('token') if(verifyToken(token)): conn = connection.db_connection() cursor = conn.cursor() code = None if request.method == 'GET': cursor.execute( "SELECT * FROM code WHERE identifiant_QRCode =?", (identifiant_QRCode,)) rows = cursor.fetchall() for r in rows: code = r if code is not None: cursor.close() conn.close() return jsonify(code), 200 else: cursor.close() conn.close() return "Something wrong", 404 else: return "Your token is expired"
def codelist(): token = request.headers.get('token') if(verifyToken(token)) : conn = connection.db_connection() cursor= conn.cursor() if request.method=='POST' : data = request.get_json() code_id=data['code_id'] user_id = data['user_id'] status = data['status'] sqllist = """INSERT INTO codelist (code_id,user_id,status) VALUES (%s,%s,%s) """ cursor.execute(sqllist,(code_id,user_id,status)) cerated_item = { 'code_id' : code_id, 'user_id' : user_id, 'status' : status } conn.commit() cursor.close() conn.close() return jsonify(cerated_item),200 else : return "Your token is expired"
def single_code(id): token = request.headers.get('token') if(verifyToken(token)): conn = connection.db_connection() cursor = conn.cursor() code = None if request.method == 'GET': cursor.execute("SELECT * FROM code WHERE id =%s", (int(id),)) rows = cursor.fetchall() for r in rows: code = r if code is not None: cursor.close() conn.close() return jsonify(code), 200 else: cursor.close() conn.close() return "Bad user id", 400 if request.method == 'PUT': sql = """UPDATE code SET name = %s, expiration_date=%s, image=%s, description=%s, value=%s, identifiant_QRCode=%s, is_unique =%s, category = %s WHERE id=%s """ data = request.get_json() name = data["name"] expiration_date = data["expiration_date"] image = data["image"] description = data["description"] value = data["value"] identifiant_QRCode = data["identifiant_QRCode"] is_unique = data["is_unique"] category = data['category'] updated_code = { 'id': id, 'name': name, 'expiration_date': expiration_date, 'image': image, 'description': description, 'value': value, 'identifiant_QRCode': identifiant_QRCode, 'is_unique': is_unique, 'category': category } cursor.execute(sql, (name, expiration_date, image, description, value, identifiant_QRCode, is_unique, category, int(id))) conn.commit() cursor.close() conn.close() return jsonify(updated_code) if request.method == 'DELETE': cursor.execute("SELECT * FROM code WHERE id =%s", (int(id),)) rows = cursor.fetchall() for r in rows: code = r if code is not None: sql_list = """DELETE FROM codelist WHERE code_id=%s """ cursor.execute(sql_list, (int(id),)) conn.commit() sql = """ DELETE FROM code WHERE id=%s """ cursor.execute(sql, (int(id),)) conn.commit() cursor.close() conn.close() return jsonify(code), 200 else : return "Erreur de suppression",500 else: return "Your token is expired"