summaryrefslogtreecommitdiff
path: root/servicios/xmlrpc.py
blob: 51e2b6827d7bb410255ba656204f02be18f7fcf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# vim: set fileencoding=utf-8 ts=4 shiftwidth=4 softtabstop=4 expandtab:
#
# Usando el código de
# Brendan W. McAdams <brendan.mcadams@thewintergrp.com>
# http://code.djangoproject.com/wiki/XML-RPC

from SimpleXMLRPCServer import SimpleXMLRPCDispatcher
from django.http import HttpResponse

from blog.models import Articulo

dispatcher = SimpleXMLRPCDispatcher(allow_none=False, encoding=None)


def rpc_handler(request):
    """
    the actual handler:
    if you setup your urls.py properly, all calls to the xml-rpc service
    should be routed through here.
    If post data is defined, it assumes it's XML-RPC and tries to process as such
    Empty post assumes you're viewing from a browser and tells you about the service.
    """

    response = HttpResponse()
    if len(request.POST):
        response.write(dispatcher._marshaled_dispatch(request.raw_post_data))
    else:
        response.write("<h1>Éste es el servicio XML-RPC.</h1>")
        response.write("<p>Deberías invocarlo usando un cliente XML-RPC.</p>")
        response.write("<h2>Métodos disponibles:</h2><ul>")
        methods = dispatcher.system_listMethods()

        for method in methods:
            sig = dispatcher.system_methodSignature(method)
            help = dispatcher.system_methodHelp(method)
            response.write("<li><b>%s</b>: [%s] %s</li>" % (method, sig, help))

        response.write("</ul>")

    response['Content-length'] = str(len(response.content))
    return response


def articulos_blog(max=10):
    """
    Devuelve los últimos artículos del blog. Admite un argumento, que indica el
    número de artículos máximo a devolver.
    """
    articulos = Articulo.objects.all()
    lista = []
    for a in articulos:
        lista.append({
            "titulo": a.titulo,
            "autor": a.autor.username,
            "created_on": a.created_on.__str__(),
            "texto": a.texto
        })
    return lista
dispatcher.register_function(articulos_blog, 'articulos_blog')