From 5cfc90ed09215a84d483a616fab03f483053116f Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Wed, 4 Apr 2012 22:30:20 +0200 Subject: add command mode --- stocks.py | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 77 insertions(+), 13 deletions(-) diff --git a/stocks.py b/stocks.py index 2b7475c..5c7bb52 100755 --- a/stocks.py +++ b/stocks.py @@ -1,4 +1,5 @@ #!/usr/bin/python +# coding=utf-8 """Handle stocks on the web.""" import cgi import cgitb @@ -127,12 +128,36 @@ class AppCGI: pass def run (self): - stocks = Stocks () - stocks.load (DEFAULT_DATABASE) - stocks.dump () - t = Template (AppCGI.template) + self.error = None + dirty = False + # Get form content. + form = cgi.FieldStorage () + self.mode = form.getfirst ('mode', None) + self.place = form.getfirst ('place', None) + code = form.getfirst ('code', None) + try: + qty = int (form.getfirst ('qty', 0)) + except ValueError: + self.error = "bad quantity" + qty = None + # Read database. + self.stocks = Stocks () + self.stocks.load (DEFAULT_DATABASE) + # Make operations. + if not self.error: + if self.mode == 'command' and self.place and code and qty: + i = self.stocks.items[code] + old_qty = i.qty.get (self.place, 0) + i.qty[self.place] = old_qty + qty + dirty = True + if dirty: + self.stocks.dump () + # Done. + self.output () + + def output (self): + t = Template (AppCGI.template, searchList = [ self ]) t.stylesheet = AppCGI.stylesheet - t.stocks = stocks print t AppCGI.stylesheet = """\ @@ -145,18 +170,26 @@ th { } td { background: #e0e0e0; - border-top: 1px solid white; - border-left: 1px solid white; + border: 1px solid white; } -tr:hover td { +tr.place td { background: #e8e8e8; } -td.qty { +tr:hover td, tr.place:hover td { + background: #f0f0f0; +} +td.qty, td.action { text-align: right; } +form { + margin: 0; +} +input.qty { + width: 5em; +} """ -AppCGI.template = """\ +AppCGI.template = u"""\ Content-Type: text/html; charset=UTF-8 @@ -179,6 +212,28 @@ $stylesheet #end if #end def +#def command_mode($i) + #if $mode == 'command' +
+ + #if $place in $i.qty then $i.qty[$place] else 0 # + ± + +
+ #end if +#end def + +#if not $mode +

+ Command mode + + + +

+#else +

+#end if + #filter WebSafe @@ -187,6 +242,9 @@ $stylesheet + #if $mode + + #end if #for $i in $stocks.itersorted @@ -196,12 +254,18 @@ $stylesheet #end filter + #filter None + $command_mode($i) + #end filter - #for $place, $qty in $i.itersortedqty - + #for $iplace, $qty in $i.itersortedqty + - + + #if $mode + + #end if #end for #end for -- cgit v1.2.3
Command Description Qty$place
$i.desc $i.qty.main
$place$iplace $qty