Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask
- from flask import jsonify
- from datetime import datetime, timedelta
- from MySQLdb.cursors import DictCursor
- from flaskext.mysql import MySQL
- import os
- app = Flask('restaurant')
- DB_USER = os.getenv('MYSQL_USER', 'user')
- DB_PASSWORD = os.getenv('MYSQL_PASSWORD', '55F3WzF7yudAd8L#')
- DB_NAME = os.getenv('MYSQL_DATABASE', 'restaurant')
- DB_HOST = '172.19.0.2'
- app.config.update(
- MYSQL_DATABASE_HOST=DB_HOST,
- MYSQL_DATABASE_USER=DB_USER,
- MYSQL_DATABASE_PASSWORD=DB_PASSWORD,
- MYSQL_DATABASE_DB=DB_NAME
- )
- mysql = MySQL(cursorclass=DictCursor)
- mysql.init_app(app)
- TABLE_STATUS_FREE = 0
- TABLE_STATUS_RESERVED = 1
- TABLE_STATUS_BUSY = 2
- @app.route("/tables")
- def all_tables():
- tables = []
- cursor = mysql.get_db().cursor()
- sql = "SELECT tables_id, table_status, updated_at FROM tables"
- cursor.execute(sql)
- rows = cursor.fetchall()
- for row in rows:
- # tables[index] = {'tables_id': <int>, 'table_status': <int>, 'updated_at': <datetime>}
- if not can_reserve(datetime.now(), row['tables_id']):
- row['table_status'] = TABLE_STATUS_RESERVED
- tables.append(row)
- jsonify(tables)
- def can_reserve(date, table_id):
- cursor = mysql.get_db().cursor()
- table_sql = "SELECT tables_id, table_status, updated_at FROM tables WHERE tables_id = {}"\
- .format(table_id)
- reservation_sql = "SELECT reservation_date, table_id " \
- "FROM reservations " \
- "WHERE table_id = {} ORDER BY reservation_date DESC".format(table_id)
- cursor.execute(table_sql)
- table = cursor.fetchone()
- cursor.execute(reservation_sql)
- reservation = cursor.fetchone()
- if table['table_status'] == TABLE_STATUS_FREE \
- and table['updated_at'] > reservation['reservation_date']:
- return True
- if table['table_status'] == TABLE_STATUS_BUSY and table['updated_at'] > reservation['reservation_date']:
- return date > (table['updated_at'] + timedelta(hours=5))
- return False
- def can_create(dish_id, count=1):
- pass
- app.run(host='0.0.0.0', port=8080)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement