1. Daniel Patterson
  2. wishlist


dan...@localhost.localdomain  committed 7b91706

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

  • Participants
  • Branches default

Comments (0)

Files changed (4)


View file
+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
+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);

File app_wishlist.py

View file
+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()

File data.db

Binary file added.

File templates/wishlist.html

View file
+$def with (items, checks)
+<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/>");
+        })
+    }
+<h2>A Wishlist</h2>
+<p style="color: #666; font-size: .9em; line-height: 1.4em;">
+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 style="color: #666; font-size: .9em; line-height: 1.4em;">
+Hope this is all clear, and that this makes everything easier!
+<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>
+$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>