Commits

Anonymous committed 7b91706

initial commit. working with current web.py as of this date (0.33).

Comments (0)

Files changed (4)

+May 2009. daniel patterson (http://dbpatterson.com), BSD license.
+
+A simple wishlist app based on web.py. Allows people to check off things they intend to buy, and uncheck if they make a mistake. assumes that people wont uncheck others boxes, using a small record of when things were checked and unchecked as a slight deterance. Don't use this anywhere where you think people would want to mess with you!
+
+To get it running, you need an sqlite database file with two tables, items and checks. I've provided an empty one that should work, which means all you need to do is run:
+
+python app_wishlist.py fastcgi 127.0.0.1:9999
+
+and then set up a web server to sit in front of it (I use nginx, but lighttpd or the like would work equally). alternatively, you could use the built in debuggging web server with:
+python app_wishlist.py
+
+but don't put that anywhere where load is a concern!
+
+
+And then of course to actually add items to the list you just need to insert rows into the database table 'items'. Since this was just something for me to use, it was easiest to just use:
+sqlite3 data.db
+
+and then use regular SQL to update it. ie:
+
+INSERT into items  (name, dec, url, price, checked) VALUES ("Name", "Some longer description", "Url to where it can be bought", "$20", false);
+
+import web,time
+
+urls = (
+'/', 'index',
+)
+
+app = web.application(urls, locals(), autoreload=False)
+db = web.database(dbn='sqlite', db='data.db')
+render =  web.template.render('templates/', cache=True)
+class index:
+    def GET(self):
+        items = db.select("items", order="name ASC").list()
+        tmpchecks = db.select("checks").list()
+        lis = list()
+        for n in range(len(items)):
+            lis.append(list())
+        checks = dict(zip(map(lambda x: x["id"], items), lis))
+        for a in tmpchecks:
+            if a["type"] == "check":
+                checks[int(a["item"])].append("Checked on " + a["time"]) 
+            elif a["type"] == "uncheck":
+                checks[int(a["item"])].append("Unchecked on " + a["time"]) 
+        return render.wishlist(items, checks)
+    def POST(self):
+        posted = web.input()
+        now = time.strftime("%D at %T")
+        db.insert("checks", type=posted.type, item=posted.item, time=now)
+        if posted.type=="check":
+            db.update("items", where="id='%s'"%posted.item, checked="true")
+        elif posted.type=="uncheck":
+            db.update("items", where="id='%s'"%posted.item, checked="false")
+        return posted.type.capitalize() + "ed on " + now
+if __name__ == '__main__':
+  web.webapi.internalerror = web.debugerror
+  app.run()
Binary file added.

templates/wishlist.html

+$def with (items, checks)
+
+<html><body>
+
+<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
+<script type="text/javascript">
+function toggleCheck(id) {
+    if(\$("#id"+id).attr("checked")){
+        \$.post("/",{ type: "check", item: id}, function(text) {
+            \$("#checks"+id).append(text + "<br/>");
+        })
+    } else {
+        \$.post("/",{ type: "uncheck", item: id}, function(text) {
+            \$("#checks"+id).append(text + "<br/>");
+        })
+    }
+}
+</script>
+
+<h2>A Wishlist</h2>
+<p style="color: #666; font-size: .9em; line-height: 1.4em;">
+FEEL FREE TO CHANGE THIS!<br/><br/>
+
+The way this works is pretty simple - if you find an item (or multiple) that you think match what you would like to gift, click on the check box - that will mark that you are interested in giving that item, so noone duplicates anything (but, to preserve the element of surprise, neither I nor anyone else can see who has checked what). Because of that anonymity, and since it would be good to be able to uncheck something, ANYONE can uncheck something - please dont do it! And the checks and unchecks are recorded (viewable in gray below the description). Clicking on the price links to somewhere it can be purchased.
+</p>
+<p style="color: #666; font-size: .9em; line-height: 1.4em;">
+Hope this is all clear, and that this makes everything easier!
+</p>
+
+<h4>Finally - another option is to give no gift at all! The point of this is not to streamline gift getting, but rather to avoid if at all possible getting unnecessary stuff, and try to make doing that as easy as possible!</h4>
+
+<table>
+$for i in items:
+    <tr><td><form><input id="id$i.id" type="checkbox" \
+    $if i.checked=="true":
+        checked \
+    onclick="toggleCheck($i.id)"></input></form></td><td style="font-weight: bold;">$i.name</td><td><a href="$i.url">$i.price</a></td><td style="padding: 10px; color: #777; font-size: .9em;">$i.dec</td></tr>
+    <tr>
+    <td></td><td></td><td></td>
+    <td>
+    <div id="checks$i.id" style="font-size: .6em; color: #ccc">
+    $for c in checks[i.id]:
+        $c<br/>
+    </div>
+    </td><tr>
+</body>
+</html>