Archive for the ‘Python’ Tag

Gnome Deskbar Applet plugins

This is a collection of my plugins, I wrote for the Gnome Deskbar Applet. Feel free to use them!
You can find the code repository on launchpad https://code.launchpad.net/~bernde/+junk/deskbar-plugins.

leo de/en translate – plugin

Queries the online dictionary of dict.leo.org, only for english or german words.

deskbar-leo

Read more »

Different templates for iPhone/iPod in Django

I use this Django Middleware for detecting iPhones or iPods and for setting the template directory dynamically. If the user agent is an iPhone or iPod, the template directory is changed, so that different templates for the iProducts can be used. The idea is taken from this snippet, but it only detects iPhones and I had some troubles with caching so I wrote one myself.

from django.conf import settings
import re

class iPhoneMiddleware(object):
    """
    If the Middleware detects an iPhone/iPod the template dir changes to the
    iPhone template folder.
    """

    def __init__(self):
        self.normal_templates = settings.TEMPLATE_DIRS
        self.iphone_templates = (settings.TEMPLATE_DIRS[0] + '/iphone',)

    def process_request(self, request):
        p = re.compile('iPhone|iPod', re.IGNORECASE)
        if p.search(request.META['HTTP_USER_AGENT']):
            # user agent looks like iPhone or iPod
            settings.TEMPLATE_DIRS = self.iphone_templates
        else:
            # other user agents
            settings.TEMPLATE_DIRS = self.normal_templates
        return

BBCode markup in Django

Why BBCode?

I’m a big fan of reStructuredText but faced the problem of being forced to use two versions of one text field: one without any markup for RSS, one with markup for rendering HTML. I found no easy solution how to remove reST markup, so I decided to change markup for this project.
I chose BBCode because it’s pretty easy to understand for ordinary users and it’s easy to remove since BBCode uses a syntax similar to HTML, just with squared brackets.

Example tags: [b][i][u][s][color][size][link][img][list][center][quote]

Read more »

HTML color code field

I needed a field that checks that the value is a valid HTML color code (Hex triplet) like #FFEE00. I started searching, but only found requests for such a field, but no working solution. So it was time to write my first custom DB-model-field. I found this very nice Paper http://toys.jacobian.org/presentations/2007/oscon/tutorial/#s105 that give a first idea how to build a custom field.

The result can be found on http://www.djangosnippets.org/snippets/865/ or here below.

Read more »

Use a templatetag without the load statement

Templatetags are extremely useful, they are similar to helpers in ruby on rails. If you don’t know them you should check out the documentation on how to use and write them: http://www.djangoproject.com/documentation/templates_python/#extending-the-template-system

For my blog-application I wanted the post-time as word like ‘Morning’, ‘Afternoon’, ‘Evening’, etc. rather than the normal time format. I was searching on http://www.djangosnippets.org and found a templatetag called “Fuzzy Time of Day“ from wayalan.
Read more »

Creating a sitemap with Django

The Sitemaps protocol allows a webmaster to inform search engines about URLs on a website that are available for crawling. A sitemap is an XML file that lists a site’s URLs. It allows webmasters to include additional information about each URL: when it was last updated, how often it is likely to be changed, and how important it is in relation to other URLs on the site. This allows search engines to crawl the site more intelligently.

This text shows how to generate a sitemap.xml with “django.contrib.sitemaps“ in Django, based on a simple blog application.
You can download the complete code here [3].
Read more »