Something is ticking.

Latest

Missing objects in Django admin list view (change list)

This was a good one…

The object count at the bottom of the list view was changing accordingly but no objects were being display in the list. I had recently made some changes to the model so that kept me busy for a while. But ultimately it turned out to be a broken foreign key on a distantly related object. Also, I was using fixtures so this allowed a broken FK to get into the DB. Normally Django will detect this situation but apparently fixtures can lead to this situation.

Moral of the story – If you’re experiencing this problem – you have a broken FK somewhere in your project.

Django: TemplateSyntaxError at /admin/foo/bar/30/ Caught an exception while rendering: ‘ascii’ codec can’t encode character u’\u201d’ in position 3: ordinal not in range(128)

I recently ran into this error with an old Django site.
TemplateSyntaxError at /admin/foo/bar/30/
Caught an exception while rendering: 'ascii' codec can't encode character u'\u201d' in position 3: ordinal not in range(128)
Make sure that you're defining __unicode__ methods on your models and not __str__!
This fixed the problem for me.

Facebook font size reduced smaller

Facebook font sized is now tiny!

Facebook reduced its font size!

Facebook decided to reduce the size of their font just now. It’s really small, tiny even. It hurts my eyes.
Not only does this decrease readability, it also is pissing off a lot of people. The text is so small that I want to hurt someone.
Could this be related to election day? Possibly. But I’m just guessing.

Dear Facebook,
Please restore the font back to the normal size. Oh, and keep my status on the same line as my name. It looks horrible as it is now.
Thanks!

UPDATE:

To temporarily fix this annoyance you can increase the size of the facebook font by pressing Ctrl-+ (Control + Plus). This will affect all fonts on the page but it’s better than squinting!

Also, for those with a browser with a Javascript Console (such as Chrome users) you can use the following code to increase the font size:

for(i = 0, e = document.getElementsByClassName('messageBody'); i < e.length; i++) { e[i].style.fontSize = "12px"; }</span>

Chrome users can open the JavaScript Console with Ctrl+Shift+J. Unfortunately, this isn’t a permanent fix.

Python Threads and Sockets Example – DNS Server and Client

I’m too lazy to make a decent write-up for this code so I’m just going to post it. More information about threading and sockets can be found in the official documentation:
http://docs.python.org/library/threading.html
http://docs.python.org/library/socket.html
Code after the break
Read the rest of this page »

Debian Django package missing jQuery … prepopulate_fields fails silently

Apparently the Django Debian package has had some trouble keeping jQuery in the right place for the admin media. Django now uses the official jQuery package (libjs-jquery) and a symlink from the admin media directory.
So if you’re having trouble getting prepopulate_fields or other Javascript augmented admin widgets to work, check your jQuery symlinks in /usr/share/pyshared/django/contrib/admin/media/js/ and make sure libjs-jquery is installed… APT somehow missed that when I upgraded to Django 1.2.

Django: Reset app database with sqlclear after changes to model

Early in the development process application models change all the time. I found myself needing a quick way to drop tables for individual apps. This command will do exactly that.
Read the rest of this page »

ISBN Checksum Validation in Javascript

ISBN’s have a built in error checking digit. This checksum can be easily verified with a simple algorithm, which can be handy for client-side input validation.
This code will validate ISBN-10 and ISBN-13.

See code… Read the rest of this page »

Installing Go in Ubuntu in 4 Easy Steps

  1. Setup the environment

    Create a binaries directory. The Go build process expects this directory to exist, otherwise you must set $GOBIN accordingly.
    $ mkdir ~/bin

    Add the following to ~/.bashrc

    # export some Go variables
    export GOROOT=$HOME/go
    export GOOS=linux
    # change this to amd64 for 64bit systems
    export GOARCH=386
    # $GOBIN gets set to $HOME/bin by default
    # $HOME/bin should be added to your path
    export PATH=$HOME/bin:$PATH
    
  2. Install the needed packages

    $ sudo apt-get install mercurial bison gcc libc6-dev ed make

  3. Download the Go repository with Mercurial

    $ hg clone -r release https://go.googlecode.com/hg/ $GOROOT

  4. Build and install Go

    $ cd $GOROOT/src
    $ ./all.bash

That’s it. For more reading check out the official Go language website at golang.org

C# Getting First Active MAC Address

Just wanted to post some basic code for retrieving the first active MAC address from a C# program.

First we need just one namespace:

using System.Net.NetworkInformation;

And then you can just use this static method.

public static string GetFirstActiveMacAddress()
 {
 NetworkInterface[] allNetworkAdapters = NetworkInterface.GetAllNetworkInterfaces();
 foreach (NetworkInterface currentAdapter in allNetworkAdapters)
 {
 if (
 currentAdapter.OperationalStatus == OperationalStatus.Up &&
 currentAdapter.NetworkInterfaceType != NetworkInterfaceType.Loopback
 )
 {
 PhysicalAddress currentAddress = currentAdapter.GetPhysicalAddress();
 string macAddressToReturn = string.Empty;
 byte[] addressByes = currentAddress.GetAddressBytes();
 for (int i = 0; i < addressByes.Length; i++)
 {
 macAddressToReturn += addressByes[i].ToString("X2");

 if (i != addressByes.Length - 1)
 {
 macAddressToReturn += "-";
 }
 }
 return macAddressToReturn;
 }
 }
 return string.Empty; // they're all down, return empty
 }

Thanks for reading through, hopefully this code will help you in some way.

C# Single Instance Program Like a Pro

Hello again, I recently wrote single instance code… well,  I wrote it twice. Once for work, and once to help you in this Mutex infested area of application development. It’s pretty simple code, there are three parts that you need to add to your Program.cs file.

First we need some namespaces:


using System.Runtime.InteropServices;

using System.Diagnostics;

And then we need some native win32api calls:


[DllImport("user32.dll")]
 static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

 public enum WindowShowStyle
 {
 Hide = 0, ShowNormal, ShowMinimized, ShowMaximized,
 Maximize, ShowNormalNoActivate, Show, Minimize,
 ShowMinNoActivate, ShowNoActivate, Restore,
 ShowDefault, ForceMinimized
 }

 [DllImport("user32.dll")]
 [return: MarshalAs(UnmanagedType.Bool)]
 static extern bool SetForegroundWindow(IntPtr hWnd);

And then we need some new code in our Main() method. This should go at the top of the method. This method is pretty cool because it will allow you to use this in an ClickOnce environment and use Application.Restart() after downloading a new update. It’s not messy like a Mutex is.


Process thisInstance = Process.GetCurrentProcess();
 Process[] allInstances = Process.GetProcessesByName(thisInstance.ProcessName);
 if (allInstances != null && allInstances.Length > 1)
 {
 foreach (Process currentInstance in allInstances)
 {
 if (currentInstance.Id == thisInstance.Id)
 {
 continue;
 }
 else if (currentInstance.MainWindowHandle.ToInt32() == 0 && currentInstance.Id != thisInstance.Id)
 {
 continue;
 }

 ShowWindow(currentInstance.MainWindowHandle, (int)WindowShowStyle.Restore);
 SetForegroundWindow(currentInstance.MainWindowHandle);
 return;
 }
 }

Thanks for reading through, I hope you find this code to be helpful. I enjoyed writing it. Until next time.

Follow

Get every new post delivered to your Inbox.