Skip to content

Commit

Permalink
Add methods interfacing with register
Browse files Browse the repository at this point in the history
  • Loading branch information
vdumoulin committed Dec 28, 2014
1 parent a7f7a9d commit 105304f
Showing 1 changed file with 138 additions and 13 deletions.
151 changes: 138 additions & 13 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
__email__ = "vincent.dumoulin@umontreal.ca"

import argparse
from Tkinter import Tk, N, S, E, W
from Tkinter import Tk, N, S, E, W, StringVar
from ttk import Frame, Button, Entry, Label
from pyplanck.register import Register
from pyplanck.exceptions import CredentialException, ItemNotFoundException


class GUI(Frame):
Expand All @@ -21,8 +22,126 @@ def __init__(self, parent, register):
Frame.__init__(self, parent, padding=(3, 3, 3, 3))
self.parent = parent
self.register = register
self.logger = register.get_events_logger()
self.init_ui()

def login(self, token):
try:
self.register.login_employee(token)
self.prompt = self.register.get_employee_name() + " > "
except CredentialException:
self.logger.warning("invalid employee token '" + token + "', " +
"unable to login")

def logout(self):
self.register.logout_employee()
self.prompt = self.default_prompt

def print_count(self):
try:
print self.register.get_register_count()
except CredentialException:
self.logger.warning("insufficient privileges to print register " +
"count")

def add(self, token):
try:
self.register.add(token)
except CredentialException:
self.logger.warning("insufficient privileges to add an item")
except ValueError:
pass

def add_custom(self, name, price):
try:
self.register.add_custom(name, price)
except CredentialException:
self.logger.warning("insufficient privileges to add a custom item")
except ValueError as e:
self.logger.warning(e.__str__)

def remove(self, token):
try:
self.register.remove(token)
except CredentialException:
self.logger.warning("insufficient privileges to scan an item")
except ValueError:
self.logger.warning("token does not correspond to any item")
except ItemNotFoundException:
self.logger.warning("item not in order, unable to remove it")

def adjust(self, token):
try:
amount = float(token)
self.register.adjust(amount)
except CredentialException:
self.logger.warning("insufficient privileges to adjust register " +
"count")
except ValueError as e:
self.logger.warning("invalid adjustment amount: " + e)

def checkout(self):
try:
self.register.checkout_order()
except CredentialException:
self.logger.warning("insufficient privileges to checkout order")

def count(self, count_string):
try:
count = float(count_string)
self.register.count_register(count)
except CredentialException:
self.logger.warning("insufficient privileges to count register")
except ValueError as e:
self.logger.warning("invalid count: " + e)

def parse_barcode_field(self, event):
command = self.barcode_field.get().strip()
self.barcode_var.set("")
tokens = command.split(" ")
if tokens[0] == "q":
self.quit()
elif tokens[0] == "login":
if len(tokens) < 2:
self.logger.warning("need a login token")
return None
self.login(tokens[1])
elif tokens[0] == "logout":
self.logout()
elif tokens[0] == "print_count":
self.print_count()
elif tokens[0] == "print_order":
self.print_order()
elif tokens[0] == "remove":
if len(tokens) < 2:
self.logger.warning("need an item to remove")
return None
self.remove(tokens[1])
elif tokens[0] == "adjust_count":
if len(tokens) < 2:
self.logger.warning("need an adjustment amount")
return None
self.adjust(tokens[1])
elif tokens[0] == "custom":
if len(tokens) < 3:
self.logger.warning("need an name and a price")
return None
try:
self.add_custom(tokens[1], float(tokens[2]))
except ValueError:
self.logger.warning("price is not valid")
elif tokens[0] == "checkout":
self.checkout()
elif tokens[0] == "count":
if len(tokens) < 2:
self.logger.warning("need an register count")
return None
else:
self.count(tokens[1])
else:
if tokens[0] != "":
self.add(tokens[0])

def init_ui(self):
# Full screen
screen_width = self.parent.winfo_screenwidth()
Expand All @@ -41,23 +160,29 @@ def init_ui(self):
self.columnconfigure(1, weight=0)
self.columnconfigure(2, weight=0)

items_list = Frame(self, relief="sunken")
items_list.grid(row=1, column=0, rowspan=3, sticky=(N, S, E, W))
self.items_list = Frame(self, relief="sunken")
self.items_list.grid(row=1, column=0, rowspan=3, sticky=(N, S, E, W))

self.barcode_var = StringVar(self)
self.barcode_field = Entry(self, textvariable=self.barcode_var)
self.barcode_field.bind("<KeyPress-Return>", self.parse_barcode_field)
self.barcode_field.grid(row=0, column=0, sticky=(N, E, W))

barcode_field = Entry(self)
barcode_field.grid(row=0, column=0, sticky=(N, E, W))
self.name_var = StringVar(self, value="Name")
self.name_label = Label(self, textvar=self.name_var)
self.name_label.grid(row=0, column=1, columnspan=2, sticky=(N, E, W))

name_label = Label(self, text="Name")
name_label.grid(row=0, column=1, columnspan=2, sticky=(N, E, W))
self.some_button = Button(self, text="Something")
self.some_button.grid(row=1, column=1, columnspan=2, sticky=(E, W))

some_button = Button(self, text="Something")
some_button.grid(row=1, column=1, columnspan=2, sticky=(E, W))
self.ok_button = Button(self, text="Ok")
self.ok_button.grid(row=2, column=1, sticky=(S, E, W))

ok_button = Button(self, text="Ok")
ok_button.grid(row=2, column=1, sticky=(S, E, W))
self.cancel_button = Button(self, text="Cancel")
self.cancel_button.grid(row=2, column=2, sticky=(S, E, W))

cancel_button = Button(self, text="Cancel")
cancel_button.grid(row=2, column=2, sticky=(S, E, W))
# Put focus in barcode field
self.barcode_field.focus_set()


if __name__ == "__main__":
Expand Down

0 comments on commit 105304f

Please sign in to comment.