2010-05-11 17:04:40 +02:00
|
|
|
#!/usr/bin/python
|
2010-05-16 22:42:35 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-05-11 17:04:40 +02:00
|
|
|
|
2010-05-08 22:59:48 +02:00
|
|
|
import sqlalchemy
|
2011-01-30 22:54:23 +01:00
|
|
|
from sqlalchemy.sql import func
|
|
|
|
from sqlalchemy import desc
|
2011-03-05 16:43:35 +01:00
|
|
|
import re, sys, os, traceback, signal, readline
|
2010-05-07 19:32:39 +02:00
|
|
|
from helpers import *
|
2011-03-06 21:18:06 +01:00
|
|
|
|
2010-05-07 19:32:39 +02:00
|
|
|
|
2010-05-18 21:25:01 +02:00
|
|
|
exit_commands = ['exit', 'abort', 'quit', 'bye', 'eat flaming death', 'q']
|
2010-05-09 21:49:50 +02:00
|
|
|
help_commands = ['help', '?']
|
|
|
|
context_commands = ['what', '??']
|
|
|
|
local_help_commands = ['help!', '???']
|
2010-05-16 22:42:35 +02:00
|
|
|
faq_commands = ['faq']
|
2010-05-12 19:42:48 +02:00
|
|
|
restart_commands = ['restart']
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2010-11-24 17:22:10 +01:00
|
|
|
low_credit_warning_limit = -100
|
|
|
|
|
2011-03-06 21:15:38 +01:00
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
class ExitMenu(Exception):
|
|
|
|
pass
|
|
|
|
|
2010-05-07 19:32:39 +02:00
|
|
|
class Menu():
|
2010-05-08 22:59:48 +02:00
|
|
|
def __init__(self, name, items=[], prompt='> ',
|
2010-05-09 21:49:50 +02:00
|
|
|
return_index=True,
|
2010-05-11 17:04:40 +02:00
|
|
|
exit_msg=None, exit_confirm_msg=None, exit_disallowed_msg=None,
|
2010-05-16 20:43:41 +02:00
|
|
|
help_text=None, uses_db=False):
|
2010-05-07 19:32:39 +02:00
|
|
|
self.name = name
|
2010-05-08 02:23:21 +02:00
|
|
|
self.items = items
|
|
|
|
self.prompt = prompt
|
2010-05-08 22:59:48 +02:00
|
|
|
self.return_index = return_index
|
2010-05-08 02:23:21 +02:00
|
|
|
self.exit_msg = exit_msg
|
2010-05-09 21:49:50 +02:00
|
|
|
self.exit_confirm_msg = exit_confirm_msg
|
2010-05-11 17:04:40 +02:00
|
|
|
self.exit_disallowed_msg = exit_disallowed_msg
|
2010-05-09 21:49:50 +02:00
|
|
|
self.help_text = help_text
|
|
|
|
self.context = None
|
|
|
|
self.header_format = '[%s]'
|
2010-05-16 20:43:41 +02:00
|
|
|
self.uses_db = uses_db
|
2010-05-09 21:49:50 +02:00
|
|
|
|
|
|
|
def exit_menu(self):
|
2010-05-11 17:04:40 +02:00
|
|
|
if self.exit_disallowed_msg != None:
|
|
|
|
print self.exit_disallowed_msg
|
|
|
|
return
|
2010-05-09 21:49:50 +02:00
|
|
|
if self.exit_confirm_msg != None:
|
2010-05-11 22:12:32 +02:00
|
|
|
if not self.confirm(self.exit_confirm_msg, default=True):
|
2010-05-09 21:49:50 +02:00
|
|
|
return
|
|
|
|
raise ExitMenu()
|
2010-05-08 02:23:21 +02:00
|
|
|
|
|
|
|
def at_exit(self):
|
|
|
|
if self.exit_msg:
|
|
|
|
print self.exit_msg
|
|
|
|
|
2010-05-09 21:49:50 +02:00
|
|
|
def set_context(self, string, display=True):
|
|
|
|
self.context = string
|
|
|
|
if self.context != None and display:
|
|
|
|
print self.context
|
|
|
|
|
2010-05-11 22:12:32 +02:00
|
|
|
def add_to_context(self, string):
|
|
|
|
self.context += string
|
|
|
|
|
2010-05-09 21:49:50 +02:00
|
|
|
def printc(self, string):
|
|
|
|
print string
|
|
|
|
if self.context == None:
|
|
|
|
self.context = string
|
|
|
|
else:
|
|
|
|
self.context += '\n' + string
|
|
|
|
|
|
|
|
def show_context(self):
|
|
|
|
print self.header_format % self.name
|
|
|
|
if self.context != None:
|
|
|
|
print self.context
|
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
def item_is_submenu(self, i):
|
|
|
|
return isinstance(self.items[i], Menu)
|
|
|
|
|
|
|
|
def item_name(self, i):
|
|
|
|
if self.item_is_submenu(i):
|
|
|
|
return self.items[i].name
|
2010-05-08 22:59:48 +02:00
|
|
|
elif isinstance(self.items[i], tuple):
|
|
|
|
return self.items[i][1]
|
2010-05-08 02:23:21 +02:00
|
|
|
else:
|
2010-05-11 18:38:30 +02:00
|
|
|
return self.items[i]
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2010-05-08 22:59:48 +02:00
|
|
|
def item_value(self, i):
|
|
|
|
if isinstance(self.items[i], tuple):
|
|
|
|
return self.items[i][0]
|
|
|
|
if self.return_index:
|
|
|
|
return i
|
|
|
|
return self.items[i]
|
|
|
|
|
2010-05-09 20:24:05 +02:00
|
|
|
def input_str(self, prompt=None, regex=None, length_range=(None,None),
|
|
|
|
empty_string_is_none=False):
|
2010-05-08 22:59:48 +02:00
|
|
|
if regex != None:
|
|
|
|
while True:
|
2010-05-11 22:12:32 +02:00
|
|
|
result = self.input_str(prompt, length_range=length_range,
|
|
|
|
empty_string_is_none=empty_string_is_none)
|
|
|
|
if result == None or re.match(regex+'$', result):
|
2010-05-08 22:59:48 +02:00
|
|
|
return result
|
|
|
|
else:
|
|
|
|
print 'Value must match regular expression "%s"' % regex
|
2010-05-09 20:24:05 +02:00
|
|
|
if length_range != (None,None):
|
|
|
|
while True:
|
2010-05-11 22:12:32 +02:00
|
|
|
result = self.input_str(prompt, empty_string_is_none=empty_string_is_none)
|
|
|
|
if result == None:
|
|
|
|
length = 0
|
|
|
|
else:
|
|
|
|
length = len(result)
|
2010-05-09 20:24:05 +02:00
|
|
|
if ((length_range[0] and length < length_range[0]) or
|
|
|
|
(length_range[1] and length > length_range[1])):
|
|
|
|
if length_range[0] and length_range[1]:
|
|
|
|
print 'Value must have length in range [%d,%d]' % length_range
|
2010-06-19 14:00:20 +02:00
|
|
|
elif length_range[0]:
|
2010-05-09 20:24:05 +02:00
|
|
|
print 'Value must have length at least %d' % length_range[0]
|
|
|
|
else:
|
|
|
|
print 'Value must have length at most %d' % length_range[1]
|
|
|
|
else:
|
|
|
|
return result
|
2010-05-08 02:23:21 +02:00
|
|
|
if prompt == None:
|
|
|
|
prompt = self.prompt
|
2010-05-09 21:49:50 +02:00
|
|
|
while True:
|
|
|
|
try:
|
2010-05-16 16:15:36 +02:00
|
|
|
result = unicode(raw_input(safe_str(prompt)),
|
|
|
|
conf.input_encoding)
|
2010-05-09 21:49:50 +02:00
|
|
|
except EOFError:
|
|
|
|
print 'quit'
|
|
|
|
self.exit_menu()
|
|
|
|
continue
|
|
|
|
if result in exit_commands:
|
|
|
|
self.exit_menu()
|
|
|
|
continue
|
|
|
|
if result in help_commands:
|
|
|
|
self.general_help()
|
|
|
|
continue
|
|
|
|
if result in local_help_commands:
|
|
|
|
self.local_help()
|
|
|
|
continue
|
|
|
|
if result in context_commands:
|
|
|
|
self.show_context()
|
2011-03-06 21:15:38 +01:00
|
|
|
print 'hei hello'
|
2010-05-09 21:49:50 +02:00
|
|
|
continue
|
2010-05-16 22:42:35 +02:00
|
|
|
if result in faq_commands:
|
|
|
|
FAQMenu().execute()
|
|
|
|
continue
|
2010-05-12 19:42:48 +02:00
|
|
|
if result in restart_commands:
|
|
|
|
if self.confirm('Restart Dibbler?'):
|
|
|
|
restart()
|
|
|
|
continue
|
2010-05-09 21:49:50 +02:00
|
|
|
if empty_string_is_none and result == '':
|
|
|
|
return None
|
|
|
|
return result
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2011-03-05 16:43:35 +01:00
|
|
|
def thing_in_menu_choice(self, result):
|
|
|
|
self.session = Session()
|
|
|
|
thing = self.search_for_thing(result)
|
|
|
|
if thing:
|
|
|
|
BuyMenu(initThing = thing).execute(self.session)
|
|
|
|
print ""
|
|
|
|
self.show_context()
|
|
|
|
self.session.close()
|
|
|
|
else:
|
|
|
|
print "Please enter an integer"
|
|
|
|
|
|
|
|
def input_choice(self, number_of_choices, prompt=None):
|
|
|
|
if prompt == None:
|
2011-03-06 14:53:01 +01:00
|
|
|
prompt = self.prompt
|
2011-03-05 16:43:35 +01:00
|
|
|
while True:
|
|
|
|
result = self.input_str(prompt)
|
2011-03-06 14:53:01 +01:00
|
|
|
if result == '':
|
|
|
|
print 'Please enter something'
|
|
|
|
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
choice = int(result)
|
|
|
|
if (choice > 0 and choice <= number_of_choices):
|
|
|
|
return choice
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
self.thing_in_menu_choice(result)
|
2011-03-05 16:43:35 +01:00
|
|
|
|
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
def input_int(self, prompt=None, allowed_range=(None,None)):
|
|
|
|
if prompt == None:
|
|
|
|
prompt = self.prompt
|
|
|
|
while True:
|
|
|
|
result = self.input_str(prompt)
|
|
|
|
try:
|
|
|
|
value = int(result)
|
|
|
|
if ((allowed_range[0] and value < allowed_range[0]) or
|
|
|
|
(allowed_range[1] and value > allowed_range[1])):
|
|
|
|
if allowed_range[0] and allowed_range[1]:
|
|
|
|
print 'Value must be in range [%d,%d]' % allowed_range
|
|
|
|
elif allowed_range[0]:
|
|
|
|
print 'Value must be at least %d' % allowed_range[0]
|
|
|
|
else:
|
|
|
|
print 'Value must be at most %d' % allowed_range[1]
|
|
|
|
else:
|
|
|
|
return value
|
|
|
|
except ValueError:
|
2011-03-05 16:43:35 +01:00
|
|
|
print "Please enter an integer"
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2010-05-08 20:05:28 +02:00
|
|
|
def input_user(self, prompt=None):
|
|
|
|
user = None
|
|
|
|
while user == None:
|
2010-05-13 20:11:31 +02:00
|
|
|
user = self.retrieve_user(self.input_str(prompt))
|
2010-05-08 20:05:28 +02:00
|
|
|
return user
|
|
|
|
|
2010-05-13 20:11:31 +02:00
|
|
|
def retrieve_user(self, search_str):
|
|
|
|
return self.search_ui(search_user, search_str, 'user')
|
|
|
|
|
2010-05-08 20:05:28 +02:00
|
|
|
def input_product(self, prompt=None):
|
|
|
|
product = None
|
|
|
|
while product == None:
|
2010-05-13 20:11:31 +02:00
|
|
|
product = self.retrieve_product(self.input_str(prompt))
|
2010-05-08 20:05:28 +02:00
|
|
|
return product
|
|
|
|
|
2010-05-13 20:11:31 +02:00
|
|
|
def retrieve_product(self, search_str):
|
|
|
|
return self.search_ui(search_product, search_str, 'product')
|
|
|
|
|
2010-05-08 20:05:28 +02:00
|
|
|
def input_thing(self, prompt=None, permitted_things=('user','product'),
|
2010-05-13 20:11:31 +02:00
|
|
|
add_nonexisting=(), empty_input_permitted=False):
|
2010-05-08 20:05:28 +02:00
|
|
|
result = None
|
|
|
|
while result == None:
|
|
|
|
search_str = self.input_str(prompt)
|
|
|
|
if search_str == '' and empty_input_permitted:
|
|
|
|
return None
|
2010-05-13 20:11:31 +02:00
|
|
|
result = self.search_for_thing(search_str, permitted_things, add_nonexisting)
|
2010-05-08 20:05:28 +02:00
|
|
|
return result
|
|
|
|
|
2010-05-13 20:11:31 +02:00
|
|
|
def search_for_thing(self, search_str, permitted_things=('user','product'),
|
|
|
|
add_nonexisting=()):
|
2010-05-08 20:05:28 +02:00
|
|
|
search_fun = {'user': search_user,
|
|
|
|
'product': search_product}
|
|
|
|
results = {}
|
|
|
|
result_values = {}
|
|
|
|
for thing in permitted_things:
|
|
|
|
results[thing] = search_fun[thing](search_str, self.session)
|
|
|
|
result_values[thing] = self.search_result_value(results[thing])
|
|
|
|
selected_thing = argmax(result_values)
|
2010-05-13 20:11:31 +02:00
|
|
|
if results[selected_thing] == []:
|
|
|
|
thing_for_type = {'card': 'user', 'username': 'user',
|
|
|
|
'bar_code': 'product'}
|
|
|
|
type_guess = guess_data_type(search_str)
|
|
|
|
if type_guess != None and thing_for_type[type_guess] in add_nonexisting:
|
|
|
|
return self.search_add(search_str)
|
|
|
|
print 'No match found for "%s".' % search_str
|
|
|
|
return None
|
|
|
|
return self.search_ui2(search_str, results[selected_thing], selected_thing)
|
2010-05-08 20:05:28 +02:00
|
|
|
|
|
|
|
def search_result_value(self, result):
|
|
|
|
if result == None:
|
|
|
|
return 0
|
|
|
|
if not isinstance(result, list):
|
|
|
|
return 3
|
|
|
|
if len(result) == 0:
|
|
|
|
return 0
|
|
|
|
if len(result) == 1:
|
|
|
|
return 2
|
|
|
|
return 1
|
|
|
|
|
2010-05-13 20:11:31 +02:00
|
|
|
def search_add(self, string):
|
|
|
|
type_guess = guess_data_type(string)
|
|
|
|
if type_guess == 'username':
|
|
|
|
print '"%s" looks like a username, but no such user exists.' % string
|
|
|
|
if self.confirm('Create user %s?' % string):
|
|
|
|
user = User(string, None)
|
|
|
|
self.session.add(user)
|
|
|
|
return user
|
|
|
|
return None
|
|
|
|
if type_guess == 'card':
|
|
|
|
selector = Selector('"%s" looks like a card number, but no user with that card number exists.' % string,
|
|
|
|
[('create', 'Create user with card number %s' % string),
|
|
|
|
('set', 'Set card number of an existing user to %s' % string)])
|
|
|
|
selection = selector.execute()
|
|
|
|
if selection == 'create':
|
|
|
|
username = self.input_str('Username for new user (should be same as PVV username)> ',
|
|
|
|
User.name_re, (1,10))
|
|
|
|
user = User(username, string)
|
|
|
|
self.session.add(user)
|
|
|
|
return user
|
|
|
|
if selection == 'set':
|
|
|
|
user = self.input_user('User to set card number for> ')
|
|
|
|
old_card = user.card
|
|
|
|
user.card = string
|
|
|
|
print 'Card number of %s set to %s (was %s)' % (user.name, string, old_card)
|
|
|
|
return user
|
|
|
|
return None
|
|
|
|
if type_guess == 'bar_code':
|
|
|
|
print '"%s" looks like the bar code for a product, but no such product exists.' % string
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def search_ui(self, search_fun, search_str, thing):
|
|
|
|
result = search_fun(search_str, self.session)
|
|
|
|
return self.search_ui2(search_str, result, thing)
|
|
|
|
|
|
|
|
def search_ui2(self, search_str, result, thing):
|
|
|
|
if not isinstance(result, list):
|
|
|
|
return result
|
|
|
|
if len(result) == 0:
|
|
|
|
print 'No %ss matching "%s"' % (thing, search_str)
|
|
|
|
return None
|
|
|
|
if len(result) == 1:
|
|
|
|
msg = 'One %s matching "%s": %s. Use this?' %\
|
|
|
|
(thing, search_str, unicode(result[0]))
|
|
|
|
if self.confirm(msg, default=True):
|
|
|
|
return result[0]
|
|
|
|
return None
|
|
|
|
limit = 9
|
|
|
|
if len(result) > limit:
|
|
|
|
select_header = '%d %ss matching "%s"; showing first %d' % \
|
|
|
|
(len(result), thing, search_str, limit)
|
|
|
|
select_items = result[:limit]
|
|
|
|
else:
|
|
|
|
select_header = '%d %ss matching "%s"' % \
|
|
|
|
(len(result), thing, search_str)
|
|
|
|
select_items = result
|
|
|
|
selector = Selector(select_header, items=select_items,
|
|
|
|
return_index=False)
|
|
|
|
return selector.execute()
|
|
|
|
|
|
|
|
|
|
|
|
|
2010-05-11 21:23:52 +02:00
|
|
|
def confirm(self, prompt, default=None):
|
|
|
|
return ConfirmMenu(prompt, default).execute()
|
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
def print_header(self):
|
|
|
|
print
|
2010-05-09 21:49:50 +02:00
|
|
|
print self.header_format % self.name
|
2010-05-08 02:23:21 +02:00
|
|
|
|
|
|
|
def pause(self):
|
|
|
|
self.input_str('.')
|
|
|
|
|
2010-05-09 21:49:50 +02:00
|
|
|
def general_help(self):
|
|
|
|
print '''
|
|
|
|
DIBBLER HELP
|
|
|
|
|
|
|
|
The following commands are recognized (almost) everywhere:
|
|
|
|
|
|
|
|
help, ? -- display this help
|
|
|
|
what, ?? -- redisplay the current context
|
|
|
|
help!, ??? -- display context-specific help (if any)
|
2010-05-16 22:42:35 +02:00
|
|
|
faq -- display frequently asked questions (with answers)
|
2010-05-09 21:49:50 +02:00
|
|
|
exit, quit, etc. -- exit from the current menu
|
|
|
|
|
|
|
|
When prompted for a user, you can type (parts of) the user name or
|
|
|
|
card number. When prompted for a product, you can type (parts of) the
|
|
|
|
product name or barcode.
|
2010-05-16 21:34:12 +02:00
|
|
|
|
|
|
|
About payment and "credit": When paying for something, use either
|
|
|
|
Dibbler or the good old money box -- never both at the same time.
|
|
|
|
Dibbler keeps track of a "credit" for each user, which is the amount
|
|
|
|
of money PVVVV owes the user. This value decreases with the
|
|
|
|
appropriate amount when you register a purchase, and you may increase
|
|
|
|
it by putting money in the box and using the "Adjust credit" menu.
|
2010-05-09 21:49:50 +02:00
|
|
|
'''
|
|
|
|
|
|
|
|
def local_help(self):
|
|
|
|
if self.help_text == None:
|
|
|
|
print 'no help here'
|
|
|
|
else:
|
|
|
|
print
|
2010-05-11 17:04:40 +02:00
|
|
|
print 'Help for %s:' % (self.header_format%self.name)
|
2010-05-09 21:49:50 +02:00
|
|
|
print self.help_text
|
|
|
|
|
2011-03-05 16:43:35 +01:00
|
|
|
def execute(self, session=None):
|
2010-05-11 20:12:44 +02:00
|
|
|
self.set_context(None)
|
2011-03-05 16:43:35 +01:00
|
|
|
self.session = session
|
2010-05-08 02:23:21 +02:00
|
|
|
try:
|
2011-03-05 16:43:35 +01:00
|
|
|
if self.uses_db and not session:
|
2010-05-16 20:43:41 +02:00
|
|
|
self.session = Session()
|
2010-05-08 02:23:21 +02:00
|
|
|
return self._execute()
|
|
|
|
except ExitMenu:
|
|
|
|
self.at_exit()
|
|
|
|
return None
|
2010-05-16 20:43:41 +02:00
|
|
|
finally:
|
|
|
|
if self.session != None:
|
|
|
|
self.session.close()
|
|
|
|
self.session = None
|
2010-05-08 02:23:21 +02:00
|
|
|
|
|
|
|
def _execute(self):
|
2010-06-04 23:07:47 +02:00
|
|
|
line_format = '%' + str(len(str(len(self.items)))) + 'd ) %s'
|
2010-05-08 02:23:21 +02:00
|
|
|
while True:
|
|
|
|
self.print_header()
|
2010-05-09 21:49:50 +02:00
|
|
|
self.set_context(None)
|
2010-05-08 02:23:21 +02:00
|
|
|
if len(self.items)==0:
|
2010-05-09 21:49:50 +02:00
|
|
|
self.printc('(empty menu)')
|
2010-05-08 02:23:21 +02:00
|
|
|
self.pause()
|
|
|
|
return None
|
|
|
|
for i in range(len(self.items)):
|
2010-06-04 23:07:47 +02:00
|
|
|
self.printc(line_format % (i+1, self.item_name(i)))
|
2011-03-05 16:43:35 +01:00
|
|
|
item_i = self.input_choice(len(self.items), prompt=self.prompt)-1
|
2010-05-08 02:23:21 +02:00
|
|
|
if self.item_is_submenu(item_i):
|
|
|
|
self.items[item_i].execute()
|
|
|
|
else:
|
2010-05-08 22:59:48 +02:00
|
|
|
return self.item_value(item_i)
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2010-05-11 21:23:52 +02:00
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
class Selector(Menu):
|
2010-05-08 23:54:58 +02:00
|
|
|
def __init__(self, name, items=[], prompt='select> ',
|
2010-05-09 21:49:50 +02:00
|
|
|
return_index=True,
|
|
|
|
exit_msg=None, exit_confirm_msg=None,
|
|
|
|
help_text=None):
|
2010-05-08 23:54:58 +02:00
|
|
|
Menu.__init__(self, name, items, prompt, return_index, exit_msg)
|
2010-05-09 21:49:50 +02:00
|
|
|
self.header_format = '%s'
|
2010-05-08 23:54:58 +02:00
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
def print_header(self):
|
2010-05-09 21:49:50 +02:00
|
|
|
print self.header_format % self.name
|
|
|
|
|
|
|
|
def local_help(self):
|
|
|
|
if self.help_text == None:
|
|
|
|
print 'This is a selection menu. Enter one of the listed numbers, or'
|
|
|
|
print '\'exit\' to go out and do something else.'
|
|
|
|
else:
|
|
|
|
print
|
2010-05-11 17:04:40 +02:00
|
|
|
print 'Help for selector (%s):' % self.name
|
2010-05-09 21:49:50 +02:00
|
|
|
print self.help_text
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2010-05-07 19:32:39 +02:00
|
|
|
|
2010-05-11 21:23:52 +02:00
|
|
|
class ConfirmMenu(Menu):
|
|
|
|
def __init__(self, prompt='confirm?', default=None):
|
|
|
|
Menu.__init__(self, 'question', prompt=prompt,
|
|
|
|
exit_disallowed_msg='Please answer yes or no')
|
|
|
|
self.default=default
|
|
|
|
|
|
|
|
def _execute(self):
|
2010-05-16 15:13:58 +02:00
|
|
|
options = {True: '[y]/n', False: 'y/[n]', None: 'y/n'}[self.default]
|
2010-05-11 21:23:52 +02:00
|
|
|
while True:
|
|
|
|
result = self.input_str('%s (%s) ' % (self.prompt, options))
|
2010-05-13 20:11:31 +02:00
|
|
|
result = result.lower()
|
2010-05-11 21:23:52 +02:00
|
|
|
if result in ['y','yes']:
|
|
|
|
return True
|
|
|
|
if result in ['n','no']:
|
|
|
|
return False
|
|
|
|
if self.default != None and result == '':
|
|
|
|
return self.default
|
|
|
|
print 'Please answer yes or no'
|
|
|
|
|
|
|
|
|
2010-05-16 22:42:35 +02:00
|
|
|
class MessageMenu(Menu):
|
|
|
|
def __init__(self, name, message, pause_after_message=True):
|
|
|
|
Menu.__init__(self, name)
|
|
|
|
self.message = message.strip()
|
|
|
|
self.pause_after_message = pause_after_message
|
|
|
|
|
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
|
|
|
print
|
|
|
|
print self.message
|
|
|
|
if self.pause_after_message:
|
|
|
|
self.pause()
|
|
|
|
|
|
|
|
|
|
|
|
class FAQMenu(Menu):
|
|
|
|
def __init__(self):
|
|
|
|
Menu.__init__(self, 'Frequently Asked Questions')
|
|
|
|
self.items = [MessageMenu('What is the meaning with this program?',
|
|
|
|
'''
|
|
|
|
We want to avoid keeping lots of cash in PVVVV\'s money box and to
|
|
|
|
make it easy to pay for stuff without using money. (Without using
|
|
|
|
money each time, that is. You do of course have to pay for the things
|
|
|
|
you buy eventually).
|
|
|
|
|
|
|
|
Dibbler stores a "credit" amount for each user. When you register a
|
|
|
|
purchase in Dibbler, this amount is decreased. To increase your
|
|
|
|
credit, add money to the money box and use "Adjust credit" to tell
|
|
|
|
Dibbler about it.
|
|
|
|
'''),
|
|
|
|
MessageMenu('Can I still pay for stuff using cash?',
|
|
|
|
'Yes. You can safely ignore this program completely.'),
|
|
|
|
MessageMenu('How do I exit from a submenu/dialog/thing?',
|
2011-01-30 22:01:38 +01:00
|
|
|
'Type "exit" or C-d.'),
|
2010-05-16 22:42:35 +02:00
|
|
|
MessageMenu('What does "." mean?',
|
|
|
|
'''
|
|
|
|
The "." character, known as "full stop" or "period", is most often
|
|
|
|
used to indicate the end of a sentence.
|
|
|
|
|
|
|
|
It is also used by Dibbler to indicate that the program wants you to
|
|
|
|
read some text before continuing. Whenever some output ends with a
|
|
|
|
line containing only a period, you should read the lines above and
|
|
|
|
then press enter to continue.
|
|
|
|
'''),
|
|
|
|
MessageMenu('Why is the user interface so terribly unintuitive?',
|
|
|
|
'''
|
|
|
|
Answer #1: It is not.
|
|
|
|
|
|
|
|
Answer #2: We are trying to compete with PVV\'s microwave oven in
|
|
|
|
userfriendliness.
|
|
|
|
|
|
|
|
Answer #3: YOU are unintuitive.
|
|
|
|
'''),
|
|
|
|
MessageMenu('Why is there no help command?',
|
|
|
|
'There is. Have you tried typing "help"?'),
|
|
|
|
MessageMenu('Where are the easter eggs? I tried saying "moo", but nothing happened.',
|
|
|
|
'Don\'t say "moo".'),
|
2010-05-16 22:50:37 +02:00
|
|
|
MessageMenu('Why does the program speak English when all the users are Norwegians?',
|
2010-05-16 22:42:35 +02:00
|
|
|
u'Godt spørsmål. Det virket sikkert som en god idé der og da.'),
|
2011-01-30 22:01:38 +01:00
|
|
|
MessageMenu('I found a bug; is there a reward?',
|
2010-06-04 23:00:58 +02:00
|
|
|
'''
|
2011-01-30 22:01:38 +01:00
|
|
|
No.
|
|
|
|
|
|
|
|
But if you are certain that it is a bug, not a feature, then you
|
|
|
|
should fix it (or better: force someone else to do it).
|
|
|
|
|
|
|
|
Follow this procedure:
|
|
|
|
|
|
|
|
1. Check out the Dibbler code from https://dev.pvv.ntnu.no/svn/dibbler
|
|
|
|
|
|
|
|
2. Fix the bug.
|
|
|
|
|
|
|
|
3. Check that the program still runs (and, preferably, that the bug is
|
|
|
|
in fact fixed).
|
|
|
|
|
|
|
|
4. Commit.
|
|
|
|
|
|
|
|
5. Update the running copy from svn:
|
|
|
|
|
|
|
|
$ su -
|
|
|
|
# su -l -s /bin/bash pvvvv
|
|
|
|
$ cd dibbler
|
|
|
|
$ svn up
|
|
|
|
|
|
|
|
6. Type "restart" in Dibbler to replace the running process by a new
|
|
|
|
one using the updated files.
|
2010-06-04 23:00:58 +02:00
|
|
|
'''),
|
2010-05-16 22:42:35 +02:00
|
|
|
MessageMenu('My question isn\'t listed here; what do I do?',
|
|
|
|
'''
|
|
|
|
DON\'T PANIC.
|
|
|
|
|
|
|
|
Follow this procedure:
|
|
|
|
|
|
|
|
1. Ask someone (or read the source code) and get an answer.
|
|
|
|
|
|
|
|
2. Check out the Dibbler code from https://dev.pvv.ntnu.no/svn/dibbler
|
|
|
|
|
|
|
|
3. Add your question (with answer) to the FAQ and commit.
|
|
|
|
|
2011-01-30 22:01:38 +01:00
|
|
|
4. Update the running copy from svn:
|
|
|
|
|
|
|
|
$ su -
|
|
|
|
# su -l -s /bin/bash pvvvv
|
|
|
|
$ cd dibbler
|
|
|
|
$ svn up
|
2010-05-16 22:42:35 +02:00
|
|
|
|
2011-01-30 22:01:38 +01:00
|
|
|
5. Type "restart" in Dibbler to replace the running process by a new
|
|
|
|
one using the updated files.
|
|
|
|
''')]
|
2010-05-11 21:23:52 +02:00
|
|
|
|
2010-05-07 19:32:39 +02:00
|
|
|
|
|
|
|
|
2010-05-08 20:05:28 +02:00
|
|
|
class TransferMenu(Menu):
|
|
|
|
def __init__(self):
|
2010-05-16 20:43:41 +02:00
|
|
|
Menu.__init__(self, 'Transfer credit between users',
|
|
|
|
uses_db=True)
|
2010-05-08 20:05:28 +02:00
|
|
|
|
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
2010-05-11 22:12:32 +02:00
|
|
|
amount = self.input_int('Transfer amount> ', (1,100000))
|
|
|
|
self.set_context('Transfering %d kr' % amount, display=False)
|
2010-05-08 20:05:28 +02:00
|
|
|
user1 = self.input_user('From user> ')
|
2010-05-11 22:12:32 +02:00
|
|
|
self.add_to_context(' from ' + user1.name)
|
2010-05-08 20:05:28 +02:00
|
|
|
user2 = self.input_user('To user> ')
|
2010-05-11 22:12:32 +02:00
|
|
|
self.add_to_context(' to ' + user2.name)
|
|
|
|
|
2010-05-08 20:05:28 +02:00
|
|
|
t1 = Transaction(user1, amount,
|
2010-05-08 23:18:40 +02:00
|
|
|
'transfer to '+user2.name)
|
2010-05-08 20:05:28 +02:00
|
|
|
t2 = Transaction(user2, -amount,
|
2010-05-08 23:18:40 +02:00
|
|
|
'transfer from '+user1.name)
|
2010-05-08 20:05:28 +02:00
|
|
|
t1.perform_transaction()
|
|
|
|
t2.perform_transaction()
|
|
|
|
self.session.add(t1)
|
|
|
|
self.session.add(t2)
|
2010-05-09 20:24:05 +02:00
|
|
|
try:
|
|
|
|
self.session.commit()
|
|
|
|
print 'Transfered %d kr from %s to %s' % (amount, user1, user2)
|
|
|
|
print 'User %s\'s credit is now %d kr' % (user1, user1.credit)
|
|
|
|
print 'User %s\'s credit is now %d kr' % (user2, user2.credit)
|
|
|
|
except sqlalchemy.exc.SQLAlchemyError, e:
|
|
|
|
print 'Could not perform transfer: %s' % e
|
2010-05-08 20:05:28 +02:00
|
|
|
self.pause()
|
|
|
|
|
|
|
|
|
2010-05-08 22:59:48 +02:00
|
|
|
class AddUserMenu(Menu):
|
|
|
|
def __init__(self):
|
2010-05-16 20:43:41 +02:00
|
|
|
Menu.__init__(self, 'Add user', uses_db=True)
|
2010-05-08 22:59:48 +02:00
|
|
|
|
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
2010-05-13 20:11:31 +02:00
|
|
|
username = self.input_str('Username (should be same as PVV username)> ', User.name_re, (1,10))
|
|
|
|
cardnum = self.input_str('Card number (optional)> ', User.card_re, (0,10))
|
2010-11-24 17:24:52 +01:00
|
|
|
cardnum = cardnum.lower()
|
2010-05-08 22:59:48 +02:00
|
|
|
user = User(username, cardnum)
|
|
|
|
self.session.add(user)
|
|
|
|
try:
|
|
|
|
self.session.commit()
|
|
|
|
print 'User %s stored' % username
|
|
|
|
except sqlalchemy.exc.IntegrityError, e:
|
|
|
|
print 'Could not store user %s: %s' % (username,e)
|
|
|
|
self.pause()
|
|
|
|
|
|
|
|
|
|
|
|
class EditUserMenu(Menu):
|
|
|
|
def __init__(self):
|
2010-05-16 20:43:41 +02:00
|
|
|
Menu.__init__(self, 'Edit user', uses_db=True)
|
2010-05-11 22:12:32 +02:00
|
|
|
self.help_text = '''
|
|
|
|
The only editable part of a user is its card number.
|
|
|
|
|
|
|
|
First select an existing user, then enter a new card number for that
|
|
|
|
user (write an empty line to remove the card number).
|
|
|
|
'''
|
2010-05-08 22:59:48 +02:00
|
|
|
|
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
|
|
|
user = self.input_user('User> ')
|
2010-05-09 21:49:50 +02:00
|
|
|
self.printc('Editing user %s' % user.name)
|
2010-05-11 22:12:32 +02:00
|
|
|
card_str = '"%s"' % user.card
|
|
|
|
if user.card == None:
|
|
|
|
card_str = 'empty'
|
|
|
|
user.card = self.input_str('Card number (currently %s)> ' % card_str,
|
2010-05-09 20:24:05 +02:00
|
|
|
User.card_re, (0,10),
|
|
|
|
empty_string_is_none=True)
|
2010-11-24 17:24:52 +01:00
|
|
|
user.card = user.card.lower()
|
2010-05-09 20:24:05 +02:00
|
|
|
try:
|
|
|
|
self.session.commit()
|
|
|
|
print 'User %s stored' % user.name
|
|
|
|
except sqlalchemy.exc.SQLAlchemyError, e:
|
|
|
|
print 'Could not store user %s: %s' % (user.name,e)
|
2010-05-08 22:59:48 +02:00
|
|
|
self.pause()
|
|
|
|
|
|
|
|
|
|
|
|
class AddProductMenu(Menu):
|
|
|
|
def __init__(self):
|
2010-05-16 20:43:41 +02:00
|
|
|
Menu.__init__(self, 'Add product', uses_db=True)
|
2010-05-08 22:59:48 +02:00
|
|
|
|
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
2010-05-11 17:30:13 +02:00
|
|
|
bar_code = self.input_str('Bar code> ', Product.bar_code_re, (8,13))
|
2010-09-04 16:57:32 +02:00
|
|
|
name = self.input_str('Name> ', Product.name_re, (1,Product.name_length))
|
2010-05-09 20:24:05 +02:00
|
|
|
price = self.input_int('Price> ', (1,100000))
|
2010-05-08 22:59:48 +02:00
|
|
|
product = Product(bar_code, name, price)
|
|
|
|
self.session.add(product)
|
|
|
|
try:
|
|
|
|
self.session.commit()
|
|
|
|
print 'Product %s stored' % name
|
2010-05-09 20:24:05 +02:00
|
|
|
except sqlalchemy.exc.SQLAlchemyError, e:
|
2010-05-08 22:59:48 +02:00
|
|
|
print 'Could not store product %s: %s' % (name,e)
|
|
|
|
self.pause()
|
|
|
|
|
|
|
|
|
|
|
|
class EditProductMenu(Menu):
|
|
|
|
def __init__(self):
|
2010-05-16 20:43:41 +02:00
|
|
|
Menu.__init__(self, 'Edit product', uses_db=True)
|
2010-05-08 22:59:48 +02:00
|
|
|
|
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
|
|
|
product = self.input_product('Product> ')
|
2010-05-09 21:49:50 +02:00
|
|
|
self.printc('Editing product %s' % product.name)
|
2010-05-08 22:59:48 +02:00
|
|
|
while True:
|
|
|
|
selector = Selector('Do what with %s?' % product.name,
|
|
|
|
items=[('name', 'Edit name'),
|
|
|
|
('price', 'Edit price (currently %d)' % product.price),
|
|
|
|
('store', 'Store')])
|
|
|
|
what = selector.execute()
|
|
|
|
if what == 'name':
|
2010-09-04 16:57:32 +02:00
|
|
|
product.name = self.input_str('Name> ', Product.name_re, (1,product.name_length))
|
2010-05-08 22:59:48 +02:00
|
|
|
elif what == 'price':
|
2010-05-09 20:24:05 +02:00
|
|
|
product.price = self.input_int('Price> ', (1,100000))
|
2010-05-08 22:59:48 +02:00
|
|
|
elif what == 'store':
|
2010-05-09 20:24:05 +02:00
|
|
|
try:
|
|
|
|
self.session.commit()
|
|
|
|
print 'Product %s stored' % product.name
|
|
|
|
except sqlalchemy.exc.SQLAlchemyError, e:
|
|
|
|
print 'Could not store product %s: %s' % (product.name, e)
|
2010-05-08 22:59:48 +02:00
|
|
|
self.pause()
|
|
|
|
return
|
2010-05-11 18:45:32 +02:00
|
|
|
elif what == None:
|
|
|
|
print 'Edit aborted'
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
print 'What what?'
|
2010-05-08 22:59:48 +02:00
|
|
|
|
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
class ShowUserMenu(Menu):
|
|
|
|
def __init__(self):
|
2010-05-16 20:43:41 +02:00
|
|
|
Menu.__init__(self, 'Show user', uses_db=True)
|
2010-05-08 02:23:21 +02:00
|
|
|
|
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
2010-05-08 20:05:28 +02:00
|
|
|
user = self.input_user('User name or card number> ')
|
2010-05-08 02:23:21 +02:00
|
|
|
print 'User name: %s' % user.name
|
|
|
|
print 'Card number: %s' % user.card
|
2010-05-08 23:54:58 +02:00
|
|
|
print 'Credit: %s kr' % user.credit
|
2010-08-28 16:13:59 +02:00
|
|
|
selector = Selector('What do you want to know about %s?' % user.name,
|
|
|
|
items=[('transactions', 'Everything (list of all transactions)'),
|
|
|
|
('products', 'Which products %s has bought, and how many' % user.name)])
|
|
|
|
what = selector.execute()
|
|
|
|
if what == 'transactions':
|
|
|
|
self.print_all_transactions(user)
|
|
|
|
elif what == 'products':
|
|
|
|
self.print_purchased_products(user)
|
|
|
|
else:
|
|
|
|
print 'What what?'
|
|
|
|
|
|
|
|
def print_all_transactions(self, user):
|
|
|
|
num_trans = len(user.transactions)
|
|
|
|
string = '%s\'s transactions (%d):\n' % (user.name, num_trans)
|
|
|
|
for t in user.transactions:
|
|
|
|
string += ' * %s: %s %d kr, ' % \
|
|
|
|
(t.time.strftime('%Y-%m-%d %H:%M'),
|
|
|
|
{True:'in', False:'out'}[t.amount<0],
|
|
|
|
abs(t.amount))
|
|
|
|
if t.purchase:
|
|
|
|
string += 'purchase ('
|
|
|
|
string += ', '.join(map(lambda e: e.product.name,
|
|
|
|
t.purchase.entries))
|
|
|
|
string += ')'
|
|
|
|
else:
|
|
|
|
string += t.description
|
|
|
|
string += '\n'
|
|
|
|
less(string)
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2010-08-28 15:11:48 +02:00
|
|
|
def print_transactions(self, user, limit=10):
|
2010-05-09 21:49:50 +02:00
|
|
|
num_trans = len(user.transactions)
|
|
|
|
if num_trans == 0:
|
2010-05-08 20:05:28 +02:00
|
|
|
print 'No transactions'
|
|
|
|
return
|
2010-05-09 21:49:50 +02:00
|
|
|
if num_trans <= limit:
|
|
|
|
print 'Transactions (%d):' % num_trans
|
2010-05-08 23:35:30 +02:00
|
|
|
else:
|
2010-05-09 21:49:50 +02:00
|
|
|
print 'Transactions (%d, showing only last %d):' % (num_trans,limit)
|
2010-05-08 23:35:30 +02:00
|
|
|
for t in user.transactions[-limit:]:
|
2010-05-08 23:18:40 +02:00
|
|
|
string = ' * %s: %s %d kr, ' % \
|
|
|
|
(t.time.strftime('%Y-%m-%d %H:%M'),
|
|
|
|
{True:'in', False:'out'}[t.amount<0],
|
|
|
|
abs(t.amount))
|
2010-05-08 20:05:28 +02:00
|
|
|
if t.purchase:
|
|
|
|
string += 'purchase ('
|
|
|
|
string += ', '.join(map(lambda e: e.product.name,
|
|
|
|
t.purchase.entries))
|
|
|
|
string += ')'
|
|
|
|
else:
|
|
|
|
string += t.description
|
|
|
|
print string
|
2010-08-28 15:11:48 +02:00
|
|
|
|
|
|
|
def print_purchased_products(self, user):
|
|
|
|
products = {}
|
|
|
|
for transaction in user.transactions:
|
|
|
|
if transaction.purchase:
|
|
|
|
for entry in transaction.purchase.entries:
|
|
|
|
n = entry.product.name
|
|
|
|
if n in products:
|
|
|
|
products[n]+=1
|
|
|
|
else:
|
|
|
|
products[n]=1
|
|
|
|
num_products = len(products)
|
|
|
|
if num_products == 0:
|
|
|
|
print 'No products purchased yet'
|
|
|
|
else:
|
2011-01-31 16:37:11 +01:00
|
|
|
text = ''
|
|
|
|
text += 'Products purchased:\n'
|
2010-08-28 15:11:48 +02:00
|
|
|
for product in products:
|
2011-01-31 16:37:11 +01:00
|
|
|
text += ('%-'+str(Product.name_length)+'s %3i\n') \
|
|
|
|
% (product, products[product])
|
|
|
|
less(text)
|
2010-05-08 20:05:28 +02:00
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2010-05-13 20:25:19 +02:00
|
|
|
class UserListMenu(Menu):
|
|
|
|
def __init__(self):
|
2010-05-16 20:43:41 +02:00
|
|
|
Menu.__init__(self, 'User list', uses_db=True)
|
2010-05-13 20:25:19 +02:00
|
|
|
|
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
|
|
|
user_list = self.session.query(User).all()
|
|
|
|
total_credit = self.session.query(sqlalchemy.func.sum(User.credit)).first()[0]
|
|
|
|
|
2010-11-24 17:15:12 +01:00
|
|
|
line_format = '%-12s | %6s\n'
|
|
|
|
hline = '---------------------\n'
|
|
|
|
text = ''
|
|
|
|
text += line_format % ('username', 'credit')
|
|
|
|
text += hline
|
2010-05-13 20:25:19 +02:00
|
|
|
for user in user_list:
|
2010-11-24 17:15:12 +01:00
|
|
|
text += line_format % (user.name, user.credit)
|
|
|
|
text += hline
|
|
|
|
text += line_format % ('total credit', total_credit)
|
|
|
|
less(text)
|
2010-05-13 20:25:19 +02:00
|
|
|
|
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
class BuyMenu(Menu):
|
2011-03-05 16:43:35 +01:00
|
|
|
def __init__(self, initThing=None):
|
2010-05-16 20:43:41 +02:00
|
|
|
Menu.__init__(self, 'Buy', uses_db=True)
|
2011-03-05 16:43:35 +01:00
|
|
|
self.initThing = initThing
|
2010-05-09 21:49:50 +02:00
|
|
|
self.help_text = '''
|
|
|
|
Each purchase may contain one or more products and one or more buyers.
|
|
|
|
|
|
|
|
Enter products (by name or bar code) and buyers (by name or bar code)
|
|
|
|
in any order. The information gathered so far is displayed after each
|
|
|
|
addition, and you can type 'what' at any time to redisplay it.
|
|
|
|
|
|
|
|
When finished, write an empty line to confirm the purchase.
|
|
|
|
'''
|
2010-05-08 02:23:21 +02:00
|
|
|
|
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
2010-05-08 20:05:28 +02:00
|
|
|
self.purchase = Purchase()
|
2010-05-11 21:34:15 +02:00
|
|
|
self.exit_confirm_msg=None
|
2010-05-08 02:23:21 +02:00
|
|
|
while True:
|
2010-05-08 20:05:28 +02:00
|
|
|
self.print_purchase()
|
2010-05-09 21:49:50 +02:00
|
|
|
self.printc({(False,False): 'Enter user or product identification',
|
|
|
|
(False,True): 'Enter user identification or more products',
|
|
|
|
(True,False): 'Enter product identification or more users',
|
|
|
|
(True,True): 'Enter more products or users, or an empty line to confirm'
|
|
|
|
}[(len(self.purchase.transactions) > 0,
|
|
|
|
len(self.purchase.entries) > 0)])
|
2011-03-05 16:43:35 +01:00
|
|
|
if self.initThing:
|
|
|
|
thing = self.initThing
|
|
|
|
else:
|
|
|
|
thing = self.input_thing(add_nonexisting=('user',),
|
|
|
|
empty_input_permitted=True)
|
2010-05-08 20:05:28 +02:00
|
|
|
if thing == None:
|
|
|
|
if not self.complete_input():
|
2010-05-11 21:23:52 +02:00
|
|
|
if self.confirm('Not enough information entered. Abort purchase?',
|
|
|
|
default=True):
|
2010-05-08 02:23:21 +02:00
|
|
|
return False
|
|
|
|
continue
|
|
|
|
break
|
2010-05-11 21:34:15 +02:00
|
|
|
else:
|
|
|
|
# once we get something in the
|
|
|
|
# purchase, we want to protect the
|
|
|
|
# user from accidentally killing it
|
|
|
|
self.exit_confirm_msg='Abort purchase?'
|
2010-05-08 20:05:28 +02:00
|
|
|
if isinstance(thing, User):
|
2011-03-06 17:33:43 +01:00
|
|
|
if thing.card=='11122233':
|
|
|
|
print '--------------------------------------------'
|
|
|
|
print 'You are now purchasing as the user anonym.'
|
|
|
|
print 'All you purchases must be done as this user,'
|
|
|
|
print 'and you have to put money in the anonym-jar.'
|
|
|
|
print 'Ignore the credit of this user.'
|
|
|
|
print '--------------------------------------------'
|
|
|
|
Transaction(thing, purchase=self.purchase)
|
|
|
|
else:
|
|
|
|
Transaction(thing, purchase=self.purchase)
|
2010-05-08 20:05:28 +02:00
|
|
|
elif isinstance(thing, Product):
|
|
|
|
PurchaseEntry(self.purchase, thing, 1)
|
2011-03-05 16:43:35 +01:00
|
|
|
self.initThing = None
|
2010-05-08 20:05:28 +02:00
|
|
|
|
|
|
|
self.purchase.perform_purchase()
|
|
|
|
self.session.add(self.purchase)
|
2010-05-09 20:24:05 +02:00
|
|
|
try:
|
|
|
|
self.session.commit()
|
|
|
|
except sqlalchemy.exc.SQLAlchemyError, e:
|
|
|
|
print 'Could not store purchase: %s' % e
|
|
|
|
else:
|
|
|
|
print 'Purchase stored.'
|
|
|
|
self.print_purchase()
|
|
|
|
for t in self.purchase.transactions:
|
|
|
|
print 'User %s\'s credit is now %d kr' % (t.user.name, t.user.credit)
|
2011-03-06 21:15:38 +01:00
|
|
|
if (t.user.credit < low_credit_warning_limit and t.user.card != '11122233'):
|
2010-11-24 17:22:10 +01:00
|
|
|
print ('USER %s HAS LOWER CREDIT THAN %d, AND SHOULD CONSIDER PUTTING SOME MONEY IN THE BOX.'
|
|
|
|
% (t.user.name, low_credit_warning_limit))
|
2011-03-06 21:15:38 +01:00
|
|
|
self.pause()
|
|
|
|
#skriver til log
|
|
|
|
#print Product.price
|
|
|
|
#with open("dibbler-out.txt", "a") as f:
|
|
|
|
# f.write("purchase|"+ time() +"|"+self.purchase.entries[0].product.name+"|"+t.user.name+"|+str(Product.price)+|"+'-1'+"|\n")
|
|
|
|
|
2010-05-08 20:05:28 +02:00
|
|
|
return True
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2010-05-08 20:05:28 +02:00
|
|
|
def complete_input(self):
|
|
|
|
return self.purchase.is_complete()
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2010-05-08 20:05:28 +02:00
|
|
|
def format_purchase(self):
|
|
|
|
self.purchase.set_price()
|
|
|
|
transactions = self.purchase.transactions
|
|
|
|
entries = self.purchase.entries
|
|
|
|
if len(transactions) == 0 and len(entries) == 0:
|
|
|
|
return None
|
|
|
|
string = 'Purchase:'
|
|
|
|
string += '\n buyers: '
|
|
|
|
if len(transactions) == 0:
|
|
|
|
string += '(empty)'
|
|
|
|
else:
|
2010-05-13 20:11:31 +02:00
|
|
|
string += ', '.join(map(lambda t: t.user.name,
|
|
|
|
transactions))
|
2010-05-08 20:05:28 +02:00
|
|
|
string += '\n products: '
|
|
|
|
if len(entries) == 0:
|
|
|
|
string += '(empty)'
|
|
|
|
else:
|
|
|
|
string += ', '.join(map(lambda e: '%s (%d kr)'%(e.product.name, e.product.price),
|
|
|
|
entries))
|
|
|
|
if len(transactions) > 1:
|
|
|
|
string += '\n price per person: %d kr' % self.purchase.price_per_transaction()
|
|
|
|
string += '\n total price: %d kr' % self.purchase.price
|
|
|
|
return string
|
|
|
|
|
|
|
|
def print_purchase(self):
|
|
|
|
info = self.format_purchase()
|
2010-05-08 02:23:21 +02:00
|
|
|
if info != None:
|
2010-05-09 21:49:50 +02:00
|
|
|
self.set_context(info)
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2011-03-06 12:24:58 +01:00
|
|
|
class AdjustStockMenu(Menu):
|
|
|
|
def __init__(self):
|
|
|
|
Menu.__init__(self,'Adjust stock', uses_db=True)
|
|
|
|
|
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
|
|
|
product = self.input_product('Product> ')
|
2011-03-06 13:27:46 +01:00
|
|
|
|
|
|
|
|
2011-03-06 12:24:58 +01:00
|
|
|
print 'The stock of this product is: %d ' % (product.stock)
|
2011-03-06 13:27:46 +01:00
|
|
|
print 'Write the number of products you have added to the stock'
|
2011-03-06 14:07:49 +01:00
|
|
|
print 'Alternatively, correct the stock for any mistakes'
|
2011-03-06 12:24:58 +01:00
|
|
|
add_stock = self.input_int('Added stock> ', (-1000, 1000))
|
|
|
|
print 'You added %d to the stock of %s' % (add_stock,product)
|
2011-03-06 13:27:46 +01:00
|
|
|
|
2011-03-06 12:24:58 +01:00
|
|
|
product.stock += add_stock
|
2011-03-06 13:27:46 +01:00
|
|
|
|
|
|
|
print 'The stock is now %d' % (product.stock)
|
|
|
|
|
2011-03-06 12:24:58 +01:00
|
|
|
try:
|
|
|
|
self.session.commit()
|
|
|
|
print 'Stock is now stored'
|
2011-03-06 13:27:46 +01:00
|
|
|
self.pause()
|
2011-03-06 12:24:58 +01:00
|
|
|
except sqlalchemy.exc.SQLAlchemyError, e:
|
|
|
|
print 'Could not store stock: %s' % (e)
|
|
|
|
self.pause()
|
|
|
|
return
|
2011-03-06 13:27:46 +01:00
|
|
|
print 'The stock is now %d' % (product.stock)
|
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2010-05-08 22:59:48 +02:00
|
|
|
class AdjustCreditMenu(Menu): # reimplements ChargeMenu; these should be combined to one
|
|
|
|
def __init__(self):
|
2010-05-16 20:43:41 +02:00
|
|
|
Menu.__init__(self, 'Adjust credit', uses_db=True)
|
2010-05-08 22:59:48 +02:00
|
|
|
|
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
|
|
|
user = self.input_user('User> ')
|
|
|
|
print 'User %s\'s credit is %d kr' % (user.name, user.credit)
|
2010-05-09 21:49:50 +02:00
|
|
|
self.set_context('Adjusting credit for user %s' % user.name, display=False)
|
2010-05-16 21:34:12 +02:00
|
|
|
print '(Note on sign convention: Enter a positive amount here if you have'
|
|
|
|
print 'added money to the PVVVV money box, a negative amount if you have'
|
|
|
|
print 'taken money from it)'
|
2010-05-09 20:24:05 +02:00
|
|
|
amount = self.input_int('Add amount> ', (-100000,100000))
|
2010-05-16 21:34:12 +02:00
|
|
|
print '(The "log message" will show up in the transaction history in the'
|
|
|
|
print '"Show user" menu. It is not necessary to enter a message, but it'
|
|
|
|
print 'might be useful to help you remember why you adjusted the credit)'
|
2010-05-09 20:24:05 +02:00
|
|
|
description = self.input_str('Log message> ', length_range=(0,50))
|
2010-05-08 22:59:48 +02:00
|
|
|
if description == '':
|
|
|
|
description = 'manually adjusted credit'
|
|
|
|
transaction = Transaction(user, -amount, description)
|
|
|
|
transaction.perform_transaction()
|
|
|
|
self.session.add(transaction)
|
2010-05-09 20:24:05 +02:00
|
|
|
try:
|
|
|
|
self.session.commit()
|
|
|
|
print 'User %s\'s credit is now %d kr' % (user.name, user.credit)
|
|
|
|
except sqlalchemy.exc.SQLAlchemyError, e:
|
|
|
|
print 'Could not store transaction: %s' % e
|
2010-05-08 22:59:48 +02:00
|
|
|
self.pause()
|
|
|
|
|
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
class ProductListMenu(Menu):
|
|
|
|
def __init__(self):
|
2010-05-16 20:43:41 +02:00
|
|
|
Menu.__init__(self, 'Product list', uses_db=True)
|
2010-05-08 02:23:21 +02:00
|
|
|
|
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
2010-09-04 16:30:19 +02:00
|
|
|
text = ''
|
2010-05-13 20:25:19 +02:00
|
|
|
product_list = self.session.query(Product).all()
|
2011-03-06 14:07:49 +01:00
|
|
|
total_value = 0
|
|
|
|
for p in product_list:
|
|
|
|
total_value += p.price*p.stock
|
2011-03-06 12:24:58 +01:00
|
|
|
line_format = '%-15s | %5s | %-'+str(Product.name_length)+'s | %5s \n'
|
|
|
|
text += line_format % ('bar code', 'price', 'name', 'stock')
|
2011-03-06 14:53:01 +01:00
|
|
|
text += '-------------------------------------------------------------------------------\n'
|
2010-05-08 02:23:21 +02:00
|
|
|
for p in product_list:
|
2011-03-06 12:24:58 +01:00
|
|
|
text += line_format % (p.bar_code, p.price, p.name, p.stock)
|
2011-03-06 14:53:01 +01:00
|
|
|
text += '-------------------------------------------------------------------------------\n'
|
2011-03-06 14:07:49 +01:00
|
|
|
text += line_format % ('Total value',total_value,'','', )
|
2010-09-04 16:30:19 +02:00
|
|
|
less(text)
|
|
|
|
|
|
|
|
|
|
|
|
class ProductSearchMenu(Menu):
|
|
|
|
def __init__(self):
|
|
|
|
Menu.__init__(self, 'Product search', uses_db=True)
|
|
|
|
|
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
|
|
|
self.set_context('Enter (part of) product name or bar code')
|
|
|
|
product = self.input_product()
|
2011-03-06 12:24:58 +01:00
|
|
|
print 'Result: %s, price: %d kr, bar code: %s, stock: %d' % (product.name, product.price, product.bar_code, product.stock)
|
2010-05-08 02:23:21 +02:00
|
|
|
self.pause()
|
|
|
|
|
|
|
|
|
2011-01-30 22:54:23 +01:00
|
|
|
class ProductPopularityMenu(Menu):
|
|
|
|
def __init__(self):
|
|
|
|
Menu.__init__(self, 'Products by popularity', uses_db=True)
|
|
|
|
|
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
|
|
|
text = ''
|
|
|
|
sub = \
|
|
|
|
self.session.query(PurchaseEntry.product_bar_code,
|
|
|
|
func.count('*').label('purchase_count'))\
|
|
|
|
.group_by(PurchaseEntry.product_bar_code)\
|
|
|
|
.subquery()
|
|
|
|
product_list = \
|
|
|
|
self.session.query(Product, sub.c.purchase_count)\
|
|
|
|
.outerjoin((sub, Product.bar_code==sub.c.product_bar_code))\
|
|
|
|
.order_by(desc(sub.c.purchase_count))\
|
|
|
|
.filter(sub.c.purchase_count != None)\
|
|
|
|
.all()
|
|
|
|
line_format = '%10s | %-'+str(Product.name_length)+'s\n'
|
|
|
|
text += line_format % ('items sold', 'product')
|
|
|
|
text += '-'*58 + '\n'
|
|
|
|
for product, number in product_list:
|
|
|
|
text += line_format % (number, product.name)
|
|
|
|
less(text)
|
|
|
|
|
|
|
|
class ProductRevenueMenu(Menu):
|
|
|
|
def __init__(self):
|
|
|
|
Menu.__init__(self, 'Products by revenue', uses_db=True)
|
|
|
|
|
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
|
|
|
text = ''
|
|
|
|
sub = \
|
|
|
|
self.session.query(PurchaseEntry.product_bar_code,
|
|
|
|
func.count('*').label('purchase_count'))\
|
|
|
|
.group_by(PurchaseEntry.product_bar_code)\
|
|
|
|
.subquery()
|
|
|
|
product_list = \
|
|
|
|
self.session.query(Product, sub.c.purchase_count)\
|
|
|
|
.outerjoin((sub, Product.bar_code==sub.c.product_bar_code))\
|
|
|
|
.order_by(desc(sub.c.purchase_count*Product.price))\
|
|
|
|
.filter(sub.c.purchase_count != None)\
|
|
|
|
.all()
|
|
|
|
line_format = '%7s | %10s | %5s | %-'+str(Product.name_length)+'s\n'
|
|
|
|
text += line_format % ('revenue', 'items sold', 'price', 'product')
|
|
|
|
text += '-'*76 + '\n'
|
|
|
|
for product, number in product_list:
|
|
|
|
text += line_format % (number*product.price, number, product.price, product.name)
|
|
|
|
less(text)
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2011-03-06 17:33:43 +01:00
|
|
|
class BalanceMenu(Menu):
|
|
|
|
def __init__(self):
|
|
|
|
Menu.__init__(self, 'Total balance of PVVVV', uses_db=True)
|
2010-05-08 02:23:21 +02:00
|
|
|
|
2011-03-06 17:33:43 +01:00
|
|
|
def _execute(self):
|
|
|
|
self.print_header()
|
|
|
|
text = ''
|
|
|
|
total_value = 0;
|
|
|
|
product_list = self.session.query(Product).all()
|
|
|
|
for p in product_list:
|
|
|
|
total_value += p.stock*p.price
|
|
|
|
|
|
|
|
total_credit = self.session.query(sqlalchemy.func.sum(User.credit)).first()[0]
|
|
|
|
total_balance = total_value - total_credit
|
|
|
|
|
|
|
|
line_format = '%15s | %5d \n'
|
|
|
|
text += line_format % ('Total value', total_value)
|
|
|
|
text += '-------------------------\n'
|
|
|
|
text += line_format % ('Total credit', total_credit)
|
|
|
|
text += '-------------------------\n'
|
|
|
|
text += line_format % ('Total balance', total_balance)
|
|
|
|
less(text)
|
2010-05-12 19:42:48 +02:00
|
|
|
def restart():
|
|
|
|
# Does not work if the script is not executable, or if it was
|
|
|
|
# started by searching $PATH.
|
|
|
|
os.execv(sys.argv[0], sys.argv)
|
|
|
|
|
|
|
|
|
2010-05-12 18:49:37 +02:00
|
|
|
if not conf.stop_allowed:
|
|
|
|
signal.signal(signal.SIGTSTP, signal.SIG_IGN)
|
2011-03-06 21:15:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2010-05-08 02:23:21 +02:00
|
|
|
main = Menu('Dibbler main menu',
|
2011-01-30 22:54:23 +01:00
|
|
|
items=[BuyMenu(),
|
|
|
|
ProductListMenu(),
|
|
|
|
ShowUserMenu(),
|
|
|
|
UserListMenu(),
|
|
|
|
AdjustCreditMenu(),
|
|
|
|
TransferMenu(),
|
2010-05-08 22:59:48 +02:00
|
|
|
Menu('Add/edit',
|
2011-01-30 22:54:23 +01:00
|
|
|
items=[AddUserMenu(),
|
|
|
|
EditUserMenu(),
|
|
|
|
AddProductMenu(),
|
2011-03-06 12:24:58 +01:00
|
|
|
EditProductMenu(),
|
|
|
|
AdjustStockMenu(),]),
|
2010-09-04 16:30:19 +02:00
|
|
|
ProductSearchMenu(),
|
2011-01-30 22:54:23 +01:00
|
|
|
Menu('Statistics',
|
|
|
|
items=[ProductPopularityMenu(),
|
2011-03-06 17:33:43 +01:00
|
|
|
ProductRevenueMenu(),
|
|
|
|
BalanceMenu()]),
|
2010-05-16 22:42:35 +02:00
|
|
|
FAQMenu()
|
2010-05-08 22:59:48 +02:00
|
|
|
],
|
2010-05-09 21:49:50 +02:00
|
|
|
exit_msg='happy happy joy joy',
|
2010-05-11 21:23:52 +02:00
|
|
|
exit_confirm_msg='Really quit Dibbler?')
|
2010-05-11 17:04:40 +02:00
|
|
|
if not conf.quit_allowed:
|
|
|
|
main.exit_disallowed_msg = 'You can check out any time you like, but you can never leave.'
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
main.execute()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print
|
|
|
|
print 'Interrupted.'
|
|
|
|
except:
|
|
|
|
print 'Something went wrong.'
|
2010-05-11 18:17:48 +02:00
|
|
|
print '%s: %s' % sys.exc_info()[0:2]
|
2010-05-11 18:38:30 +02:00
|
|
|
if conf.show_tracebacks:
|
|
|
|
traceback.print_tb(sys.exc_info()[2])
|
2010-05-11 17:04:40 +02:00
|
|
|
else:
|
|
|
|
break
|
|
|
|
print 'Restarting main menu.'
|