Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Creando un foro de la manera fácil (32 líneas)

Aquí es­tán al­gu­nos de los fea­tu­res que quie­ro:

  • Lo­­­gin via twi­­tter / Fa­­ce­­book / Google / Ope­­nID

  • Nú­­me­­ro ili­­mi­­ta­­do de th­­rea­­ds

  • So­­­po­r­­te de like / dis­­like en th­­rea­­ds y en po­s­­ts

  • Ava­­ta­­res

  • HT­­ML en los po­s­­ts

  • Que man­­de mail al usua­­rio si le res­­po­n­­den

  • Fee­­ds RSS pa­­ra los th­­rea­­ds

Se lo pue­de ver en ac­ción en http://­fo­ro­.­ne­t­ma­na­ger­s.­co­ (por un tiem­po li­mi­ta­do ;-)

Y aquí es­tá el có­di­go:

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

def thread(id):
    t = api.threads.details(thread=id)
    return bottle.template('thread.tpl', shortname=shortname, id=id, thread=t.__dict__['response'])

def server_static(path):
    return bottle.static_file(path, root='./static')

app =
app.catchall = False #Now most exceptions are re-raised within bottle.'', port=80, app=app)

Re­quie­re Bo­ttle y la Dis­qus py­thon API

Por su­pues­to que hay un po­qui­to de tem­pla­tes, acá es­tá mai­n.­tpl y th­rea­d.­tpl. Co­mo apes­to pa­ra el HT­M­L, usa Blue­trip CSS y es sen­ci­llo de cus­to­mi­za­r.


Es­ta co­sa es ape­nas una ca­pa de pin­tu­ra en­ci­ma de Dis­qus! Más un blog sin pos­ts pe­ro con co­men­ta­rios que un fo­ro! Pe­ro­... qué le fal­ta pa­ra ser un fo­ro de ver­da­d? Fun­cio­na, no? Has­ta se po­drían usar ca­te­go­rías de Dis­qus pa­ra crear su­bfo­ro­s...

Te­nien­do to­do en cuen­ta, creo que es un ha­ck bo­ni­to.

Y si es­pe­rás unos día­s, es­to lle­va a otra co­sa que es mu­cho más má­gi­ca...

Có­di­go fuen­te com­ple­to en http://­ma­gi­cfo­ru­m.­google­co­de.­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/", 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-2023 Roberto Alsina