Skip to main content

Ralsina.Me — Roberto Alsina's website

Creating a forum the easy way (32 LOC)

This is on­ly the first part of a project to cre­ate the sim­plest (for me) soft­ware fo­rum pos­si­ble.

Here are the fea­tures I wan­t:

  • Lo­­gin us­ing twit­ter / Face­­book / Google / OpenID

  • Un­lim­it­ed num­ber of threads

  • Sup­­port for like / dis­­­like both on threads and on posts

  • Avatars

  • HTML in com­­ments

  • Mail the us­er on replies

  • RSS feeds for threads

You can see it in ac­tion at http://­foro.net­man­ager­s.­com.ar (for a lim­it­ed time on­ly ;-)

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[0]
    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='184.82.108.14', port=80, app=app)

It re­quires Bot­tle and the Dis­qus python API

Of course, there is al­so a bit of tem­plat­ing in­volved, here is main.t­pl and the thread­.t­pl. Since I suck at HTM­L, it us­es Bluetrip CSS and it's more than sim­ple enough to cus­tom­ize.

OF COURSE I AM CHEAT­ING!

This thing is just a sim­ple ve­neer around Dis­qus! More like a blog with com­ments and with­out posts than a fo­rum! But ... what's miss­ing to make this a re­al fo­rum? It work­s, does­n't it? You could even use Dis­qus cat­e­gories to cre­ate sub­fo­rum­s...

All things con­sid­ered, I think it's a cute hack.

And if you wait a few days, this will lead to some­thing much more mag­i­cal!

Full source code at http://­mag­ic­fo­rum.­google­code.­com

El Nassto / 2011-03-30 00:30:

Genialidad en acción

David Cramer / 2011-03-30 01:59:

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.

Roberto Alsina / 2011-03-30 02:04:

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 :-)

David Cramer / 2011-03-30 02:06:

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).

Roberto Alsina / 2011-03-30 02:11:

When I try to do this:

    thread = api.threads.create(forum=shortname, title = title)
thread_id = thread.__dict__['response']['id']
api.posts.create(thread=thread_id, message="Post about %s here!"%title)

I get this error:

    api.posts.create(thread=thread_id, message="Post about %s here!"%title)
File "/usr/lib/python2.6/site-packages/disqusapi/__init__.py", line 163, in __call__
raise APIError(data['code'], data['response'])
APIError: 2: Invalid argument, 'thread': Unable to find thread with id '266344105'


David Cramer / 2011-03-30 02:16:

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).

Roberto Alsina / 2011-03-30 02:22:

No big problem, the user would just have to wait and post later. If this had any users, of course.

Tyler Hayes / 2011-03-30 02:09:

Well done. Can't wait to see where this goes.

Shulai / 2011-04-03 02:06:

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.

blogvetica / 2011-04-09 21:42:

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?

El vacas de cuero / 2011-04-29 20:49:

Is it really working?

Roberto Alsina / 2011-04-29 21:45:
White Veneers / 2011-05-18 07:18:

I'll post the same information to my blog, thanks for
ideas and great article.

 

employment background check / 2011-12-27 23:33:


Well, the write-up is truly the freshest on this laudable topic. 


Contents © 2000-2020 Roberto Alsina