Creating a forum the easy way (32 LOC)
This is only the first part of a project to create the simplest (for me) software forum possible.
Here are the features I want:
Login using twitter / Facebook / Google / OpenID
Unlimited number of threads
Support for like / dislike both on threads and on posts
HTML in comments
Mail the user on replies
RSS feeds for threads
You can see it in action at http://foro.netmanagers.com.ar (for a limited time only ;-)
And here is the code:
import bottle import disqusapi as disqus import json shortname = 'magicmisteryforum' api = disqus.DisqusAPI(open("key").read().strip()) @bottle.route('/', method='GET') def index(): msg = bottle.request.GET.get('msg', '') threads = api.forums.listThreads(forum=shortname, limit=100) print threads return bottle.template('main.tpl', threads=threads, shortname=shortname, msg=msg) @bottle.route('/new', method='POST') def new(): title = bottle.request.forms.get('title', None) if not title: bottle.redirect('/?msg=Missing%20Thread%20Name') return thread = api.threads.create(forum=shortname, title = title) thread_id = thread.__dict__['response']['id'] # Redirecting to /thread/thread_id doesn't work # because threads take a few seconds to appear on the listing bottle.redirect('/') @bottle.route('/thread/:id') def thread(id): t = api.threads.details(thread=id) return bottle.template('thread.tpl', shortname=shortname, id=id, thread=t.__dict__['response']) @bottle.route('/static/:path#.+#') def server_static(path): return bottle.static_file(path, root='./static') app = bottle.app() app.catchall = False #Now most exceptions are re-raised within bottle. bottle.run(host='220.127.116.11', port=80, app=app)
It requires Bottle and the Disqus python API
Of course, there is also a bit of templating involved, here is main.tpl and the thread.tpl. Since I suck at HTML, it uses Bluetrip CSS and it's more than simple enough to customize.
OF COURSE I AM CHEATING!
This thing is just a simple veneer around Disqus! More like a blog with comments and without posts than a forum! But ... what's missing to make this a real forum? It works, doesn't it? You could even use Disqus categories to create subforums...
All things considered, I think it's a cute hack.
And if you wait a few days, this will lead to something much more magical!
Full source code at http://magicforum.googlecode.com
Genialidad en acción
Awesome to see someone using the new API bindings! Would love to hear any feedback or criticism you have about the new API and the bindings.
Well, my main problem is that I can't find a way to create a thread and then show it. It seems there is a long delay between api.threads.create and the moment when you can use posts.create on it, or when it appears on api.threads.list
Other than that, it has been lots of fun :-)
There shouldn't be any delay for creating a post, though there might be a small delay before it appears in the threads/list method. Is there something that would help ease that process (assuming the delay still exists).
When I try to do this:
I get this error:
Ah you're right, I see the code now. Will be making a change so at least for now all creation (POST) requests shouldn't have to deal w/ this delay.
We're aware the delay sucks, do plan to solve the delay eventually (hopefully soon).
No big problem, the user would just have to wait and post later. If this had any users, of course.
Well done. Can't wait to see where this goes.
Yes, bottle is nice for this kind of things. One of the first things I did a few months ago, after switching job, was a 100LOC(?) kind-of-management of dashboard using bootle and Google Charts. It was really low effort, high reward (everybody loves it) exercise.
My only complaint is that there is no syntax support for Disqus, so if I want to paste some code, it goes AWOL. Oh well, maybe you could utilize the pastebin API?
Is it really working?
I'll post the same information to my blog, thanks for
ideas and great article.
Well, the write-up is truly the freshest on this laudable topic.