From cb70069b0c5f9372597ed7317ca255abb0779957 Mon Sep 17 00:00:00 2001 From: khamidou Date: Tue, 29 Dec 2009 20:08:10 +0100 Subject: [PATCH] first commit --- add-daily-actions.py | 32 +++++++++ fuzzr.py | 139 +++++++++++++++++++++++++++++++++++++++ models.py | 44 +++++++++++++ rundb | 3 + static/style.css | 38 +++++++++++ static/style.css~ | 11 ++++ templates/add.htm | 6 ++ templates/error.htm | 3 + templates/error.htm~ | 1 + templates/feed.xml | 20 ++++++ templates/feed.xml~ | 21 ++++++ templates/index.htm | 15 +++++ templates/index.htm~ | 8 +++ templates/layout.htm | 25 +++++++ templates/layout.htm~ | 11 ++++ templates/login.htm | 7 ++ templates/login.htm~ | 6 ++ templates/page.htm | 12 ++++ templates/page.htm~ | 9 +++ templates/userfeed.rss~ | 21 ++++++ templates/userfeed.rss~~ | 21 ++++++ 21 files changed, 453 insertions(+) create mode 100644 add-daily-actions.py create mode 100644 fuzzr.py create mode 100644 models.py create mode 100755 rundb create mode 100644 static/style.css create mode 100644 static/style.css~ create mode 100644 templates/add.htm create mode 100644 templates/error.htm create mode 100644 templates/error.htm~ create mode 100644 templates/feed.xml create mode 100644 templates/feed.xml~ create mode 100644 templates/index.htm create mode 100644 templates/index.htm~ create mode 100644 templates/layout.htm create mode 100644 templates/layout.htm~ create mode 100644 templates/login.htm create mode 100644 templates/login.htm~ create mode 100644 templates/page.htm create mode 100644 templates/page.htm~ create mode 100644 templates/userfeed.rss~ create mode 100644 templates/userfeed.rss~~ diff --git a/add-daily-actions.py b/add-daily-actions.py new file mode 100644 index 0000000..b817f5c --- /dev/null +++ b/add-daily-actions.py @@ -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 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 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 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 index 0000000..76aa107 --- /dev/null +++ b/static/style.css @@ -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 index 0000000..aeb728b --- /dev/null +++ b/static/style.css~ @@ -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 index 0000000..3ecd2d4 --- /dev/null +++ b/templates/add.htm @@ -0,0 +1,6 @@ +$def with (form) + +
+$if not form.valid:

Try again

+$:form.render() +
diff --git a/templates/error.htm b/templates/error.htm new file mode 100644 index 0000000..63fc21d --- /dev/null +++ b/templates/error.htm @@ -0,0 +1,3 @@ +$def with (error) + +$error diff --git a/templates/error.htm~ b/templates/error.htm~ new file mode 100644 index 0000000..c4d157b --- /dev/null +++ b/templates/error.htm~ @@ -0,0 +1 @@ +$error diff --git a/templates/feed.xml b/templates/feed.xml new file mode 100644 index 0000000..d9c413b --- /dev/null +++ b/templates/feed.xml @@ -0,0 +1,20 @@ +$def with (actions, username) + + + + +$username Fuzzr RSS feed + +http://fuzzr.org/page/$username/feed.xml + + + +$for action in actions: + + $action["title"] + $action["description"] + http://fuzzr.org/page/$username/ + + + + diff --git a/templates/feed.xml~ b/templates/feed.xml~ new file mode 100644 index 0000000..a74e99e --- /dev/null +++ b/templates/feed.xml~ @@ -0,0 +1,21 @@ +$def with (actions, username) + + + + + +$username Fuzzr RSS feed + +http://fuzzr.org/page/$username/feed.rss + + + +$for action in actions: + +$action["title"] +$action["description"] +http://fuzzr.org/page/$username/ + + + + \ No newline at end of file diff --git a/templates/index.htm b/templates/index.htm new file mode 100644 index 0000000..15dcb62 --- /dev/null +++ b/templates/index.htm @@ -0,0 +1,15 @@ + + +Page +Karim's feed +login +logout +Add + +
+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. +
diff --git a/templates/index.htm~ b/templates/index.htm~ new file mode 100644 index 0000000..a1d7d70 --- /dev/null +++ b/templates/index.htm~ @@ -0,0 +1,8 @@ + + +Page +Karim's feed +login +logout +Add diff --git a/templates/layout.htm b/templates/layout.htm new file mode 100644 index 0000000..9e4b8e6 --- /dev/null +++ b/templates/layout.htm @@ -0,0 +1,25 @@ +$def with (content) + + + Fuzzr + + + + +
+ $:content +
+ + + diff --git a/templates/layout.htm~ b/templates/layout.htm~ new file mode 100644 index 0000000..cf536e1 --- /dev/null +++ b/templates/layout.htm~ @@ -0,0 +1,11 @@ +$def with (content) + + + Fuzzr + + + + Home, home again + $:content + + diff --git a/templates/login.htm b/templates/login.htm new file mode 100644 index 0000000..e5ee1cd --- /dev/null +++ b/templates/login.htm @@ -0,0 +1,7 @@ +$def with (form, error) + +
+$if not form.valid:

Unable to log you, please retry

+$if error:

$error

+$:form.render() +
diff --git a/templates/login.htm~ b/templates/login.htm~ new file mode 100644 index 0000000..b094dfa --- /dev/null +++ b/templates/login.htm~ @@ -0,0 +1,6 @@ +$def with (form) + +
+$if not form.valid:

Unable to log you, please retry

+$:form.render() +
diff --git a/templates/page.htm b/templates/page.htm new file mode 100644 index 0000000..d16cab3 --- /dev/null +++ b/templates/page.htm @@ -0,0 +1,12 @@ +$def with (actions, username) + +
+$if username != None:

$username's page

+
    + $for action in actions: +
  • +

    $action["title"]

    +

    $action["description"]

    +
  • +
+
diff --git a/templates/page.htm~ b/templates/page.htm~ new file mode 100644 index 0000000..fbafb20 --- /dev/null +++ b/templates/page.htm~ @@ -0,0 +1,9 @@ +$def with (actions, username) + +
+$if username != None:

$username's page

+
    + $for action in actions: +
  • $action["description"]
  • +
+
diff --git a/templates/userfeed.rss~ b/templates/userfeed.rss~ new file mode 100644 index 0000000..a74e99e --- /dev/null +++ b/templates/userfeed.rss~ @@ -0,0 +1,21 @@ +$def with (actions, username) + + + + + +$username Fuzzr RSS feed + +http://fuzzr.org/page/$username/feed.rss + + + +$for action in actions: + +$action["title"] +$action["description"] +http://fuzzr.org/page/$username/ + + + + \ No newline at end of file diff --git a/templates/userfeed.rss~~ b/templates/userfeed.rss~~ new file mode 100644 index 0000000..a74e99e --- /dev/null +++ b/templates/userfeed.rss~~ @@ -0,0 +1,21 @@ +$def with (actions, username) + + + + + +$username Fuzzr RSS feed + +http://fuzzr.org/page/$username/feed.rss + + + +$for action in actions: + +$action["title"] +$action["description"] +http://fuzzr.org/page/$username/ + + + + \ No newline at end of file -- 2.43.0