django-pingback

annotate README.md @ 32:6ca8eadcd22d

fix relative urls and some additional logging
author Alexander Artemenko <svetlyak.40wt@gmail.com>
date Sun Dec 14 18:10:11 2008 +0200
parents f3bc8c661309
children f06451c79457
rev   line source
svetlyak@22 1 django-pingback
svetlyak@22 2 ===============
svetlyak@22 3
svetlyak@22 4 This two applications provide 3 connected services:
svetlyak@22 5 pingback server, pingback client and directory ping client.
piranha@26 6
piranha@26 7 Depends on the [django-xmlrpc][].
svetlyak@22 8
svetlyak@22 9 Configuration
svetlyak@22 10 -------------
svetlyak@22 11
piranha@26 12 First, install the [django-xmlrpc][] application. You can download it either
piranha@26 13 from [repo][django-xmlrpc] or just use setuptools:
svetlyak@22 14
svetlyak@24 15 easy_install -f http://pypi.aartemenko.com django-xmlrpc
svetlyak@24 16
svetlyak@24 17 Next, download and install `django-pingback`:
svetlyak@24 18
piranha@26 19 * download sources from main [repository][django-pingback]
piranha@26 20 * or use `easy_install django-pingback`
piranha@26 21 * add `pingback` to your `INSTALLED_APPS`
piranha@26 22 * run `./manage.py syncdb`
piranha@26 23 * setup client and server callbacks.
svetlyak@22 24
svetlyak@22 25
svetlyak@22 26 Connecting server
svetlyak@22 27 -----------------
svetlyak@22 28
svetlyak@22 29 Pingback server receives pings from other sites, so, we must
svetlyak@22 30 create function which binds our URLs an objects.
svetlyak@22 31
svetlyak@22 32 But first of all, add this urlpattern to your urls configuration:
svetlyak@22 33
svetlyak@22 34 ((r'^xmlrpc/$', 'django_xmlrpc.views.handle_xmlrpc', {}, 'xmlrpc'))
svetlyak@22 35
svetlyak@22 36 It is a handler for all xmlrpc requests.
svetlyak@22 37
svetlyak@22 38 Usually, blog has a detailed view for each post. Suppose that
piranha@26 39 our view has name `post_detail` and accepts one keyword arguments
svetlyak@22 40 `slug`.
svetlyak@22 41
svetlyak@22 42 Here is simple example, how to make Post objects pingable:
svetlyak@22 43
svetlyak@22 44 from datetime import time, date, datetime
svetlyak@22 45 from time import strptime
piranha@26 46
svetlyak@22 47 from blog.models import Post
svetlyak@22 48 from pingback import create_ping_func
svetlyak@22 49 from django_xmlrpc import xmlrpcdispatcher
svetlyak@22 50
piranha@26 51 # create simple function which returns Post object and accepts
piranha@26 52 # exactly same arguments as 'details' view.
svetlyak@22 53 def pingback_blog_handler(slug, **kwargs):
svetlyak@22 54 return Post.objects.get(slug=slug)
svetlyak@22 55
svetlyak@22 56 # define association between view name and our handler
svetlyak@22 57 ping_details = {'post_detail': pingback_blog_handler}
svetlyak@22 58
svetlyak@22 59 # create xml rpc method, which will process all
svetlyak@22 60 # ping requests
svetlyak@22 61 ping_func = create_ping_func(**ping_details)
svetlyak@22 62
svetlyak@22 63 # register this method in the dispatcher
svetlyak@22 64 xmlrpcdispatcher.register_function(ping_func, 'pingback.ping')
svetlyak@22 65
svetlyak@22 66 Now, go at you http://mysweetsite.com/xmlrpc/ and you should
svetlyak@22 67 see `pingback.ping` method among few other system methods. If it
svetlyak@22 68 is not there, then you made mistake in you server setup.
svetlyak@22 69
svetlyak@22 70 Also, you need to tell other sites, that your blog accepts
svetlyak@22 71 pingbacks. You can do it by adding a link in the head of your site:
svetlyak@22 72
svetlyak@22 73 <link rel="pingback" href="{% url 'xmlrpc' %}" />
svetlyak@22 74
svetlyak@22 75 Or by adding X-Pingback HTTP header. Do do this, just add such line
svetlyak@22 76 in the settings.py:
svetlyak@22 77
svetlyak@22 78 MIDDLEWARE_CLASSES = [
svetlyak@22 79 # ...
svetlyak@22 80 'pingback.middleware.XPingMiddleware',
svetlyak@22 81 ]
svetlyak@22 82
svetlyak@22 83 Connecting client signals
svetlyak@22 84 -------------------------
svetlyak@22 85
svetlyak@22 86 Let's suppose, that you have a blog and want to ping external sites
svetlyak@22 87 (like Technorati) on post save, and to receive pingbacks from other
svetlyak@22 88 sites. Next two sections contain simple 'how-to' enable these features.
svetlyak@22 89
svetlyak@22 90 At first, setup configuration in the settings, here is an example:
svetlyak@22 91
svetlyak@22 92 DIRECTORY_URLS = (
svetlyak@22 93 'http://ping.blogs.yandex.ru/RPC2',
svetlyak@22 94 'http://rpc.technorati.com/rpc/ping',
svetlyak@22 95 )
svetlyak@22 96
svetlyak@22 97
svetlyak@22 98 Next, you must connect some signals to ping workers,
svetlyak@22 99 which created using `ping_external_links` and `ping_directories`
svetlyak@22 100 functions:
svetlyak@22 101
svetlyak@22 102 from django.db.models import signals
svetlyak@22 103 from pingback.client import ping_external_links, ping_directories
svetlyak@22 104 from blog.models import Post
piranha@26 105
svetlyak@22 106 signals.post_save.connect(
svetlyak@22 107 ping_external_links(content_attr = 'html',
svetlyak@22 108 url_attr = 'get_absolute_url'),
svetlyak@22 109 sender = Post, weak = False)
svetlyak@22 110
svetlyak@22 111 signals.post_save.connect(
svetlyak@22 112 ping_directories(content_attr = 'html',
svetlyak@22 113 url_attr = 'get_absolute_url'),
svetlyak@22 114 sender = Post, weak = False)
svetlyak@22 115
piranha@26 116 Please note, that in the `content_attr` you must give either attribute or method
piranha@26 117 name, which returns HTML content of the object.
svetlyak@22 118
piranha@26 119 If you don't have such attribute or method, for example if you apply markdown
piranha@26 120 filter in the template, then `content_func` argument can be used instead of the
piranha@26 121 `content_attr`.
svetlyak@22 122
piranha@26 123 `content_func` must return HTML, and must accepts an instance as a single
piranha@26 124 argument.
svetlyak@22 125
piranha@26 126 Pay attention to the `weak = False` argument. If case of omitting django's event
piranha@26 127 dispatcher will remove your signal.
svetlyak@22 128
svetlyak@22 129 Template tags
svetlyak@22 130 -------------
svetlyak@22 131
svetlyak@22 132 To show pingbacks on your page, you can use code like this:
svetlyak@22 133
svetlyak@22 134 {% load pingback_tags %}
svetlyak@22 135 {% get_pingback_list for object as pingbacks %}
svetlyak@22 136 {% if pingbacks %}
svetlyak@22 137 <h1>Pingbacks</h1>
svetlyak@22 138 {% for pingback in pingbacks %}
svetlyak@22 139 <div class="b-pingback">
svetlyak@22 140 <p class="b-meta">
svetlyak@22 141 <a name="pingback-{{ pingback.id }}" href="{{ object.get_absolute_url }}#pingback-{{ pingback.id }}" class="b-permlink">permalink</a>
svetlyak@22 142 {{ pingback.date }}, pingback from {{ pingback.url|urlizetrunc:40 }}:
svetlyak@22 143 </p>
svetlyak@22 144
svetlyak@22 145 <p>{{ pingback.content }}</p>
svetlyak@22 146 </div>
svetlyak@22 147 {% endfor %}
svetlyak@22 148 {% endif %}
svetlyak@22 149
piranha@26 150 Also you can use `{% get_pingback_count for object as cnt %}`, to save
svetlyak@22 151 pingbacks' count in the context variable.
svetlyak@22 152
svetlyak@24 153 [django-xmlrpc]: https://code.launchpad.net/~aartemenko/django-xmlrpc/svetlyak40wt
piranha@26 154 [django-pingback]: http://hg.piranha.org.ua/django-pingback/
Repositories maintained by Alexander Solovyov