]> khamidou — GitHub mirrors - fuzzr.git/commitdiff
first commit
authorkhamidou <khamidou@gmail.com>
Tue, 29 Dec 2009 19:08:10 +0000 (20:08 +0100)
committerkhamidou <khamidou@gmail.com>
Tue, 29 Dec 2009 19:08:10 +0000 (20:08 +0100)
21 files changed:
add-daily-actions.py [new file with mode: 0644]
fuzzr.py [new file with mode: 0644]
models.py [new file with mode: 0644]
rundb [new file with mode: 0755]
static/style.css [new file with mode: 0644]
static/style.css~ [new file with mode: 0644]
templates/add.htm [new file with mode: 0644]
templates/error.htm [new file with mode: 0644]
templates/error.htm~ [new file with mode: 0644]
templates/feed.xml [new file with mode: 0644]
templates/feed.xml~ [new file with mode: 0644]
templates/index.htm [new file with mode: 0644]
templates/index.htm~ [new file with mode: 0644]
templates/layout.htm [new file with mode: 0644]
templates/layout.htm~ [new file with mode: 0644]
templates/login.htm [new file with mode: 0644]
templates/login.htm~ [new file with mode: 0644]
templates/page.htm [new file with mode: 0644]
templates/page.htm~ [new file with mode: 0644]
templates/userfeed.rss~ [new file with mode: 0644]
templates/userfeed.rss~~ [new file with mode: 0644]

