gaelog

Rich Text Editor in Django Admin

  The built in Django admin application is quite useful in typing simple data into your system. But when dealing with complex html format, it just can't work.

  Then integrating with rich text editor is a good choice, but it need some extra work either in the admin itself or using the newform admin branch. After looking around into this branch, i'm not sure if it's the right time to use it right now. On the other hand, i choose the other way round to solve this problem, yup, i'm not adding the rich editor into django, but to setup a FCKeditor outside.

  Now i can insert all the things in this editor, like images, urls, flash, etc. After finishing, just click the source button to get the xhtml1.1 code and paste them into the TextField in Django admin interface. Honestly, it saves me a lot time.

  The one thing more i want to say is that, the rerason why there is no popular blog system built in Django out there yet is that everybody could start their own app in quite a short time, and find sort of UGLY ways to overcome the shortages like this. And more ugly the method is, the less people wanting to show their code to the others. So i really hope that the newform admin branch could fix these kind of problem and bring us a much powerful admin interface in the future.

 

Inspired by:

http://yuiblog.com/blog/2007/08/13/rte-notes/

Trackback Class in Python/Django

  Trackback is a simple way of linking blog entries together, and it might be the most misused for SPAM.

  When i'm trying to build Gaelog, I google a lot about trackback lib writing in Python, but only get one called tblib. It's a very simple trackback class using the default httplib. The thing about httplib is that it can not set a timeout attribute which i think is very important in doing the trackback. Because if a remote server can not handle the post in time, the function will always be blocked.

  So i end up writing my own trackback class based on the wonderful pycurl.

  Here is the code ( Most of them based on the tblib, it's ok? ):

class TrackBack:
    
    def __init__(self, tbUrl=None, title=None, excerpt=None, url=None, blog_name=None):
        self.tbUrl = tbUrl
        self.title = title
        self.excerpt = excerpt
        self.url = url
        self.blog_name = blog_name
        self.tbErrorCode = None
        self.tbErrorMessage = None

    def ping(self):
        if self.tbUrl:
            params = urllib.urlencode({'title': self.title, 'url': self.url, 'excerpt': self.excerpt, 'blog_name': self.blog_name})            
            agent = "Appoil.com/0.0.1 Python"
            c = pycurl.Curl()
            c.setopt(pycurl.URL, smart_str(self.tbUrl))
            c.setopt(c.POSTFIELDS, params)
            b = StringIO.StringIO()
            c.setopt(pycurl.WRITEFUNCTION, b.write)
            c.setopt(pycurl.FOLLOWLOCATION, 0)
            c.setopt(pycurl.USERAGENT, agent)
            c.setopt(pycurl.SSL_VERIFYPEER, 0)            
            c.setopt(pycurl.TIMEOUT, 30)            
            c.perform()                    
            self.tbResponse = b.getvalue()
            errorpattern = r'(.*?)'
            reg = re.search(errorpattern, self.tbResponse)
            if reg:
                self.tbErrorCode = reg.group(1)
                if int(self.tbErrorCode) == 1:
                    errorpattern2 = r'(.*?)'
                    reg2 = re.search(errorpattern2, self.tbResponse)
                    if reg2:
                        self.tbErrorMessage = reg2.group(1)
        else:
            return 1

  If you came here from the trackback links that means this class works just fine. :-)

  There is one more thing i should say about the pycurl. If you are trying to compile pycurl lib from source code, the LD_LIBRARY_PATH environment variable is a bit tricky. I spent quite some time to figure out why it don't accept the PATH i set. After compiling the lib for several times, it's ok within Bash. But Django still can't find the libcurl.so file. My last attempt was to copy the .so file into my own lib directory. Luckily everything seems all right this time.

Inspired by:

http://weblog.philringnalda.com/2003/02/01/tblib-a-python-trackback-library

http://coderseye.com/2007/trackbacks-on-banjo.html

Talk to me, plz!
© 2008 Appoil The Why