Date formats and Django forms

Date formats are something that US dominance remains unable to overcome. Whilst all software assumes a format of mm/dd/yyyy, all other nations of the world want to change that to their local preference.

Django is no exception.

Here’s the magic incantation to make Django 1.1 output and input dates in an Australian format.


DATE_FORMAT = '%d/%m/%Y'
...
class ProfileForm(forms.ModelForm):
    ...
    date_of_birth = forms.DateField()
    date_of_birth.widget = forms.DateInput(format=DATE_FORMAT)
    date_of_birth.input_formats = (DATE_FORMAT,)
Posted in geek | Tagged | Leave a comment

domainnamegroup.com.au is a scam

I got this letter in the mail recently. At first glance I thought, “oh, does my programmerforhire.com.au domain need renewal already?”…

I have a sneaking suspicion that’s exactly the reaction this item was intended to provoke…
don’t get stung by domain name scams

Posted in geek | 3 Comments

My errors always return to jQuery’s “success” function?

How do I tell my application that the query was unsuccessful when jQuery insists on always running the “success” function when it returns??

When we make an ajax call with jQuery the server’s response is consumed by the “success” function we’ve provided. “success”, in this case, refers to whether the server responded according to the HTTP protocol. “success”, in this case, does not refer to whether the application was happy to do what we were asking for, or not. If the user tried to do something silly don’t expect that putting a function in the “error” callback will deal with it. “error” and “timeout” are only useful if you want to know when the ajax request couldn’t converse with the server at all.

Q: So how do I tell my application if the call was successful or not?

A: Return a JSON response.

For example have your server return:

{'success': false; "message": "Nah. I'm not in the mood", 'id': 292} or
{'success': true; "message": "Okey dokey!", 'id': 292}

If, in your ajax call, you set the “dataType” to “json” jQuery automatically parses the json and gives you a javascript object all ready to go. Man, this was so wonderful, I thought I should blog it.

This little sample throws up a dialog to present ‘data.message’, then uses ‘data.success’ to decide if the action was successful or not. If it was successful we could use data.id for something useful. But in this case I fade out an element identified with the id passed to the original manage function which is somehow still in scope.


function manage(action, id){
    $.ajax({
        type: "POST",
        data: "action=" + action + "&id=" + id,
        url: "/membership/xhr/manage/",
        dataType: "json",
        success: function(data){
            $("<div></div>").html(data.message).dialog();
                modal:true,
                buttons: {
                    "Ok": function(){
                        $(this).dialog("close");
                        if(data.success) $('#membership_'+ id).fadeOut();
                    }
                }
            });
        }
    });
}

I’d give you a working demo, but that would be work ;-)

Posted in geek | Tagged | Leave a comment

Age in years with python?

Given someone’s date of birth how do I work out their age? This is actually less trivial than first appears because:

  • the person’s age doesn’t increment until their birthday passes on the calendar
  • that day is not always the same period of time after midnight on the 1st of January

This solution works by asking if today’s month is before their birthday, and if their birthday is this month whether today’s date is before the birth day.

This works even if they are born on the 29th of Feb.


def age(dob):
    import datetime
    today = datetime.date.today()

    if today.month < dob.month or \
       (today.month == dob.month and today.day < dob.day):
          return today.year - dob.year - 1
    else:
          return today.year - dob.year

>>> import datetime
>>> datetime.date.today()
datetime.date(2009, 12, 1)
>>> age(datetime.date(2008, 11, 30))
1
>>> age(datetime.date(2008, 12, 1))
1
>>> age(datetime.date(2008, 12, 2))
0
Posted in geek | Tagged , | 2 Comments

How to convert a VMWare vmdk appliance to VirtualBox vdi

There are loads of ready-built appliances for VMWare in .vmdk format but I use VirtualBox and there is never a release in .vdi form to be found. I’ve heard that virtualbox will run the vmware images… but is that true?

Yes you can run vmware drives in virtualbox. Here’s how…

Posted in geek | Tagged | Leave a comment

How to push wordpress from testing to production

I use a testing server for my site, and I use wordpress. I create and update content on testing, then deploy it to production. But the wordpress import procedure won’t update an existing post so…

How do I deploy my wordpress content from Testing to Production?

Posted in geek | Tagged | 1 Comment

How to use a testing server with wordpress

