Broken County Borders broke the townlands

Leitrim and Cavan county borders were broken in Open Street Map for about 2 days just now, and it caused the townlands to break. I've updated the code to break when this happens (it means those counties aren't in the DB now), but the border has been fixed. Those counties should appear tomorrow.

Published on

Tags:

Oops

Oops. Turns out I had a bug in my "detect if this user is a bot" part, where my site would have an internal error if a bot when to it (and I'd get an error email from django obviously). I've fixed that now (hopefully).

And I wondered why there was like 1 or 2 pages coming up in the google search results for this site.... :P

Published on

Tags:

Including your management commands in your setup.py

Django allows you to have management commands which allow you to have chucks of code which can be executed from the command line.

pip et al. are great ways to install software, and can be used to distribute your django app (e.g. Bare minimum setup.py for a django app)

If you have a management command, you will need to ensure that your packages line includes APPNAME.management and APPNAME.management.commands.

e.g. it should look something like this:

…
packages=['APPNAME', 'APPNAME.management', 'APPNAME.management.commands'],
…

Remember to include both the ….management and ….management.commands.

Published on

Tags:

Slugifying something in python

Django comes with a handy template tag to turn any string into a nice 'slug', which you can use in URLs. If you ever need to use this in python, you can do this:

from django.template.defaultfilters import slugify

and then call the slugify function, e.g. slugify('My string here')

Published on

Tags:

Undoing all the migrations

You can migrate a django south app to a specific version with this command: python manage.py migrate $APPNAME $NUMBER, i.e. to migrate it to the one that starts with 0002_…, enter this command: python manage.py migrate $APPNAME 0002.

You can undo all the migrations by giving using zero, i.e. python manage.py migrate $APPNAME zero. I initially tried 0000, but that doesn't work. This will 'remove' all the migrations done for an app. (The migrations will still be there as files, but the models won't be there)

Published on

Tags:

ManyToMany fields checkboxes in Django Admin

When you have a ManyToMany field in Django and use the Django Admin, it by default uses a <select> widget. It's easy to change this to be a list of checkboxes (i.e. <input type=checkbox>).

Checkboxes, in the default Django Admin style/CSS are easier to read than a multiple select box.

If you have a Post object, with a tags ManyToMany field, then this code in your admin.py will turn the tags field into checkboxes.

class PostAdminForm(forms.ModelForm):
    class Meta:
        widgets = {
            'tags': forms.CheckboxSelectMultiple
        }

class PostAdmin(admin.ModelAdmin):
    form = PostAdminForm

admin.site.register(Post, PostAdmin)

Published on

Tags:

Bare minimum setup.py for a single file python module

Similar to my Bare minimum setup.py for a django app, here's how to do a setup.py for a python project that consists of a single module file.

Sometimes you only want to distribute a single python file as a module. It might just be a few simple functions, and rather than have a whole directory, it's much easier to have a single python file that can be imported and used.

Assuming your python file is called name_of_file.py, then make a file setup.py with this content in the same directory as name_of_file.py:

#! /usr/bin/env python

from setuptools import setup, find_packages

setup(name="name_of_package",
      version="0.0.1",
      author="Your Name",
      author_email="you@example.org",
      py_modules=['name_of_file'],
)

Published on

Tags:

Replace the transparency on a PNG with a white background

If you have a png file with a transparent background, you can easily replace it with a white background using this command:

mogrify -flatten -background white FILENAME

(replace FILENAME with the name of the file).

It will work on the file itself, overwriting the old file.

Published on

