Implementing the MetaWeblog API
19. November 2007
Posting in a blog with an external program needs support by an API. One of these is MetaWeblog API, implemented for Django by Greg Abbas. I adapted his implementation for my needs and extended it so my blog can handle multimedia content in posts.
Now I could post with images to the blog if not the blog tools I found for Linux were so horribly bad. With Gnome-Blog I can upload images from the desktop, but cannot mark a post as unpublished, which is a little bit dangerous for me. BloGTK has it the other way round, Drivel does not support the MetaWeblog API. I don't want to install the KDE panel applet KBlogger on my Gnome system. Are there any alternatives? Well, I'm doing this to be able to blog via my smartphone, so it doesn't matter anyway. Here comes the code.
I added a table Attachment to my model, that take care about content attached to blog posts:
class Attachment(models.Model):
"""
A Attachment to a blog post
"""
filename= models.CharField(max_length=100)
contenttype= models.CharField(max_length=255)
content= models.FileField(upload_to='blog-attach/%Y/%m')
class Admin:
pass
def delete(self):
import os
filename=self.get_content_filename()
try:
os.remove(filename)
except OSError:
pass
models.Model.delete(self)
Then I added the RPC function metaWeblog_newMediaObject that is missing in Greg's code:
@public
@authenticated()
def metaWeblog_newMediaObject(user, blogid, struct):
bits = b64decode(struct['bits'])
name = struct['name']
mime = struct['type']
attachment= Attachment(content= bits,
filename= name,
contenttype= mime)
attachment.save_content_file(name, bits)
attachment.save()
return {'url': attachment.get_content_url()}
#1 rgergerger schrieb am 4. November 2008:
Thank for making this valuable information available to the public..