Wednesday, January 06, 2010

Multiple Inserts with a Subquery | Code Spatter

Multiple Inserts with a Subquery | Code Spatter

Django Dynamic Formset

I posted Previously about this jquery plugin. I have just figured out a very plugable way to get this working in the admin with adding just one form.
Add the following to templates/admin/APP/MODEL/change_form.html and also update the MODEL in the prefix setting.
{% extends "admin/change_form.html" %}
{% load i18n admin_modify adminmedia %}
{% block extrahead %}
{{ block.super }}

  // Define this so we don't have to bother with the admin jsi18n stuff:
  function gettext(msgid) { return msgid; }

  $(function() {
      $('.inline-related tbody tr').formset({
   prefix: 'MODEL_set',
   addText: 'Add',
   deleteText: 'Delete',
  });
  });

  .add-row {
      padding-left:18px;
      background:url({% admin_media_prefix %}img/admin/icon_addlink.gif) no-repeat left center;
  }
  .delete-row {
      float:right;
      display:block;
      padding-left:18px;
      background:url({% admin_media_prefix %}img/admin/icon_deletelink.gif) no-repeat left center;
  }

{% endblock %}
Thanks Stanislaus
Django Dynamic Formset

Friday, January 01, 2010

Download SpeedFan - Access temperature sensor in your computer

Download SpeedFan - Access temperature sensor in your computer

howto:chroot_debian - DNS323Wiki

Installing debian on the DLink DNS323 NAS is really easy, just download and extact the files to the root directory and restart it.
Default password is 12345678 make sure you reset it.
Also update your /etc/apt/source.list to lenny and apt-get update (don't forget to apt-get install debian-archive-keyring)
Files
Instructions
howto:chroot_debian - DNS323Wiki

Thursday, December 24, 2009

Tuesday, December 22, 2009

Monday, December 21, 2009

Source interface with Python and urllib2 - Stack Overflow

A monkey patch to change your source interface with python Source interface with Python and urllib2
import socket
true_socket = socket.socket
def bound_socket(*a, **k):
    sock = true_socket(*a, **k)
    sock.bind((sourceIP, 0))
    return sock
socket.socket = bound_socket

Tuesday, December 15, 2009

SplitSettings - Django

No sure why they don't set this up by default but the best way to setup your media and template paths is:
DIRNAME = os.path.abspath(os.path.dirname(__file__))
DATABASE_NAME = os.path.join(DIRNAME, 'project.db')
MEDIA_ROOT = os.path.join(DIRNAME,'media')
TEMPLATE_DIRS = (
    os.path.join(DIRNAME,'templates'),
)
SplitSettings - Django - Trac

Inlines support for Django generic views

Django generic views are missing one major feature, inline forms. This class is a drop in that adds the functionality. Wad of Stuff: Inlines support for Django generic views

Saturday, December 12, 2009

Lazy choices in Django form

A cool library that django has the doesn't seem to be documented very well
from django.utils.functional import lazy

class CarSearchForm(forms.Form):  
    # lots of fields like this
    bodystyle = forms.ChoiceField(choices=lazy(bodystyle_choices, tuple)())
Lazy choices in Django form - Stack Overflow

Thursday, December 10, 2009

Python get IP of interface

Recipe 439094: get the IP address associated with a network interface (linux only)

Simple Python ping method

def ping(host):
    result = subprocess.call(["ping","-c","1",host],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
    if result == 0:
        return True
    elif result == 1:
        raise Exception('Host not found')
    elif result == 2:
        raise Exception('Ping timed out')
useage:
try:
    ping('192.168.100.100')
except Exception:
    print "Ping error"
    raise

Friday, July 17, 2009

Foreign Key in Hidden Field

In order to have a hidden foreign key your in your form class you need to specify:
class PlanForm(forms.ModelForm):    
    owner = forms.ModelChoiceField(label="",queryset=Profile.objects.all(),widget=forms.HiddenInput())

Class definition order between two related classes

I ran into an issue where I wanted to have a foreign key to one class then over ride the save in the other class to update the current class. It would through and error because one of the classes was not defined. I finally found this post Class definition order between two related classes and see that you can but the models name in quotes in the foreign key. example:
class Foo(models.Model):
    bar = models.ForeignKey("Bar")
    name = models.CharField(max_length=100)

class Bar(models.Model):
    name = models.CharField(max_length=100)
    def save(self):
        foos = Foo.objects.filter(item=self)         
        foos.update(name=self.name)
        super(Bar, self).save()