Tags:

    Undo the local changes in a git file

    If you're using git to manage you files, and you have some changes to some files, and you want to throw away the changes (i.e. revert to the previous checked in version), then you can do it with this:

    git checkout path/to/myfile.ext
    

    (replace the path/to/my/file.ext with the path/filename of the file you want to 'revert')

    This is the git equivalent of svn's svn revert path/to/myfile.ext

    Published on

    Tags:

    Using branches with git to go back in time

    I was doing some work on a git project, on the master branch, and made 2 commits. Then I thought I should really do this on a separate branch, instead of master. But I've already committed twice to master!

    Luckily git is great.

    Firstly, you want to move to your feature branch, so do git checkout -b name-of-new-branch, tis'll create a new branch with the 2 new commits.

    Then switch to master with git checkout master, and look at the logs, with git log. You'll see your 2 new commits that you want to 'get rid of'. Look for the commit just before the 2 commits, and look at the commit: … line. Copy & paste the part after the :, it'll look something like: 09c171332d969c55bceb80db58c70a710be994e3.

    This is the id of a commit, (and tree of files), and it can act like a branch. You can checkout that 'branch' with git checkout 09c171332d969c55bceb80db58c70a710be994e3, which will give you a few lines message about how it's a "detached HEAD". Once it's checked out, give this a name, with git checkout -b master2. We are calling this new branch, master2. It's got everything in master, but without the 2 commits which want to have on the feature branch. Then just delete the old master branch (git branch -d master), and rename master2 to master (git branch -m master2 master).

    master will now have everything but without the last 2 commits, and name-of-new-branch will have master and the 2 commits, so you can continue work there.

    Obviously things get a bit more complicated if you've pushed master somewhere or if you have a lot of local changes. Best to not do it in that case.

    Published on

    Tags:

    Using scipy with Django mod_wsgi

    I had a weird bug.

    This site is a Django web application and I use Apache and mod_wsgi to serve it up in a standard django deployment setup. I wanted to use scipy, so I pip install'ed it, and then… nothing!

    My Django application appeared to just freeze. When I open a webpage, it just hangs. There is no noticable high load or CPU or memory usage on my server. There is no apache access log or error log messages. Other static apache websites I have hosted on the same machine, with the same apache instance, work fine. It's just the Django… hanging.

    This is a weird problem, and is caused by scipy being a python library that's written in C, and there's some complication with the python GIL and mos_wsgi, which means it'll hang.

    Luckily, there's a simple fix. You have to add this line to your Apache configuration file.

    WSGIApplicationGroup %{GLOBAL}
    

    And then it all works fine.

    source

    Published on

    Tags:

      ascii_sparks - simple textual sparklines in python

      Inspired by a sparks, a bash sparkline programme, I've created something exactly the same, but in python

      Installation

      It's on PyPI, so just install it with

      pip install ascii_sparks
      

      The code is also on github, feel free to contribute.

      Usage

      >>> import ascii_sparks
      >>> print ascii_sparks.sparks([0, 0, 1, 2, 1, 10])
        ????
      

      Notes

      Yes I know it's technically not ASCII, since it uses Unicode characters. However I'm using 'ascii' to refer to the style of textual drawing.

      Published on

      Tags:

        Bare Minimum setup.py for a django app

        distutils & pip is a great way to install python (incl. Django) packages. Django has lots of installable apps. This is the barebones setup.py that you need for your django app:

        Let's pretend you have an app called appname. (i.e. you started it with django-admin.py startapp appname). In the same directory as your appname folder, create a file setup.py and put this in:

        #! /usr/bin/env python
        
        from distutils.core import setup
        
        setup(name="django-appname",
              version="0.0.1",
              author="Rory McCann",
              author_email="rory@technomancy.org",
              packages=['appname'],
        )
        

        Then you'll be able to install your django app with pip install /path/to/where/you/created/the/app

        Published on

        Tags:

        Including Django template tags in your flatpages

        I use django flatpages for most of the current content on celtic knot creator, and it's great. But I want to include some django template tags in the flatpages, but they don't show up.

        The solution is to use django-qwert ( source code ) and then evalulate the template.

        Install qwert

        pip install django-qwert
        

        Then add 'qwert' to your INSTALLED_APPS setting

        Change your flatpages template

        You have to have a template for your flatpages to use, follow the instructions, except, replace your instance of `{{ flatpage.content }} with:

        {% load evaluate_tag %}
        {% evaluate flatpage.content %}
        

        That's all! Then you can use template expressions (e.g. {% … %}) from within your flatpages.

        Published on

        Tags:

        Installing Django from SVN with pip

        Installing Django with pip is quite easy, but I wanted to install Django from SVN to get the latest and greatest (and because they fixed a bug. This is how you do it:

        pip install svn+http://code.djangoproject.com/svn/django/trunk/
        

        Published on

        Tags:

        Resizing a window on the command line on Ubuntu/Unity

        If you want to resize a window on the Ubuntu command line you can use the cool tool wmctrl, and it works with the new Unity Desktop from Ubuntu. You can install wmctrl with sudo aptitude install wmctrl or click on this link

        List Windows

        List all the windows currently open with this command: wmctrl -l, you'll get some output like this:

        0x02600004 -1 lugus Desktop
        0x01800002 -1 lugus unity-2d-panel
        0x01600026 -1 lugus unity-2d-launcher
        0x04a00006  0 lugus Terminal
        0x0361a16b  0 lugus New Tab - Chromium
        

        You can see my hostname (lugus) and the titles of the windows.

        Resize windows

        To resize a window: wmctrl -r Chromium -e 0,-1,-1,480,800, that will resize any window that has `Chromium`` in the title to be 480 by 800 pixels.

        Published on

        Tags:

          HTC Desire Screen Size

          I use a HTC Desire as my primary smart phone, and want to make sure this website & Django admin works well on my phone, so I need to test it.

          So for the record, the HTC Desire is 480 pixels wide, and 800 pixels tall, i.e. 480×800

          source

          Published on

          Tags:

            Celtic Knot Creator is live!

            As should be obvious, Celtic Knot Creator is back. I intent to blog a lot about the progress of this project/business.

            About a year ago I made an online celtic knot page, that's not live now, but I'll put it back later. At the moment I'm setting some clip art, which you can find on the home page.

            Published on

            Tags:

              First Post

              Here's the first post to test

              Published on

              Tags: