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
Rambling about Django, Python, Ansible and sometimes PHP and Wordpress. I enjoy talking about all technologies, and offer help when I can.
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
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
Sunday, December 13, 2009
How to use *args and **kwargs in Python
A good example of kwargs and args
How to use *args and **kwargs in Python
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
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
Wednesday, December 09, 2009
Django/Eclipse Debug
This is a sweet setup for Django development with eclipse to use debug to run manage.py runserver
YouTube - Django/Eclipse Debug
search and replace with regular expressions in eclipse
Found a great way to do dynamic search and replace in eclipse.
search and replace with regular expressions in eclipse
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()
Thursday, April 23, 2009
Django Custom Permissions ... sorta
There is no built in way to do row level permissions in Django, yet (see here)
I have found a few hacks to get around this in the admin section. This is using 1.0.2
First add a Foreign key to the model this is an example models.py
from django.contrib.auth.models import User, Group from django.db import models class MyModel(models.Model): name = models.CharField(max_lenght=100) group = models.ForeignKey(Group)Then in admin.py we need to override the queryset so that the user can only see objects that are part of his group and that he can only add objects that belong to his group
from django.contrib.auth.models import Group
from django.contrib import admin
from myproject.myapp.models import MyModel
class MyModelAdmin(admin.ModelAdmin):
def __call__(self,request,url):
self.request = request
return super(DeviceAdmin, self).__call__(request,url)
def formfield_for_dbfield(self, db_field, **kwargs):
field = super(DeviceAdmin, self).formfield_for_dbfield(db_field, **kwargs)
if not self.request.user.is_superuser and db_field.name == 'group':
my_choices = [('', '---------')]
my_choices.extend(
Group.objects.filter(
name__in=self.request.user.groups.all()
).values_list('id','name')
) # This can be one line it just doesn't fit
print my_choices
field.choices = my_choices
return field
def queryset(self, request):
qs = super(MyModelAdmin, self).queryset(request)
if request.user.is_superuser:
return qs
else:
group_qs = Group.objects.filter(name__in=request.user.groups.all())
return qs.filter(group__in=group_qs)
admin.site.register(MyModel,MyModelAdmin)
Now if you setup the admin in your settings and urls the groups for MyModel will only show up if the user belongs to that group. If the user is a super user they will see all the groups. If they user does not belong to any groups then they will not see any groups.
Note: You can probably submit a post with the ID of a group that you do not belong to and it will work, I have not figured out how to add custom validation based on the request object yet.
Here are some links that I used as a reference:
http://stackoverflow.com/questions/430592/djang-admin-charfield-as-textarea
http://www.djangosnippets.org/snippets/414/
http://code.djangoproject.com/ticket/3987#comment:32
Tuesday, March 11, 2008
Installing MySQL and PHP4 with apache1.3
Continued from previous post
#!/bin/bash cd /usr/src wget http://dev.mysql.com/get/Downloads/MySQL-5.0/mysql-5.0.51a.tar.gz/from/http://mysql.mirror.rafal.ca/ tar xzvf mysql-5.0.51a.tar.gz cd mysql-5.0.51a groupadd mysql useradd -g mysql mysql ./configure make make install cp support-files/my-small.cnf /etc/my.cnf cd /usr/local/mysql chown -R mysql . chgrp -R mysql . bin/mysql_install_db --user=mysql chown -R root . chown -R mysql var bin/mysqld_safe --user=mysql & wget http://ca3.php.net/get/php-4.4.8.tar.gz/from/ca.php.net/mirror tar zxvf php-4.4.8.tar.gz cd php-4.4.8 #assumes apache was configure as DSO ./configure --with-apxs=/usr/local/apache/bin/apxs make make installReview /usr/local/apache/conf/httpd.conf LoadModule php4_module libexec/libphp4.so should already be in the config file Add or verify Addtype application/x-httpd-php .php Addtype application/x-httpd-php-source .phps
Install Apache with modssl from source
copy and paste the following into a new file then chmod it to 755
#!/bin/bash cd /usr/src wget http://www.openssl.org/source/openssl-0.9.8g.tar.gz tar xzvf openssl-0.9.8g.tar.gz cd openssl-0.9.8g ./config read -p "If ./config finished press enter to make" make read -p "If make finished press enter to make install" make install read -p "If make install finished press enter to install apache and modssl " wget http://archive.apache.org/dist/httpd/apache_1.3.41.tar.gz tar zxvf apache_1.3.41.tar.gz wget http://www.modssl.org/source/mod_ssl-2.8.31-1.3.41.tar.gz tar xzvf mod_ssl-2.8.31-1.3.41.tar.gz cd mod_ssl-2.8.31-1.3.41 ./configure --with-apache=../apache_1.3.41 \ --with-ssl=../openssl-0.9.8g \ --prefix=/usr/local/apache \ --enable-shared=max read -p "If ./configure finished press enter to make" cd ../apache_1.3.41 make read -p "If make finished press enter to make install" make certificate make install
Thursday, January 03, 2008
Asterisk Install Script for Debian
#!/bin/bash
# Script to download and untar lastest asterisk build
# Justin Hamade v1 - March 24, 2007
# Justin Hamade v1.1 - May 28, 2007 - Added:
# 1. ability to install 1.2 or 1.4
# 2. dep check for wget, make, and gcc
# 3. install asterisk-gui option for 1.4
# ----------------------------------------------------------------------------
# Pause.
# ----------------------------------------------------------------------------
pause()
{
if test $NONINTERACTIVE; then
return 0
fi
[ $# -ne 0 ] && echo -e $* >&2
echo -e "Press [Enter] to continue...\c" >&2
read tmp
return 0
}
# ----------------------------------------------------------------------------
# Prompt user for input.
# Usage:
# prompt "Prompt"
# $CMD
# ----------------------------------------------------------------------------
prompt()
{
if test $NONINTERACTIVE; then
return 0
fi
echo -ne "$*" >&2
read CMD rest
return 0
}
# ----------------------------------------------------------------------------
# Get Yes/No
# Usage:
# getyn "\nWould you like to continue?" || return 1
# ----------------------------------------------------------------------------
getyn()
{
if test $NONINTERACTIVE; then
return 0
fi
while prompt "$* (y/n) "
do case $CMD in
[yY]) return 0
;;
[nN]) return 1
;;
*) echo -e "\nPlease answer y or n" >&2
;;
esac
done
}
# ----------------------------------------------------------------------------
# Get Version
# ----------------------------------------------------------------------------
getversion()
{
if test $NONINTERACTIVE; then
return 0
fi
while prompt "Enter $* minor version number (for 1.$BRANCH.0 enter 0 for 1.$BRANCH.1.1 enter 1.1): "
do case $CMD in
[0-9]|[1-2][0-9]|[0-9].[0-9]|[1-2][0-9].[0-9])
return 0
;;
*) echo -e "Please answer a number from 0 to 29" >&2
;;
esac
done
}
getbranch()
{
if test $NONINTERACTIVE; then
return 0
fi
while prompt "Enter asterisk branch number (for 1.2 enter 2 for 1.4 enter 4): "
do case $CMD in
[2]|[4])
return 0
;;
*) echo -e "Please enter 2 or 4" >&2
;;
esac
done
}
install_libpri ()
{
getyn "Install libpri?" || return 1
LIBPRIVERSION=0
getversion "libpri"
LIBPRIVERSION=$CMD
cd $SRCDIR
wget ftp://ftp.digium.com/pub/libpri/libpri-1.$BRANCH.$LIBPRIVERSION.tar.gz
tar zxvf libpri-1.$BRANCH.$LIBPRIVERSION.tar.gz
rm libpri-1.$BRANCH.$LIBPRIVERSION.tar.gz
cd $SRCDIR/libpri-1.$BRANCH.$LIBPRIVERSION
make && make install
cd $SRCDIR
}
install_zaptel ()
{
getyn "Install zaptel?" || return 1
ZAPTELVERSION=1
getversion "zaptel"
ZAPTELVERSION=$CMD
cd $SRCDIR
wget ftp://ftp.digium.com/pub/zaptel/zaptel-1.$BRANCH.$ZAPTELVERSION.tar.gz
tar zxvf zaptel-1.$BRANCH.$ZAPTELVERSION.tar.gz
rm zaptel-1.$BRANCH.$ZAPTELVERSION.tar.gz
ln -s $SRCDIR/zaptel-1.$BRANCH.$ZAPTELVERSION /usr/src/zaptel
cd $SRCDIR/zaptel-1.$BRANCH.$ZAPTELVERSION
if [ "$BRANCH" = "4" ]; then
./configure && make menuselect
fi
make && make install
cd $SRCDIR
}
install_asterisk ()
{
getyn "Install asterisk?" || return 1
ASTERISKVERSION=2
getversion "asterisk"
ASTERISKVERSION=$CMD
cd $SRCDIR
wget ftp://ftp.digium.com/pub/asterisk/asterisk-1.$BRANCH.$ASTERISKVERSION.tar.gz
tar zxvf asterisk-1.$BRANCH.$ASTERISKVERSION.tar.gz
rm asterisk-1.$BRANCH.$ASTERISKVERSION.tar.gz
cd $SRCDIR/asterisk-1.$BRANCH.$ASTERISKVERSION
if [ "$BRANCH" = "4" ]; then
./configure && make menuselect
fi
make && make install
cd $SRCDIR
}
install_addons ()
{
getyn "Install asterisk-addons?" || return 1
ADDONSVERSION=0
getversion "addons"
ADDONSVERSION=$CMD
cd $SRCDIR
wget ftp://ftp.digium.com/pub/asterisk/asterisk-addons-1.$BRANCH.$ADDONSVERSION.tar.gz
tar zxvf asterisk-addons-1.$BRANCH.$ADDONSVERSION.tar.gz
rm asterisk-addons-1.$BRANCH.$ADDONSVERSION.tar.gz
cd $SRCDIR/asterisk-addons-1.$BRANCH.$ADDONSVERSION
if [ "$BRANCH" = "4" ]; then
./configure && make menuselect
fi
make && make install
cd $SRCDIR
}
install_gui ()
{
getyn "Install asterisk-gui?" || return 1
cd $SRCDIR
svn checkout http://svn.digium.com/svn/asterisk-gui/trunk asterisk-gui
cd $SRCDIR/asterisk-gui
./configure && make && make install && make samples
cd $SRCDIR
}
install_sounds ()
{
getyn "Install asterisk-sounds?" || return 1
SOUNDSVERSION=0
getversion "sounds"
SOUNDSVERSION=$CMD
cd $SRCDIR
wget ftp://ftp.digium.com/pub/asterisk/asterisk-sounds-1.$BRANCH.$ADDONSVERSION.tar.gz
tar zxvf asterisk-sounds-1.$BRANCH.$SOUNDSVERSION.tar.gz
rm asterisk-sounds-1.$BRANCH.$SOUNDSVERSION.tar.gz
cd $SRCDIR/asterisk-sounds-1.$BRANCH.$SOUNDSVERSION
make install
cd $SRCDIR
}
install_wanpipe ()
{
getyn "Install sangoma wanpipe?" || return 1
prompt "Enter wanpipe version (ie 2.3.4-9 or 3.1.0)"
WANPIPEVERSION=$CMD
cd $SRCDIR
wget ftp://ftp.sangoma.com/linux/current_wanpipe/wanpipe-$WANPIPEVERSION.tgz
tar zxvf wanpipe-$WANPIPEVERSION.tgz
rm wanpipe-$WANPIPEVERSION.tgz
cd $SRCDIR/wanpipe-$WANPIPEVERSION
./Setup install
cd $SRCDIR
}
banner ()
{
cat << ENDOFTEXT
This script will download and install Asterisk 1.2 or 1.4 and its required packages including Sangoma drivers.
ENDOFTEXT
pause
}
checkwget ()
{
echo "Checking wget ...";
if [ ! -x $WGET ]; then
getyn "wget not installed run, apt-get install wget?" || return 1
apt-get install wget
fi
echo "success"
}
checkmake ()
{
echo "Checking make ...";
if [ ! -x $MAKE ]; then
getyn "make not installed, run apt-get install make?" || return 1
apt-get install make
fi
echo "success"
}
checkgcc ()
{
echo "Checking gcc ...";
if [ ! -x $GCC ]; then
getyn "gcc not installed, run apt-get install gcc?" || return 1
apt-get install gcc
fi
echo "success"
}
banner
WGET=/usr/bin/wget
checkwget
MAKE=/usr/bin/make
checkmake
GCC=/usr/bin/gcc
checkgcc
getbranch
BRANCH=$CMD
SRCDIR="/usr/src/asterisk-1.$BRANCH"
if [ -d $SRCDIR ]; then
echo "directory exsists"
else
mkdir $SRCDIR
fi
cd $SRCDIR
install_libpri
install_zaptel
install_asterisk
install_addons
if [ "$BRANCH" = "2" ]; then
install_sounds
fi
if [ "$BRANCH" = "4" ]; then
install_gui
fi
install_wanpipe
Friday, December 07, 2007
My stdexten macro
I always forget this so I figure I will post it and update it when I improve it.
[macro-stdexten]
;ARG1 = Device(s) to ring (Default SIP/${MACRO_EXTEN}
;ARG2 = Voicemail Box to go to (Default ${MACRO_EXTEN}
;ARG3 = Timeout in seconds (divide be 5 for number of rings) default 20
exten => s,1,NoOp
exten => s,n,Set(DIALSTRING=${IF($[ "${ARG1}" = "" ]?SIP/${MACRO_EXTEN}:${ARG1})})
exten => s,n,Set(VMAILBOX=${IF($[ "${ARG2}" = "" ]?${MACRO_EXTEN}:${ARG2})})
exten => s,n,Set(TIMEOUT=${IF($[ "${ARG3}" = "" ]?20:${ARG3})})
exten => s,n,Dial(${DIALSTRING},${TIMEOUT},twk)
exten => s,n,Goto(s-${DIALSTATUS},1)
exten => s-,1,Goto(s-BUSY,1)
exten => _s-.,1,Goto(s-BUSY,1)
exten => s-CONGESTION,1,Goto(s-BUSY,1)
exten => s-BUSY,1,Voicemail(${VMAILBOX}@default,b)
exten => s-BUSY,n,Hangup()
exten => s-CHANUNAVAIL,1,Goto(s-NOANSWER,1)
exten => s-NOANSWER,1,Voicemail(${VMAILBOX}@default,u)
exten => s-NOANSWER,n,Hangup
Wednesday, November 28, 2007
Document Design for Search Engine Optimization
Saturday, October 20, 2007
Mythtv Distro Evaluations
It is a tough time for Mythtv distros right now with Zap2it cancelling their services is August. There are 4 major mythtv distro communities right now, knoppmyth, mythdora, mythbuntn and linuxmce. Today I tried all but knoppmyth and here is what I have found.
LinuxMCE
I first tried LinuxMCE as the demo video they have on their site shows some amazing features. They do have an update for the new Schedules direct service but the link to the script was down. The thing that I found about LinuxMCE was that because it is a bunch of custom changes on top of Kubuntu Feisty, updating myth from the Kubuntu repository would break it. This is a cool system but is a bit behind when on changes.
Mythdora
Mythdora acutally looked good, they have some myth specific questions at install. The downside I found was the myth version was still the older one and there was no Schedules direct support, also the links for the update at the atrpms site was down as well.
Mythbuntu
Mythbuntu is a supported ubuntu distro and is kept almost as up to date as ubuntu is. Right now they have a 7.10 release candidate and the 7.10 version has only been released for a few days at this point. The good thing is it has the newest version of myth and the Schedules Direct is supported right out of the box, no updates needs.
Ubuntu again comes through leading the pack with the most up to date distro in mythbuntu.
If anyone has comments on these distros or any other I would love to hear them.
Thursday, April 19, 2007
Polycom Presence and SIP 500 errors
I finally got someone at Polycom support that knew what they were talking about and were very helpful. I have been getting a lot of these errors from the Polycoms on my asterisk servers Incoming call: Got SIP response 500 "Internal Server Error" back from 192.168.0.100
The problem was the transport was set to "DNSnaptr" by default. So if you set it to "TCPpreferred" it should fix any issues you are having with presence and also any sip 500 errors.
EDIT*
I am still getting sip 500 errors if I restart asterisk and not the polycom phones. On a side note for asterisk 1.4 make sure call-limit is set to something. I set it to 8.
Thursday, March 22, 2007
Provisioning Polycom phones
Provisioning Polycom SIP phones can be a pain. There are some good examples on www.voip-info.org but I think a explanation would be a good idea.
For an internal LAN setup TFTP is easiest, so I have everything setup as so:
/tftpboot
/tftpboot/contacts
/tftpboot/logs
/tftpboot/overrides
The important thing is DO NOT touch sip.cfg, phone1.cfg and 000000000000.cfg, use it as reference only. Why you ask, so when new firmware comes out you just drop in the new file.
So where do you make your changes then????
- MAC.cfg
- EXTEN.cfg
- 00000000000-directory.cfg
- contacts/MAC-directory.cfg
- /tftpboot/contacts/00000000000-directory.xml does not get used it gets pulled from the /tftpboot/000000000000-directory.xml file (as of bootrom v3.2.2)
- The dhcp option to set the tftp server is option tftp-sever-name "tftp://192.168.41.1"
Subscribe to:
Posts (Atom)