Advertisement
Guest User

Untitled

a guest
Apr 7th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. from flask import Flask
  2. from flask import jsonify
  3. from datetime import datetime, timedelta
  4. from MySQLdb.cursors import DictCursor
  5. from flaskext.mysql import MySQL
  6. import os
  7.  
  8. app = Flask('restaurant')
  9.  
  10. DB_USER = os.getenv('MYSQL_USER', 'user')
  11. DB_PASSWORD = os.getenv('MYSQL_PASSWORD', '55F3WzF7yudAd8L#')
  12. DB_NAME = os.getenv('MYSQL_DATABASE', 'restaurant')
  13. DB_HOST = '172.19.0.2'
  14.  
  15. app.config.update(
  16.     MYSQL_DATABASE_HOST=DB_HOST,
  17.     MYSQL_DATABASE_USER=DB_USER,
  18.     MYSQL_DATABASE_PASSWORD=DB_PASSWORD,
  19.     MYSQL_DATABASE_DB=DB_NAME
  20. )
  21.  
  22. mysql = MySQL(cursorclass=DictCursor)
  23. mysql.init_app(app)
  24.  
  25. TABLE_STATUS_FREE = 0
  26. TABLE_STATUS_RESERVED = 1
  27. TABLE_STATUS_BUSY = 2
  28.  
  29.  
  30. @app.route("/tables")
  31. def all_tables():
  32.     tables = []
  33.     cursor = mysql.get_db().cursor()
  34.  
  35.     sql = "SELECT tables_id, table_status, updated_at FROM tables"
  36.  
  37.     cursor.execute(sql)
  38.     rows = cursor.fetchall()
  39.  
  40.     for row in rows:
  41.         # tables[index] = {'tables_id': <int>, 'table_status': <int>, 'updated_at': <datetime>}
  42.         if not can_reserve(datetime.now(), row['tables_id']):
  43.             row['table_status'] = TABLE_STATUS_RESERVED
  44.         tables.append(row)
  45.     jsonify(tables)
  46.  
  47.  
  48. def can_reserve(date, table_id):
  49.     cursor = mysql.get_db().cursor()
  50.  
  51.     table_sql = "SELECT tables_id, table_status, updated_at FROM tables WHERE tables_id = {}"\
  52.         .format(table_id)
  53.     reservation_sql = "SELECT reservation_date, table_id " \
  54.                       "FROM reservations " \
  55.                       "WHERE table_id = {} ORDER BY reservation_date DESC".format(table_id)
  56.  
  57.     cursor.execute(table_sql)
  58.     table = cursor.fetchone()
  59.  
  60.     cursor.execute(reservation_sql)
  61.     reservation = cursor.fetchone()
  62.     if table['table_status'] == TABLE_STATUS_FREE \
  63.             and table['updated_at'] > reservation['reservation_date']:
  64.         return True
  65.  
  66.     if table['table_status'] == TABLE_STATUS_BUSY and table['updated_at'] > reservation['reservation_date']:
  67.         return date > (table['updated_at'] + timedelta(hours=5))
  68.  
  69.     return False
  70.  
  71.  
  72. def can_create(dish_id, count=1):
  73.     pass
  74.  
  75. app.run(host='0.0.0.0', port=8080)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement