20 aug 07
Tonight I (finally) upgraded my Mint install to version 2. I am convinced I did this entirely because payment is through PayPal, thus I did not have to get up from the couch to find my check card. I had a really long day at work today, most of which was taken up by orientation. Fun!
One of the things I thought would be interesting to try is to make Django models of my Mint install using Django‘s inspectdb command. Basically what this does is looks at existing database tables and creates models for them, often for the purpose of using the Django automatic admin to manage the data (create, update, delete). Now, trying this out on Mint is hardly even a test of the function and not even revolutionary, but it is nice to have my Mint data available alongside all of my other blog data. I only ran into two glitches.
First, dates are stored in UNIX timestamps, so not only do you have to convert these to Python’s datetime objects (easy with datetime.fromtimestamp()), you also have to convert for timezone if your site is hosted outside of your own like mine is. My code to take the timestamp to a readable date for the admin display is:
def get_display_date(self):
return strftime(‘%b %d, %Y, %I:%M %p’, gmtime(self.dt - 10800))
display_date = property(get_display_date)
Second, IP addresses are stored in long integer format, which doesn’t mean much to me or you. No problem, as I found a function here to convert the integer to a regular IP format. I ran into a small problem, however, where some of the numbers coming out of the database are signed. To get the correct IP, negative numbers seem to need to be added to 256. I modified the code as follows:
def numToDottedQuad(n):
“convert long int to dotted quad string”
d = 256 * 256 * 256
q = []
while d > 0:
m,n = divmod(n,d)
if m < 0:
m = 256 + m
q.append(str(m))
d = d/256
return ‘.’.join(q)
And, honestly, I haven’t looked into the signed thing enough to tell you why that is.
That’s all! I highly recommend the new Mint, especially if you don’t use it already. If you do, it’s a nice, polished upgrade.
staci martin wolfe / 21 aug 2007 / 8:53 p.m.
Hi! One of our devs came across your site… you know Django! Are you interested in any freelance/contract Django work? Or perhaps a full-time gig in Knoxville, TN? :-)
Antti Rasinen / 27 aug 2007 / 1:25 a.m.
Hello!
Have you tried using bit manipulation operators to convert the IP address to dotted quad? I’m not completely sure whether it would sidestep the unsigned vs. signed problem, but some testing would seem to suggest so.
I used (n & (0xff << 24)) >> 24 (or 16 or 8 or 0) to extract the fields.
Jeff Croft / 21 aug 2007 / 1:38 p.m.
Nice! I’ve thought about doing inspectdb on Mint several times, and just never have. I’m glad you figured out the “gotcha” so I didn’t have to. :)