diff --git a/add-daily-actions.py b/add-daily-actions.py
new file mode 100644 (file)
index 0000000..b817f5c
--- /dev/null
@@ -0,0 +1,32 @@
+#!/usr/bin/env python
+#
+# This small script adds daily actions for every user.
+# It is run by cron
+
+from pymongo import Connection
+from pymongo.objectid import ObjectId
+import random
+import models
+
+connection = Connection()
+db = connection.fuzzr
+random.seed(None)
+
+users = db.users
+actions = db.actions
+
+def get_random_action():
+    a = actions.find()
+    r = True
+    for i in range(random.randint(0, actions.count())):
+       r = a.next()
+
+    return r
+        
+for user in users.find():
+    act = get_random_action()
+    while (act["_id"] in user["actions-accomplished"]) == True:
+        act = get_random_action()
+
+    models.user_has_done(user["username"], act)
+        
diff --git a/fuzzr.py b/fuzzr.py
new file mode 100644 (file)
index 0000000..b093d84
--- /dev/null
+++ b/fuzzr.py
@@ -0,0 +1,139 @@
+import web
+from web import form
+import models
+from pymongo import Connection
+from pymongo.objectid import ObjectId
+
+
+web.config.debug = False
+
+urls = (
+    '/', 'index',
+    '/add/', 'add',
+    '/page/(.+)/', 'page',
+    '/feed/(.+)/', 'feed',
+    '/login/', 'login',
+    '/logout/', 'logout',
+)
+
+
+app = web.application(urls, globals())
+session = web.session.Session(app, web.session.DiskStore('sessions'))
+render = web.template.render('templates/', base='layout')
+
+connection = Connection()
+db = connection.fuzzr
+users = db.users
+actions = db.actions
+
+def logged():
+    if hasattr(session, 'loggedin') == True:
+        return True
+    else:
+        return False
+
+def notfound():
+       return web.notfound(render.error("404 - Unable to find the requested page"))
+
+class index:
+    def GET(self):
+        return render.index()
+
+
+class add:
+    def __init__(self):
+        self.add_form = form.Form(
+            form.Textbox("title"),
+            form.Textbox("description"))
+
+    def GET(self):
+        
+        if not logged():
+            return render.error("You don't have the priviledges to add actions")
+
+        form = self.add_form()
+        return render.add(form)
+
+    def POST(self):
+        form = self.add_form()
+        if not form.validates(): 
+            return render.add(form)
+        else:
+            act = models.create_post(form.d.title, form.d.description)
+            actions.insert(act)
+            raise web.seeother("/add/")
+
+class page:
+    def GET(self, name):
+#         if logged() == False:
+#             return render.error("You are not logged in")
+
+        user = users.find_one({"username" : name})
+
+        if user == None:
+            return render.error("Page not found")
+
+        l = list()  # We need to give a list because mongo returns an iterator.
+        
+        for id in user["actions-accomplished"]:
+            act = actions.find_one({"_id" : ObjectId(id)})
+            if act != None:
+                l.append(act)
+
+        
+        return render.page(l, name)
+
+class feed:
+    def GET(self, name):
+
+        user = users.find_one({"username" : name})
+        if user == None:
+            return render.error("Page not found")
+
+        l = list()  # We need to give a list because mongo returns an iterator.
+        
+        for id in user["actions-accomplished"]:
+            act = actions.find_one({"_id" : ObjectId(id)})
+            if act != None:
+                l.append(act)
+        
+        feed_render = web.template.render('templates/')
+        web.header('Content-Type', 'application/rss+xml')
+        return feed_render.feed(l, name)
+        
+        
+class login:
+
+    def __init__(self):
+        self.login_form = form.Form(
+            form.Textbox("username"),
+            form.Textbox("password"))
+    
+    def GET(self):
+        form = self.login_form()
+        return render.login(form, None)
+
+    def POST(self):
+        form = self.login_form()
+        if not form.validates(): 
+            return render.login(form, None)
+        else:
+            user = users.find_one({"username" : form.d.username})
+            if user == None:
+                return render.login(form, "Incorrect username or password")
+            else:
+                session.loggedin = True
+                session.username = user["username"]
+
+            raise web.seeother("/page/" + user["username"] + "/")
+
+class logout:
+    def GET(self):
+        session.kill()
+        raise web.seeother("/")
+
+web.internalerror = web.debugerror
+
+if __name__ == "__main__":
+   app.run()
+
diff --git a/models.py b/models.py
new file mode 100644 (file)
index 0000000..41faf49
--- /dev/null
+++ b/models.py
@@ -0,0 +1,44 @@
+from pymongo import Connection
+from pymongo.objectid import ObjectId
+
+
+connection = Connection()
+
+db = connection.fuzzr
+
+users = db.users
+actions = db.actions
+
+def create_post(title, desc, for_musicians=0):
+    return {"title": title,
+            "description": desc,
+            "for-musicians": for_musicians)
+       }
+
+def create_user(username, passwd, is_root, is_musician=0):
+    return {"username": username,
+            "password": passwd,
+            "actions-accomplished": [],
+            "is_root": is_root,
+            "is_musician": is_musician,
+            }
+
+def user_has_done(username, action):
+            user = users.find_one({"username" : username})
+
+            if user == None:
+                return
+
+            user["actions-accomplished"].append(action["_id"])
+            users.save(user, safe=True)
+
+# Simply insert a post and a root user
+def create_db(root, passwd):
+
+    ac = create_post("Wear something green", "Today you're gonna wear something green, to celebrate your irish ancestors")
+    actions.insert(ac)
+
+    us = create_user(root, passwd, 1)
+    users.insert(us)
+
+    
diff --git a/rundb b/rundb
new file mode 100755 (executable)
index 0000000..2d60c2d
--- /dev/null
+++ b/rundb
@@ -0,0 +1,3 @@
+#!/bin/sh
+
+./mongo/bin/mongod --dbpath ./db
\ No newline at end of file
diff --git a/static/style.css b/static/style.css
new file mode 100644 (file)
index 0000000..76aa107
--- /dev/null
@@ -0,0 +1,38 @@
+body {
+  background-color: white;
+  font: 13.34px helvetica, arial, clean, sans-serif;
+  width: 750px;
+  margin: 0 auto;
+
+}
+
+#content {
+        font-size: 150%;
+        float: left;
+        width: 500px;
+        height:500px;
+}
+
+ul {
+
+}
+
+li {
+       list-style-type: none;
+       border: solid 1px;
+       padding: 2px;
+       padding-left: 5px;
+       padding-right: 5px;
+       margin: 20px;
+       
+}
+#sidebar { 
+        float: right;
+        width: 250px;
+        margin: 5px;
+}
+
+#footer {
+       clear:both;
+       text-align: center;
+}
diff --git a/static/style.css~ b/static/style.css~
new file mode 100644 (file)
index 0000000..aeb728b
--- /dev/null
@@ -0,0 +1,11 @@
+body {
+  background-color: white;
+  font: 13.34px helvetica, arial, clean, sans-serif;
+  font-size: small;
+  width: 750px;
+  margin: 0 auto;
+
+}
+
+#wrap {
+}     
\ No newline at end of file
diff --git a/templates/add.htm b/templates/add.htm
new file mode 100644 (file)
index 0000000..3ecd2d4
--- /dev/null
@@ -0,0 +1,6 @@
+$def with (form)
+
+<form name="main" method="post"> 
+$if not form.valid: <p class="error">Try again</p>
+$:form.render()
+<input type="submit" />    </form>
diff --git a/templates/error.htm b/templates/error.htm
new file mode 100644 (file)
index 0000000..63fc21d
--- /dev/null
@@ -0,0 +1,3 @@
+$def with (error)
+
+$error
diff --git a/templates/error.htm~ b/templates/error.htm~
new file mode 100644 (file)
index 0000000..c4d157b
--- /dev/null
@@ -0,0 +1 @@
+$error
diff --git a/templates/feed.xml b/templates/feed.xml
new file mode 100644 (file)
index 0000000..d9c413b
--- /dev/null
@@ -0,0 +1,20 @@
+$def with (actions, username)
+<xml version="1.0" encoding="UTF-8" ?>
+<rss version="2.0">
+
+<channel>
+<title>$username Fuzzr RSS feed</title>
+<description></description>
+<link>http://fuzzr.org/page/$username/feed.xml</link>
+<lastBuildDate></lastBuildDate>
+<pubDate></pubDate>
+
+$for action in actions:
+       <item>
+         <title>$action["title"]</title>
+         <description>$action["description"]</description>
+         <link>http://fuzzr.org/page/$username/</link>
+       </item>
+
+</channel>
+</rss>
diff --git a/templates/feed.xml~ b/templates/feed.xml~
new file mode 100644 (file)
index 0000000..a74e99e
--- /dev/null
@@ -0,0 +1,21 @@
+$def with (actions, username)
+
+<?xml version="1.0" encoding="UTF-8" ?>
+<rss version="2.0">
+
+<channel>
+<title>$username Fuzzr RSS feed</title>
+<description></description>
+<link>http://fuzzr.org/page/$username/feed.rss</link>
+<lastBuildDate></lastBuildDate>
+<pubDate></pubDate>
+
+$for action in actions:
+<item>
+<title>$action["title"]</title>
+<description>$action["description"]</description>
+<link>http://fuzzr.org/page/$username/</link>
+</item>
+
+</channel>
+</rss>
\ No newline at end of file
diff --git a/templates/index.htm b/templates/index.htm
new file mode 100644 (file)
index 0000000..15dcb62
--- /dev/null
@@ -0,0 +1,15 @@
+<link rel="alternate" type="application/rss+xml" 
+   href="/feed/karim/" title="RSS feed for My Page">
+
+<a href="/page/karim/">Page</a>
+<a href="/feed/karim/" type="application/rss+xml">Karim's feed</a>
+<a href="/login/">login</a>
+<a href="/logout/">logout</a>
+<a href="/add/">Add</a>
+
+<div class="content">
+Did you read the Diceman ? If not, you have to know that it's the story of a guy who decides
+to play everything with a dice. I feel a little anxious at the idea of doing something like
+that, but I thought "what if I had to do a random action, something incongruous, to get 
+me out of my confort zone ?", and thus fuzzr was born.
+</div>
diff --git a/templates/index.htm~ b/templates/index.htm~
new file mode 100644 (file)
index 0000000..a1d7d70
--- /dev/null
@@ -0,0 +1,8 @@
+<link rel="alternate" type="application/rss+xml" 
+   href="/feed/karim/" title="RSS feed for My Page">
+
+<a href="/page/karim/">Page</a>
+<a href="/feed/karim/" type="application/rss+xml">Karim's feed</a>
+<a href="/login/">login</a>
+<a href="/logout/">logout</a>
+<a href="/add/">Add</a>
diff --git a/templates/layout.htm b/templates/layout.htm
new file mode 100644 (file)
index 0000000..9e4b8e6
--- /dev/null
@@ -0,0 +1,25 @@
+$def with (content)
+<html>
+<head>
+  <title>Fuzzr</title>
+  <link rel="stylesheet" href="/static/style.css" type="text/css" />
+</head>
+<body>
+<div id="sidebar">
+  <a href="/">Home, home again</a>
+</div>
+<div id="content">
+  $:content
+</div>
+<div id="footer">
+  <tr>
+    <td>
+      <a href="/about/">About</a>
+    </td>
+    <td>
+      <a href="/tos/">Terms of service</a>
+    </td>
+  </tr>
+</div>
+</body>
+</html>
diff --git a/templates/layout.htm~ b/templates/layout.htm~
new file mode 100644 (file)
index 0000000..cf536e1
--- /dev/null
@@ -0,0 +1,11 @@
+$def with (content)
+<html>
+<head>
+  <title>Fuzzr</title>
+  <link rel="stylesheet" href="/static/style.css" type="text/css" />
+</head>
+<body>
+  <a href="/">Home, home again</a>
+  $:content
+</body>
+</html>
diff --git a/templates/login.htm b/templates/login.htm
new file mode 100644 (file)
index 0000000..e5ee1cd
--- /dev/null
@@ -0,0 +1,7 @@
+$def with (form, error)
+
+<form name="main" method="post"> 
+$if not form.valid: <p class="error">Unable to log you, please retry</p>
+$if error: <p class="error">$error</p>
+$:form.render()
+<input type="submit" />    </form>
diff --git a/templates/login.htm~ b/templates/login.htm~
new file mode 100644 (file)
index 0000000..b094dfa
--- /dev/null
@@ -0,0 +1,6 @@
+$def with (form)
+
+<form name="main" method="post"> 
+$if not form.valid: <p class="error">Unable to log you, please retry</p>
+$:form.render()
+<input type="submit" />    </form>
diff --git a/templates/page.htm b/templates/page.htm
new file mode 100644 (file)
index 0000000..d16cab3
--- /dev/null
@@ -0,0 +1,12 @@
+$def with (actions, username)
+
+<div class="content">
+$if username != None: <h1>$username's page</h1>
+  <ul>
+    $for action in actions:
+    <li>
+      <h3>$action["title"]</h3>
+         <p>$action["description"]</p>
+    </li>
+  </ul>
+</div>
diff --git a/templates/page.htm~ b/templates/page.htm~
new file mode 100644 (file)
index 0000000..fbafb20
--- /dev/null
@@ -0,0 +1,9 @@
+$def with (actions, username)
+
+<div class="content">
+$if username != None: <h1>$username's page</h1>
+  <ul>
+    $for action in actions:
+       <li>$action["description"]</li>
+  </ul>
+</div>
diff --git a/templates/userfeed.rss~ b/templates/userfeed.rss~
new file mode 100644 (file)
index 0000000..a74e99e
--- /dev/null
@@ -0,0 +1,21 @@
+$def with (actions, username)
+
+<?xml version="1.0" encoding="UTF-8" ?>
+<rss version="2.0">
+
+<channel>
+<title>$username Fuzzr RSS feed</title>
+<description></description>
+<link>http://fuzzr.org/page/$username/feed.rss</link>
+<lastBuildDate></lastBuildDate>
+<pubDate></pubDate>
+
+$for action in actions:
+<item>
+<title>$action["title"]</title>
+<description>$action["description"]</description>
+<link>http://fuzzr.org/page/$username/</link>
+</item>
+
+</channel>
+</rss>
\ No newline at end of file
diff --git a/templates/userfeed.rss~~ b/templates/userfeed.rss~~
new file mode 100644 (file)
index 0000000..a74e99e
--- /dev/null
@@ -0,0 +1,21 @@
+$def with (actions, username)
+
+<?xml version="1.0" encoding="UTF-8" ?>
+<rss version="2.0">
+
+<channel>
+<title>$username Fuzzr RSS feed</title>
+<description></description>
+<link>http://fuzzr.org/page/$username/feed.rss</link>
+<lastBuildDate></lastBuildDate>
+<pubDate></pubDate>
+
+$for action in actions:
+<item>
+<title>$action["title"]</title>
+<description>$action["description"]</description>
+<link>http://fuzzr.org/page/$username/</link>
+</item>
+
+</channel>
+</rss>
\ No newline at end of file