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/fr                                                                                                 om/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 install
Review /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

Digital document design has become much more then just aesthetics and it is now important that a document can be read by "robots" as well a people. Robots, or crawlers or spiders, are another name for indexing services that search engines use such as google. As you are well aware, search engines are the way people find most things on the web today. Most people assume that search engines use hidden meta tags to index web pages for searching. This may have been the case in the early days of the web, and is only partially true for a few search engines today. Search engines are now programmed to look at the text inside html tags that are used to design a web page. Weight is given to the words depending on which tag they are in. This is how Search Engine Optimization, or SEO, relates to document design. I will discuss the tags that are important to making your site search engine friendly, and some ways to keep the design you want. First off is the title tag, this should be descriptive as to who you are and what information you are presenting in your page. The title tag should also stay short and not be greater the 60 - 70 characters. Keep in mind that the title tag is not only something that search engines use to find keywords to your site, but it is also what the user will see in their search results. Heading tags are also regarded as important content, as they are meant to be headings on your tags. Designers often use images as headings, but these will not only require descriptive alt attributes, but also are weighted very low by search engine crawlers. Cascading style sheets (CSS) can also be used to design headings, but be when using CSS make sure to apply your CSS to the heading tag and not to another block element tag, like span or div. Make sure to use descriptive keywords in your ALT attribute for your image tags. This will help search engines that index images as the image file name is not always relative to the picture. The alt tag is included as body text by most search engines, but as mentioned previously is weighted low. Meta tags are still used by some search engines but are weighted very low due to over use. For the Meat Description tag, it is best to keep it under 100 characters. The meta tag is also important because it is displayed in the search results. For the Meta Keyword tag, it should be kept under 250 keywords, and should not contain any words that are not in the document. As mentioned previously search engine crawlers can not index images, this is something that many designers have problems with for a few reasons. First text is difficult to control, even when using CSS to format it. The user must have that font installed, and the user can increase and decrease the font size in their browser easily. Images solve these problems for a designer because they can not be changed, but images will hinder its search engine optimization. The last and probably most important part of search engine optimization are links. I will referrer to two different types of links, internal and external. Internal links are things like menus and site maps. It is important to have at least one static link to every page on your site that you want indexed. This is so the search engines know how to get to every page when they spider or crawl your site. External links referrer to other sites that link back to yours. This is another way that search engines rank your site. This is unrelated to design so there is no need to go into any more detail about it. As with everything, there are people out there that abuse the system, so search engines have put measures into place to not rank these types of sites highly. Some things I have already mentioned like number of characters in title and meta tags. One additional thing to be careful of is to keep the number of links on one page to under 100, more than 100 links we be thought of as spam. For a large site map it would be a good idea to split it up into multiple pages. Another thing to be careful of is using the same words an excessive amount on a single page. This is a common practice for people trying to trick search engines. Currently there is a battle between designers and search engine optimization people. Both are important elements of marketing your site and your business on the web, but they have contradicting requirements. As discussed many design needs are not SEO friendly, and many SEO needs are not design friendly. Make sure you find a good middle ground when designing your site. References: How can I create a Google-friendly site? Webmaster Guidelines Search Engine Optimization Standards and Spam Discussion Google SEO Basics for Beginners Media College.com SEO Basics Google Rankings Basic SEO advice SEO 101 - Basic Optimization Techniques

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.

\