summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rwxr-xr-xstocks.py63
1 files changed, 42 insertions, 21 deletions
diff --git a/stocks.py b/stocks.py
index 5fd68c1..b4a2192 100755
--- a/stocks.py
+++ b/stocks.py
@@ -143,22 +143,21 @@ class AppCGI:
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
+ self.action = form.getfirst ('action', 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
+ try:
+ if self.mode == 'command' and self.action == 'update':
+ for code, qty in self.iter_update (form):
+ if qty:
+ item = self.stocks.items[code]
+ old_qty = item.qty.get (self.place, 0)
+ item.qty[self.place] = old_qty + qty
+ dirty = True
+ except ValueError:
+ self.error = "bad input value"
if dirty:
self.stocks.dump ()
# Done.
@@ -169,6 +168,17 @@ class AppCGI:
t.stylesheet = AppCGI.stylesheet
print t
+ def iter_update (self, form):
+ """Iter over (code_x, qty_x) pairs from a form."""
+ line = 0
+ while True:
+ code = form.getfirst ('code_%d' % line, None)
+ if code is None:
+ break
+ qty = int (form.getfirst ('qty_%d' % line, 0))
+ yield code, qty
+ line += 1
+
AppCGI.stylesheet = """\
table {
border-collapse: collapse;
@@ -196,6 +206,11 @@ form {
input.qty {
width: 5em;
}
+p.error {
+ background: #ff4444;
+ border: 1px dashed black;
+ padding: 0.5ex;
+}
"""
AppCGI.template = u"""\
@@ -221,17 +236,20 @@ $stylesheet
#end if
#end def
-#def command_mode($i)
+#def command_mode($line, $i)
#if $mode == 'command'
- <td class="action"><form method="POST">
- <input type="hidden" name="code" value="$i.code" />
+ <td class="action">
+ <input type="hidden" name="code_$line" value="$i.code" />
#if $place in $i.qty then $i.qty[$place] else 0 #
- ±<input type="text" name="qty" class="qty" />
- <input type="submit" value="OK" />
- </form></td>
+ ±<input type="text" name="qty_$line" class="qty" />
+ </td>
#end if
#end def
+#if $error
+<p class="error">$error</p>
+#end if
+
#if not $mode
<form><p>
Command mode
@@ -245,6 +263,8 @@ $stylesheet
#filter WebSafe
+<form method="POST">
+<input type="hidden" name="action" value="update" />
<table>
<tr>
<th>Code</th>
@@ -252,10 +272,10 @@ $stylesheet
<th>Description</th>
<th colspan="2">Qty</th>
#if $mode
- <th>$place</th>
+ <th>$place <input type="submit" value="Update" /></th>
#end if
</tr>
- #for $i in $stocks.itersorted
+ #for $line, $i in enumerate($stocks.itersorted)
<tr>
<td>$i.code</td>
#filter None
@@ -264,7 +284,7 @@ $stylesheet
<td>$i.desc</td>
<td colspan="2" class="qty">$i.qty.main</td>
#filter None
- $command_mode($i)
+ $command_mode($line, $i)
#end filter
</tr>
#for $iplace, $qty in $i.qty.itersorted
@@ -279,6 +299,7 @@ $stylesheet
#end for
#end for
</table>
+</form>
#end filter