def list_dishes_by_vendor(vendor_data): print_header() vid, vname, _ = vendor_data resp = call_api('list-dishes-by-vendor', params={'vid': vid}) dishes = json.loads(resp.text) if not dishes: print('\nNo dishes found.') input('Press <Enter> to return to main menu: ') return else: print('\nDishes offered by "%s":\n' % vname) header = '%5s %-25s%8s' % ('#', 'Item', 'Price') print('-' * len(header)) print(header) print('-' * len(header)) for i, (_, item, _, price) in enumerate(dishes, 1): print('%5d. %-25s%8.2f' % (i, item, price)) print('-' * len(header)) while True: print('\nSelect a dish to add it to the cart.') print('Or just press <Enter> to return to previous menu.') n = read_choice(len(dishes), 'Dish to select: ') if not n: return print('\nEnter the repeat-count (max %d) for "%s".' % (max_qty, dishes[n - 1][1])) print('Or just press <Enter> to order one portion of it.') qty = read_choice(max_qty, 'Number of portions to order: ', default=1) cart.append(dishes[n - 1] + [qty])
def view_all_orders(): resp = call_api('list-order-by-vid', params={'vid': admindata['vid']}) if resp.status_code == HTTP_OK: all_orders = json.loads(resp.text) if len(all_orders) == 0: print('\nNo orders have been placed against your restaurant.') else: orders = [] prev_oid = None i = 0 for oid, ts, name, dish, qty in all_orders: if oid != prev_oid and orders: # All entries in `orders` should have the same timestamp # and customer: they belong to the same order. dt = datetime.fromtimestamp(orders[0][0]) customer = orders[0][1] i += 1 print('\n%5d. Order placed on %s at %s by %s' % (i, dt.strftime('%F'), dt.strftime('%T'), customer)) list_orders(orders) orders = [] orders.append((ts, name, dish, qty)) prev_oid = oid else: print('Failed to fetch orders') input('\nPress <Enter> to return to main menu: ')
def signup(): '''Perform the signup process for a new user.''' print_header() print('\nSign up for a new account.') # Get the username. while True: print('\nEnter the username for your account.') print('Or just press <Enter> to return to the main menu.') uname = input('username: '******'Sorry, that username is already taken. Try again.') # Get the password. while True: print('\nEnter the password.') print('Or just press <Enter> to return to the main menu.') pword = getpass('password: '******'confirm password: '******'Password do not match; please try again.') # Get user's full name. print('\nEnter your full name.') print('Or just press <Enter> to return to the main menu.') fname = input('fullname: ') if not fname: return # Get user's phone number. print('\nEnter your phone number.') print('Or just press <Enter> to return to the main menu.') phone = input('phone number: ') if not phone: return # Perform the user sign-up on the server. resp = call_api('add-user', params={ 'username': uname, 'password': pword, 'fullname': fname, 'phone': phone }) if resp.status_code == HTTP_OK: print('Signup successful!') else: print('Signup failed.') input('\nPress <Enter> to return to main menu: ')
def place_order(): ts = int(time.time()) # NOTE: We ignore fractions of a second for now. resp = call_api('add-order', params={ 'uid': userdata['uid'], 'timestamp': ts }) if resp.status_code == HTTP_OK: oid = resp for did, _, _, _, qty in cart: call_api('add-order-dish', params={ 'oid': oid, 'did': did, 'quantity': qty }) print('Order placed successfully!') else: print('Failed to place order.') input('Press <Enter> to return to main menu: ')
def login(): '''Perform the login process for an admin. Return the "aid" returned by the server on success, else return None.''' print_header() print('\nEnter your credentials to login.') uname = input('username: '******'*** Error: Username cannot be blank/empty.') else: pword = getpass('password: '******'login-admin', params={ 'username': uname, 'password': pword }) if resp.status_code == HTTP_OK: admindata['aid'], admindata['vid'] = json.loads(resp.text) admindata['uname'] = uname print('Welcome, %s!' % admindata['uname']) else: print('Login failed.') input('\nPress <Enter> to return to main menu: ')
def login(): '''Perform the login process for a user. Return the "uid" returned by the server on success, else return None.''' print_header() print('\nEnter your credentials to login.') uname = input('username: '******'*** Error: Username cannot be blank/empty.') else: pword = getpass('password: '******'login-user', params={ 'username': uname, 'password': pword }) if resp.status_code == HTTP_OK: userdata['uid'] = resp data = get_user_data(userdata['uid']) userdata['uname'], userdata['fname'], userdata['phone'] = data print('Welcome, %s!' % userdata['fname']) else: print('Login failed.') input('\nPress <Enter> to return to main menu: ')
def view_orders(): print_header() resp = call_api('list-order-by-uid', params={'uid': userdata['uid']}) if resp.status_code != HTTP_OK: print('\nFailed to fetch your orders.') else: ordlist = json.loads(resp.text) if len(ordlist) == 0: print('\nCould not find any orders placed by you.') else: print('\nOrders placed by you:') orders = defaultdict(list) # Segregate the orders by (order-id, timestamp) pairs. # NOTE: We include None's in the tuples below, as placeholders for # dish-IDs, so that we can reuse list_dishes(). for oid, ts, item, vendor, price, qty in ordlist: orders[(oid, ts)].append((None, item, vendor, price, qty)) for i, ((oid, ts), dishes) in enumerate(orders.items(), 1): dt = datetime.fromtimestamp(ts) print('\n%5d. Order placed on %s at %s' % (i, dt.strftime('%F'), dt.strftime('%T'))) list_dishes(dishes) input('\nPress <Enter> to return to main menu: ')
def list_dishes_by_name(name): '''Return a list of dishes whose names have "name" in them.''' resp = call_api('list-dishes-by-name', params={'name': name}) return json.loads(resp.text)
def get_user_data(uid): '''Get data for a user with a given "uid".''' resp = call_api('user-data', params={'uid': uid}) return json.loads(resp.text)
def user_exists(uname): '''Return True if a user with username `uname' exists in our system; False otherwise.''' resp = call_api('user-exists', params={'username': uname}) return json.loads(resp.text)