summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Schodet2012-04-04 22:30:20 +0200
committerNicolas Schodet2012-04-05 13:45:01 +0200
commit5cfc90ed09215a84d483a616fab03f483053116f (patch)
tree94bf54674a06b53ad4c4517a265a78c6c2b5b7f8
parent985d70c3ff00640da8884dff30872abb7590b4da (diff)
add command mode
-rwxr-xr-xstocks.py90
1 files 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
<html>
@@ -179,6 +212,28 @@ $stylesheet
#end if
#end def
+#def command_mode($i)
+ #if $mode == 'command'
+ <td class="action"><form method="POST">
+ <input type="hidden" name="code" 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>
+ #end if
+#end def
+
+#if not $mode
+<form><p>
+ Command mode
+ <input type="hidden" name="mode" value="command" />
+ <input type="text" name="place" />
+ <input type="submit" value="OK" />
+</p></form>
+#else
+<form><p><input type="submit" value="Leave $mode mode" /></p></form>
+#end if
+
#filter WebSafe
<table>
@@ -187,6 +242,9 @@ $stylesheet
<th>Command</th>
<th>Description</th>
<th colspan="2">Qty</th>
+ #if $mode
+ <th>$place</th>
+ #end if
</tr>
#for $i in $stocks.itersorted
<tr>
@@ -196,12 +254,18 @@ $stylesheet
#end filter
<td>$i.desc</td>
<td colspan="2" class="qty">$i.qty.main</td>
+ #filter None
+ $command_mode($i)
+ #end filter
</tr>
- #for $place, $qty in $i.itersortedqty
- <tr>
+ #for $iplace, $qty in $i.itersortedqty
+ <tr class="place">
<td colspan="3"></td>
- <td>$place</td>
+ <td>$iplace</td>
<td class="qty">$qty</td>
+ #if $mode
+ <td></td>
+ #end if
</tr>
#end for
#end for