As I mentioned previously, I wanted to integrate Ecto with my new Django-based blog using the "MetaWeblog" remote procedure call API. With an XMLRPC implementation ready to go, the remaining task is to write MetaWeblog methods and hook them up to my blog model. The model is pretty straightforward; for instance, here are the fields for "Post" object: class Post(models.Model): title = models.CharField(maxlength=200) slug = models.SlugField(prepopulate_from=('title',)) pub_date = models.DateTimeField('date published', blank=True, null=True) create_date = models.DateTimeField('date created', auto_now_add=True, blank=True, null=True) update_date = models.DateTimeField('date updated', auto_now=True, blank=True, null=True) body = models.TextField() tags = models.ManyToManyField(Tag) author = models.ForeignKey(User) status = models.CharField(maxlength=32, choices=STATUS_CHOICES, radio_admin=True, default='Draft') The MetaWeblog API is pretty well documented, but has some features that I'm not quite ready to tackle. For instance, I only have a single user with a single blog, you have to be a "super user" to edit it (no sophisticated authorization scheme yet), and I haven't done media objects (e.g., images) yet either. Most every method takes "username" and "password" parameters, so I wrote an "authenticated" wrapper function to convert them to a user object (and throw an exception if there are any authentication or authorization issues). Then the various methods are short, because the Django database/model API makes things easy. For instance, the method to get information about a blog post (given a post id) is called "getPost". My implementation looks up the post object by id, and then calls a function to turn it into the struct that MetaWeblog requires: @public @authenticated() def metaWeblog_getPost(user, postid): post = Post.objects.get(id=postid) return post_struct(post) def post_struct(post): link = full_url(post.get_absolute_url()) categories = post.tags.all() struct = { 'postid': post.id, 'title': post.title, 'link': link, 'permaLink': link, 'description': post.body, 'categories': [c.name for c in categories], 'userid': post.author.id, } if post.pub_date: struct['dateCreated'] = format_date(post.pub_date) return struct where full_url urljoin's the web site host name ("www.allyourpixel.com") to a URL, and format_date calls xmlrpclib to (you guessed it) turn a date into a string. Here's the whole file: metaweblog.py. It's not a complete application, but hopefully it'll be easy enough to customize for what your doing. |
|||||
5 response to "MetaWeblog & Django"Post a comment |




Hello -
Thank you very much for the XML-RPC / Metaweblog information. They've both given me a ton of ideas on stuff to do with Django.
I was hacking around with the metaweblog implementation and I got the newMediaObject to work. It's very crude -- I just got my first test working. Maybe you've already got this working, but if not, here's the absolute minimum:
@public
@authenticated()
def metaWeblog_newMediaObject(user, blogid, struct):
# The input struct must contain at least three elements, name,
# type and bits. returns struct, which must contain at least one
# element, url
mime = struct['type']
bits = struct['bits']
name = struct['name']
f = open("%s/%s" % (settings.MEDIA_ROOT, name), 'w')
f.write("%s" % bits)
f.close()
return {'url': "%s/%s" % (settings.MEDIA_URL, name)}
Sorry about the formatting ...
Anyways, I'm sure there's a lot of stuff to add here such as max file size, only accepting certain mime's, etc.. But it works!
nice site
good work
Thank you for sharing this. I extended your code for my own blog for posting multimedia content. I use a model with a FileField taking care of uploaded files. For more information, see my description at http://qoli.de/blog/2007/nov/19/implemen...
Have you implemented all three of the main XML-RPC blogging APIs, i.e. Blogger, MetaWeblog and MovableType? This is what most desktop blogging clients support (including QTM, which I write). It would be great if QTM could support Django as well as the others it supports.