def login(): data = request.json if data: dataDict = json.loads(json.dumps(data)) if 'user' in dataDict and 'pass' in dataDict: cnx, cur = helper.start() query = 'SELECT `id` FROM Client WHERE `username`=%s AND `password`=%s' cur.execute(query, (dataDict['user'], dataDict['pass'])) theid = cur.fetchone() helper.stop(cnx, cur) if theid[0] > 0: if dataDict['booking']: # deal with booking cnx, cur = helper.start() query = 'SELECT MAX(id) FROM Booking WHERE `client` = %s AND `start_time` >= %s' cur.execute(query, (theid[0], datetime.datetime.now())) ids = cur.fetchone() helper.stop(cnx, cur) return Response(json.dumps({'bookings': ids}), status=200, mimetype='application/json') else: return Response(status=200) else: return helper.error() else: return helper.error() else: return helper.error()
def getNumLounger(): con, cur = helper.start() cur.execute('SELECT COUNT(Lounger.id) ' 'FROM Lounger;') numOfLounger = cur.fetchall() helper.stop(cur, con) numOfLounger = list(numOfLounger[0]) return numOfLounger[0]
def getLoungerInfo(id): con, cur = helper.start() chair_id = id cur.execute( 'SELECT Lounger.name, Lounger.desc' ' FROM Lounger' ' WHERE Lounger.id = %s' ' LIMIT 1;', (chair_id, )) lounger_info = cur.fetchall() helper.stop(cur, con) return lounger_info[0]
def stop(logfile, errfile): helper.run([ Command( "sudo /usr/local/nginx/sbin/nginx -s stop -c $TROOT/config/nginx.conf", True) ], logfile, errfile, os.environ['TROOT']) return helper.stop('unicorn', logfile, errfile)
def addCharge(booking, prices, quantities): con, cur = helper.start() #find the owner of the booking query = 'SELECT `Client`.`id` FROM `Client` INNER JOIN `Booking` ON `Client`.`id` = `Booking`.`client` WHERE `Booking`.`id` = %s' cur.execute(query, (booking, )) owner = cur.fetchone()[0] helper.stop(con, cur) #add the total of price[i] * quantities[i] to the owner's account total_charge = 0 for i in range(0, len(prices)): total_charge = total_charge + (prices[i] * quantities[i]) con, cur = helper.start() query = 'SELECT `spend` FROM `Client` WHERE id = %s' cur.execute(query, (owner, )) current_spend = cur.fetchone()[0] helper.stop(con, cur) total_charge = total_charge + current_spend con, cur = helper.start() query = 'UPDATE `Client` SET spend = %s WHERE `Client`.`id` = %s' cur.execute(query, (total_charge, owner)) con.commit() helper.stop(con, cur)
def getBooked(): con, cur = helper.start() current_dt = datetime.datetime.now() # Get Data cur.execute( 'SELECT Lounger.id' ' FROM Booking' ' INNER JOIN Lounger ON Booking.lounger = Lounger.id' ' WHERE Booking.start_time < %s AND Booking.finish_time > %s;', (current_dt, current_dt)) booking_result = cur.fetchall() # Close Down helper.stop(con, cur) # Turn tuple into array useful_results = [x for xs in booking_result for x in xs] print(useful_results) # Return array of integers return useful_results
def makeOrder(ids, quantities, booking): # Make the connection and cursor con, cur = helper.start() # Get the size of order table before the new order is added cur.execute('SELECT COUNT(Order.id) FROM password.Order;') number_before = cur.fetchone() number_before = number_before[0] helper.stop(con, cur) con, cur = helper.start() for i in range(0, len(ids)): query = 'INSERT INTO `Order` (booking, item, quantity) VALUES (%s, %s, %s)' cur.execute(query, (booking, ids[i], quantities[i])) con.commit() helper.stop(con, cur) con, cur = helper.start() # Get the size of order table after the new order is added cur.execute('SELECT COUNT(Order.id) FROM password.Order;') number_after = cur.fetchone() number_after = number_after[0] # Check th right number of orders have been added did_work = False if ((number_after - number_before) == len(ids)): did_work = True print(did_work) helper.stop(con, cur) return did_work
def stop(logfile, errfile): return helper.stop('trinidad', logfile, errfile)
def stop(logfile, errfile): helper.run([Command("sudo /usr/local/nginx/sbin/nginx -s stop -c $TROOT/config/nginx.conf", True)], logfile, errfile, os.environ['TROOT']) return helper.stop('unicorn', logfile, errfile)
def stop(logfile, errfile): helper.run([Command('rm -rf tmp/*', True)], logfile, errfile, os.environ['TROOT']) return helper.stop('thin', logfile, errfile)
def stop(logfile, errfile): return helper.stop('puma', logfile, errfile)
def stop(logfile, errfile): return helper.stop('torqbox', logfile, errfile)
def stop(logfile, errfile): helper.run([Command("rm -rf tmp/*", True)], logfile, errfile, os.environ["TROOT"]) return helper.stop("thin", logfile, errfile)
def order(): data = request.json if data: dataDict = json.loads(json.dumps(data)) if 'order' in dataDict and 'booking' in dataDict: cnx, cur = helper.start() #query = 'SELECT `id` FROM Client WHERE `username`=%s AND `password`=%s' #cur.execute(query, (dataDict['user'], dataDict['pass'])) #theid = cur.fetchone() #helper.stop(cnx, cur) ordered_items = [] prices = [] quantities = [] for i in range(0, len(dataDict['order'])): query = 'SELECT `id` FROM Item WHERE `name`=%s;' cur.execute(query, (dataDict['order'][i]['name'], )) ordered_items.append(cur.fetchone()[0]) print(ordered_items) helper.stop(cnx, cur) cnx, cur = helper.start() for i in range(0, len(dataDict['order'])): query = 'SELECT `price` FROM Item WHERE `id`=%s;' cur.execute(query, (ordered_items[i], )) prices.append(cur.fetchone()[0]) print(prices) for i in range(0, len(dataDict['order'])): quantities.append(dataDict['order'][i]['quantity']) print(quantities) print(dataDict['booking']) did_work = makeOrder(ordered_items, quantities, dataDict['booking']) if (did_work == True): addCharge(dataDict['booking'], prices, quantities) return Response(status=200) else: return helper.error() # if theid[0] > 0: # if dataDict['booking']: # # deal with booking # cnx, cur = helper.start() # query = 'SELECT MAX(id) FROM Booking WHERE `client` = %s AND `start_time` >= %s' # cur.execute(query, (theid[0], datetime.datetime.now())) # ids = cur.fetchone() # helper.stop(cnx, cur) # return Response(json.dumps({'bookings' : ids}), status=200, mimetype='application/json') # else: # return Response(status=200) #else: # return helper.error() else: return helper.error() else: return helper.error()
def stop(logfile, errfile): helper.run([Command('rm -rf tmp/*', True)], logfile, errfile, '.') return helper.stop('thin', logfile, errfile)
def book(): data = request.json if data: dataDict = json.loads(json.dumps(data)) if 'lounger' in dataDict and 'start_time' in dataDict and 'finish_time' in dataDict and 'client' in dataDict: now = datetime.datetime.now() if datetime.datetime.strptime( dataDict['start_time'], "%Y-%m-%d %H:%M:%S") >= now and datetime.datetime.strptime( dataDict['finish_time'], "%Y-%m-%d %H:%M:%S") > datetime.datetime.strptime( dataDict['start_time'], "%Y-%m-%d %H:%M:%S"): # Check client cnx, cur = helper.start() query = 'SELECT `id` FROM Client WHERE `id` = %s' cur.execute(query, (dataDict['client'], )) num = cur.rowcount cur.close() if num > 0: # Times & Client are okay # Check if the thing is free cur = cnx.cursor() query = 'SELECT `id` FROM Booking WHERE `start_time` <= %s AND `finish_time` >= %s' cur.execute( query, (dataDict['start_time'], dataDict['finish_time'])) num = cur.rowcount cur.close() if num == 0 and dataDict['lounger'] > 0: #Everything is good! cur = cnx.cursor() query = 'INSERT INTO Booking (`lounger`, `start_time`, `finish_time`, `client`) VALUES (%s, %s, %s, %s)' cur.execute( query, (dataDict['lounger'], dataDict['start_time'], dataDict['finish_time'], dataDict['client'])) cnx.commit() cur.close() # Now get the deails! cur = cnx.cursor() query = 'SELECT `id` FROM Booking WHERE `lounger` = %s AND `start_time` = %s AND `finish_time` = %s AND `client` = %s' cur.execute( query, (dataDict['lounger'], dataDict['start_time'], dataDict['finish_time'], dataDict['client'])) theid = cur.fetchone() helper.stop(cnx, cur) return Response(json.dumps({'booking': theid}), status=200, mimetype='application/json') else: print("1") return helper.error() else: print("2") return helper.error() else: print("3") return helper.error() else: return helper.error() else: return helper.error()
def stop(logfile, errfile): command = Command("sudo /usr/local/nginx/sbin/nginx -c $TROOT/config/nginx.conf -s stop ", True) helper.run([command], logfile, errfile, '.') return helper.stop('unicorn', logfile, errfile)
def stop(logfile, errfile): return helper.stop("puma", logfile, errfile)