summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Schodet2012-04-05 22:26:07 +0200
committerNicolas Schodet2012-04-05 22:26:07 +0200
commit0739c8a0c0dd026fd57be2fc44bc63f2e3da86fa (patch)
treec5db22fcf705ab7b5dff04da1a2cb490f817a6f0
parent0802e4631b6ad221f12450824f1dd80113ab8006 (diff)
add merge action
-rwxr-xr-xstocks.py47
1 files changed, 46 insertions, 1 deletions
diff --git a/stocks.py b/stocks.py
index b4a2192..67d841f 100755
--- a/stocks.py
+++ b/stocks.py
@@ -156,10 +156,31 @@ class AppCGI:
old_qty = item.qty.get (self.place, 0)
item.qty[self.place] = old_qty + qty
dirty = True
+ elif self.action == 'merge':
+ from_ = form.getfirst ('from', None)
+ to = form.getfirst ('to', None)
+ factor = int (form.getfirst ('factor', None))
+ if not (from_ and to and factor):
+ raise StocksError ("bad parameters")
+ for item in self.stocks.items.itervalues ():
+ if from_ in item.qty:
+ old_qty = item.qty.get (to, 0)
+ item.qty[to] = old_qty + item.qty[from_] * factor
+ if from_ != to:
+ del item.qty[from_]
+ dirty = True
+ except StocksError, e:
+ self.error = str (e)
except ValueError:
self.error = "bad input value"
if dirty:
self.stocks.dump ()
+ # Make a list of places.
+ places = set ()
+ for item in self.stocks.items.itervalues ():
+ places.update (item.qty)
+ places -= set (('main', ))
+ self.places = sorted (places)
# Done.
self.output ()
@@ -203,9 +224,12 @@ td.qty, td.action {
form {
margin: 0;
}
-input.qty {
+input.qty, input.number {
width: 5em;
}
+p {
+ margin: 0.5ex 0;
+}
p.error {
background: #ff4444;
border: 1px dashed black;
@@ -246,17 +270,38 @@ $stylesheet
#end if
#end def
+#def select_places($name, $add = None)
+ #set $places = $places
+ #if $add
+ #set $places = sorted ($places + $add)
+ #end if
+ <select name="$name">
+ <option value="" selected="selected">---</option>
+ #for p in $places
+ <option>$p</option>
+ #end for
+ </select>
+#end def
+
#if $error
<p class="error">$error</p>
#end if
#if not $mode
+<form><p><input type="submit" value="Refresh" /></p></form>
<form><p>
Command mode
<input type="hidden" name="mode" value="command" />
<input type="text" name="place" />
<input type="submit" value="OK" />
</p></form>
+<form method="POST"><p>
+ <input type="hidden" name="action" value="merge" />
+ Merge $select_places("from")
+ x <input class="number" type="text" name="factor" value="1" />
+ to $select_places("to", ['main'])
+ <input type="submit" value="OK" />
+</p></form>
#else
<form><p><input type="submit" value="Leave $mode mode" /></p></form>
#end if