I like to mirror and build my wordpress site on a testing server and get it all ‘just right’ then push the changes to production when I’m somewhat ready to release. This a standard practice in the IT industry and gives us the advantages of

  • the site is never in a half-baked or broken state
  • development is super responsive since the server is on our local network
  • the site statistics don’t get polluted by the traffic of development
  • we can try new things without worrying that we’ve broken the website
  • we have a backup which is active and recent

But, because wordpress is not built with any anticipation of this concept, some problems arise

  • How to pull fresh comments and posts from production to testing?
  • How to push new and updated posts from testing to production?
  • How to handle images and media attached to posts?
  • How to handle site configuration options?
  • How to handle Theme changes?

Today we’ll tackle the easiest of these problems…

How to pull wordpress comments and posts from production to testing?

Posted in geek | Tagged | Leave a comment

Svn global-ignore

I want subversion to always ignore compiled files. There is lots on the internet about using property svn:ignore but nothing about setting a global-ignore.

Where the heck is this danged config file?

On FreeBSD I found it at “~/.subversion/config”.

Yes, in my home directory… so “global” means just global for one user which seems kinda pointless. I guess that’s why no-one uses it. I gave it a bit of this action which works.


[miscellany]
global-ignores = *.pyc

Posted in geek | Tagged | Leave a comment

How to add a new user to SVN on Apache

Problem:

I’ve started working on an existing project and needed to add myself as a user to the repository. It proved kinda tricky. First I couldn’t get Tortoise to authenticate, then I couldn’t add myself as a user, and then I couldn’t work out how to put a password on my svn user.

(“Here’s how I fixed all that…”)

Posted in geek | Tagged | Leave a comment

johnmee.com gets a facelift

Well here it is, the new look johnmee.com

I’m not happy with it yet but if I kept on working on it till I was happy, then it would never be released. It’s one of those idealist versus pragmatist tensions that every software project must endure.

Things I am happy about are:

  • isolated development environment – I’ve worked out how to run wordpress on a development server and tweak away to my hearts content without having to do any of it live and in production. This makes my idealist very happy and is worth a post of its own very soon.
  • slicker look? – well the jury is out on this but i’ve switched to this inove theme which is darker and looks a lot more web2.0 – whatever that is. It has some very nice CSS which the idealist in me likes. More on this later.
  • no ‘pages’, all ‘posts’ – getting to know wordpress I’ve been torn to know whether I should create “pages” on subjects which will stand (or move with) the test of time; or “posts” which record the moment in time. I’ve made the executive decision that pretty much everything I write will be a moment in time. Thus I have ditched the few “pages”, and moved everything into posts. It’s my blog so if I want to go back and edit recent posts so that they make more sense, then I blooming well can :-)
  • tags and categories consolidation – I’ve gone through every post and tried to consolidate the tags and categories into some sort of uniformity
  • Travel letters imported – all my travel letters from years ago were sitting separate from the blog and in their original web format. I’ve let go of the past and moved them all into the blog post format for easier and more uniform access.
  • custom category page – I’m learning heaps about wordpress. So much that I’ve probably got enough material on wordpress tips I’ll hopefully be bringing you a series on that. Important to me was to present the Travel posts in a custom way, which meant creating a special category template. You can click on the travel page and see what i’m talking about – it’s not the usual archive of posts.

There’s still lots and lots of stuff to do. Such as:

  • images – no one was finding the photo gallery before. I need to move all my photos into the blog and attach them to the travel posts
  • front page is too busy – as Mick noted, the new look is far too “busy” compared to the old one. He liked the simplicity and I’m inclined to appreciate the criticism. So very high on the agenda is to tone down the home page. Perhaps just show the last two posts in full.
  • context sensitive sidebars – since I’ve worked out how to ‘program’ wordpress I know I can do something with the sidebars. I want the sidebars to be different between the archive pages, the single pages, and perhaps different again by category. I’m wondering if images down the sidebar would work in the travel pages.
  • work on jamabelle – i know there are a lot more letters from james which are even more deep and valuable as they chart his global soul seaching following the break-up of james and isabelle. I must find the one where he hangs around with an Indian swami for a time. I think i should move these into the blog so they present better, but am not comfortable with it since they’re not my works
  • trim the database – the wordpress database likes to keep an archive of all the previous edits and dead categories and links and other trash. Only a geek would care as it has zero impact on the presentation, but I want to clear some of this cruft. Just makes me feel like I’ve mopped and vacuumed or something.

These are the new and old themes side by side…

Do you prefer the old or new? Why?

Posted in geek | Leave a comment