2009-09-24

Calculating the keytag of a DNSKEY in Python

In DNSEC, sometimes you want to know the "keytag" of a DNSKEY record. Here's a Python implementation that uses dnspython:

import struct

def keytag(dnskey):
"""
Given a dns.rdtypes.ANY.DNSKEY dnskey, compute and return its keytag.

For details, see RFC 2535, section 4.1.6
"""
if dnskey.algorithm == 1:
a = ord(dnskey.key[-3]) << 8
b = ord(dnskey.key[-2])
return a + b
else:
header = struct.pack("!HBB", dnskey.flags, dnskey.protocol, dnskey.algorithm)
key = header + dnskey.key
ac = 0
for i, value in enumerate(ord(x) for x in key):
if i % 2:
ac += value
else:
ac += (value << 8)
ac += (ac >> 16) & 0xffff
return ac & 0xffff

2009-09-15

Using Django's Syncdb outside of Django

I'm working on another project that uses Django's ORM that isn't (yet) a web applicaiton. There's no settings.py file as the application has its own configuration system build on ConfigObj and optparase. But, before you can use the app, you need to create the tables that Django's ORM needs for your Models. If you app lives in a python package named 'fooapp', then put your Django models in fooapp.models and try this code:

from django.conf import settings
from django.core.management.commands import syncdb

opts, args = build_configuration()
my_app = 'fooapp'
settings.configure(
DATABASE_ENGINE=opts['db_engine'],
DATABASE_NAME=opts['db_name'],
DATABASE_USER=opts['db_user'],
DATABASE_PASSWORD=opts['db_pass'],
DATABASE_HOST=opts['db_host'],
DATABASE_PORT=opts['db_port'],
INSTALLED_APPS=(my_app,)
)
cmd = syncdb.Command()
cmd.handle_noargs()



And do let me know if that works. Thanks!

2009-07-26

Running Django with wsgiref that also serves Admin media

I wanted to run a Django app using Python's built-in wsgiref module. It also serves Django admin media. Consider this a stepping stone to running your Django app using CherryPy.
from wsgiref.simple_server import make_server
from django.core.handlers.wsgi import WSGIHandler
from django.core.servers.basehttp import AdminMediaHandler
httpd = make_server('', 8000, AdminMediaHandler(WSGIHandler()))
httpd.serve_forever()

2009-03-19

Rhymes with Pony

Yesterday I pushed out Owney which does USPS shipment tracking in Python. It uses a bit of Django as well. It's got rough edges, so please watch your fingers! I intend to polish it as time permits. I do want to make it a re-usable Django app, but I need some advice on how to make that work.

The original Owney was a dog with a very interesting life.

Enjoy!

2009-01-17

Snakes in the bassinet (Baby's first open source project)

I pushed out my first open source project this week. It's a Python interface to HelpSpot's web services API. HelpSpot is a help desk application that I use, like and happily recommend.

You can find my project python-helpspot on GitHub. I have another open source thing in the works too. That one is going to be a doggy and it'll have a touch of the django.

2009-01-08

Missteps in Django - Part 3

This one will make it obvious that the missteps are my own mistakes, not problems inherit in Django. I'm not terribly embarrassed to look this dumb. Most days, I'm proud of my density. On with the show!

I guess this is another template issue. Oh well. I wanted to set the CSS class on an HTML tag based on some dynamic data. In this case, the data was the status field on my Shipment model:


class Shipment(models.Model):
description = models.TextField()
created = models.DateTimeField(auto_now_add=True);
status = models.CharField(max_length=64, default="new",
choices=STATUS_CHOICES)

I'm omitting STATUS_CHOICES because its not important to my point. I wanted each displayed shipment to have a different background color. My initial idea was to try something like this:

class={% ifequal thing.status "new" %}"new"{% elifequal thing.status "exception" %}"exception"{% endelifequal %}...

Of course, Django doesn't have an elif construct in its templates. Doesn't matter because my idea was stupid and eventually I figured that I could do this:

class="status_{{ shipment.status }}"

and be done with it. I hope I had a good laugh at myself.

2008-12-22

Missteps in Django - Part 2

My next misstep was related to using the regroup tag that I mentioned in the previous post.

First, let's take a look at the example model I'm working with.

class Shipment(models.Model):
description = models.TextField()
created = models.DateTimeField(auto_now_add=True);


I wanted to display these Shipments grouped by they day they had been created and I wanted that day formated like "2008-12-22". Since the 'created' field of the Shipment model is a datetime.datetime instance which, when viewed as a string, looks like "2008-12-23 19:16:06.700114". That was a bit too verbose and besides, I wanted shipments grouped by day, not by microsecond!

My solution was to a add a property (of course!) to my model and see if I could use that in the regroup tag. Here's what that looked like:


class Shipment(models.Model):
description = models.TextField()
created = models.DateTimeField(auto_now_add=True);

def _get_ship_date(self):
return self.created.date()

ship_date = property(_get_ship_date)

Then in my regroup tag, I could do this:

<ul>
{% regroup shipments by ship_date as shipments_by_date %}
{% for ship_date in shipments_by_date %}
<li>Shipments for {{ ship_date.grouper }} </li>
<ol>
{% for shipment in ship_date.list %}
<li> {{ shipment.description }} </li>
{% endfor %}
</ol>
{% endfor %}
</ul>


Hey, that worked, but this was another of my newbie missteps because there was an aspect to the regroup tag that I had overlooked (this series should be called RTFM maybe?). You can apply a filter to the attribute you are regrouping on like so:


{% regroup ship_list by created|date:"Y-n-d" as shipments_by_date %}

And you're done.