Sunday, 3 November 2013

Django - Get Model Field's Name

for field in OrderAddress._meta.fields:
  field.name

user = User.objects.latest('pk')
for field in user._meta.fields:
  field.name, field.value_to_string(user)
Reference: http://stackoverflow.com/questions/5358800/django-listing-model-field-names-and-values-in-template

Monday, 21 October 2013

Django - Loop Form Cleaned Data

for key, value in form.cleaned_data.iteritems():
    print key, value
Python 3
for key, value in form.cleaned_data.items():
    print (key, value)
Reference:
http://stackoverflow.com/questions/5904969/python-how-to-print-a-dictionarys-key

Django - Foreign Table Relational Mapping Queryset

def get_child_attribute_list(self, element):
    return Attribute.objects.filter(Q(element=element), Q(productattribute__product__parent=self)|Q(productattribute__product__root=self))

Monday, 14 October 2013

Django - Link Sub-instance to Inheritance Model

def save_to_finishing(self):
        finishing_product = FinishingProduct(product_ptr_id=self.pk)
        finishing_product.__dict__.update(self.__dict__)
        finishing_product.save()

Reference:
http://stackoverflow.com/questions/4064808/django-model-inheritance-create-sub-instance-of-existing-instance-downcast

Thursday, 12 September 2013

Django - Site Matching Query Error

Site matching query does not exist. Lookup parameters were {'pk': 1}
Open Project Shell
python manage.py shell

Create Site Object
from django.contrib.sites.models import Site
Site.objects.create(pk=1, domain='example.com', name='example')
Best Practise
python manage.py syncdb --noinput
python manage.py migrate
python mange.py createsuperuser

Sunday, 8 September 2013

Python - Fix Non-ASCII Character in File

Encode in ANSI


Additional Headers (On Top of code)
#! /usr/bin/env python
# -*- coding: utf-8 -*-

EXTRA: For Windows (Optional)

Python - Windows UTF-8 Cmd Input

Create win32_unicode_argv.py
Import
import sys
import win32_unicode_argv 
Reference:
http://code.activestate.com/recipes/572200/
http://stackoverflow.com/a/846931

Sunday, 1 September 2013

Python - Sip2sip Outgoing call with Pjsua

Error code
sip:ID@sip2sip.info
Failed to send Request msg INVITE/cseq=19833 err=171060 (Unsupported transport (PJSIP_EUNSUPTRANSPORT))
Call with sip:ID@sip2sip.info is DISCONNCTD last code = 503 (Unsupported t
ransport (PJSIP_EUNSUPTRANSPORT)) 
Add TLS support
sip:ID@sip2sip.info;transport=tls

Reference:

Friday, 30 August 2013

Python - Sip2sip with Pjsua

Rebuild Python Pjsua TLS Support with VS2008

config_site.h
#include <pj/config_site_sample.h>
#define PJ_HAS_SSL_SOCK 1
VC++ Header and Library Path

Build then Install
pjproject\pjsip-apps\lib\_pjsua.pyd
python pjsip-apps\src\python\setup-vc.py install

Sip2Sip.info Authentication Script
try:
    ua_cfg = pj.UAConfig()
    ua_cfg.nameserver = ['8.8.8.8', '8.8.4.4'] # Example: Google Public DNS
    lib.init(ua_cfg=ua_cfg, log_cfg=pj.LogConfig(level=4, callback=log_cb))
    transport = lib.create_transport(pj.TransportType.TLS, pj.TransportConfig(port=5060))
    lib.start()
 
    acc_cfg = pj.AccountConfig()
    acc_cfg.id = "sip:ID@sip2sip.info"
    acc_cfg.reg_uri = "sip:sip2sip.info;transport=tls"
    acc_cfg.auth_cred = [pj.AuthCred(
        realm="sip2sip.info",
        username="USERNAME",
        passwd="PASSWORD",
    )]
    acc_cb = MyAccountCallback(acc_cfg)
    acc = lib.create_account(acc_cfg, cb=acc_cb)
except Exception, err:
    print 'Error creating account:', err
References:
http://trac.pjsip.org/repos/wiki/Getting-Started/Installing-OpenSSL-Windows
http://trac.pjsip.org/repos/wiki/TLS

Thursday, 29 August 2013

Windows - VMWare Sound Audio Troubleshooting

Check Realtek Audio Manager

Ensure Both Playback and Recording Devices Working

Check VMWare player Sound Device Setting


Python - Pjsua 1.16 Build Fixes for Windows

Build Python2.7 x32 Windows 7 x64
pjsua==1.16-svn
http://svn.pjsip.org/repos/pjproject/branches/1.x/
Python2.7 <- Visual Studio 2008

VC++ Include & Library Directory (MenuBar -> Tools -> Options)



Replace python24.lib to python27.lib
py_pjsua.dsp
py_pjsua.vcproj
python_pjsua.vcproj
python_pjsua.dsp
Save as UTF-8 or Unicode (Optional)
g7221.h
Backup compiled pjsua 1.16:
pjproject\pjsip-apps\lib\_pjsua.pyd
python pjsip-apps\src\python\setup-vc.py install
https://www.dropbox.com/s/sgdrp8yxk2v6xe6/_pjsua.pyd

References: 
http://trac.pjsip.org/repos/wiki/Getting-Started/Download-Source
http://trac.pjsip.org/repos/wiki/Python_SIP/Build_Install
http://trac.pjsip.org/repos/wiki/Getting-Started/Windows

Wednesday, 28 August 2013

Python - Virtualenvwrapper on Ubuntu

Install pip
sudo apt-get install python-pip

Install virtualenvwrapper
pip install virtualenvwrapper
export WORKON_HOME=~/Envs
mkdir -p $WORKON_HOME
sudo echo 'source /usr/local/bin/virtualenvwrapper.sh' >> ~/.bashrc
# Restart terminal
Test
mkvirtualenv env1

Reference:
http://virtualenvwrapper.readthedocs.org/en/latest/install.html
http://roundhere.net/journal/virtualenv-ubuntu-12-10/

Python - Ubuntu Py-audio Compilation Fix

setup.py

Post Compilation Script (after python setup.py -v build)

*Final: python setup.py install

Reference:
https://code.google.com/p/py-audio/
https://code.google.com/p/py-audio/issues/detail?id=2

Tuesday, 6 August 2013

Django - Combine Querysets into List

Combine QuerySets
result_list = sorted(chain(salary_item_list, salary_item_extra_list), key=attrgetter('created'))
Deduplicate
unique_results = [rows.next() for (key, rows) in groupby(result_list, key=attrgetter('created')]

Django - Queryset Extra Replace Annotate

Individual case for Annotate Replacement without using Raw
- OrderedDict required to use with select_params
- Sum amount based on different data type
- Concatenate texts of grouped result.
Reference for GROUP_CONCAT: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
Reference for Extra: https://docs.djangoproject.com/en/dev/ref/models/querysets/#extra

Monday, 29 July 2013

Windows - Heroku SSH Troubleshooting

Could not open a connection to your authentication agent.

# Check your default .pub location, e.g. c:\users\username\.ssh
ssh -v git@github.com 
# Make sure both files included in the directory (id_rsa & id_rsa.pub)

My Key Files for Directory c:\users\username\.ssh
github_rsa
github_rsa.pub
id_rsa
id_rsa.pub
# Rename filename to id_rsa & id_rsa.pub

Cygwin - Command Shell Enable Copy Paste

1. Right Click Properties on Shell




2. Enable Quick Edit Mode Mode

OR

Edit .bachrc (Add following code)



Edit .inputrc (Add following code)

Thursday, 25 July 2013

Python - VoIP with SIP & Others

Public SIP Server List
https://code.google.com/p/sipml5/wiki/Public_SIP_Servers

SIP Client (Debug Tool)
http://icanblink.com/index.phtml

SIP response codes
http://en.wikipedia.org/wiki/List_of_SIP_response_codes

SIP

SIPpy
http://www.b2bua.org/

PJSIP Python
http://trac.pjsip.org/repos/wiki/Python_SIP_Tutorial
http://blog.pjsip.org/2008/07/24/python-sip-voip-softphone-client/

SIP SIMPLE
http://sipsimpleclient.org/
https://code.google.com/p/moshimoshi/
https://github.com/grengojbo/python-sipsimple

SIPVicious
https://code.google.com/p/sipvicious/

Divmod Sine
http://bazaar.launchpad.net/~divmod-dev/divmod.org/trunk/files/head:/Sine/
https://github.com/rcarmo/divmod.org

Shtoom (Last updated 2006)
http://sourceforge.net/projects/shtoom/
https://github.com/jrydberg/natmap

D-Bus
http://telepathy.freedesktop.org/wiki/
http://telepathy.freedesktop.org/doc/book/sect.services.python.html
https://github.com/PabloCastellano/telepathy-python

Asterisk server
https://pypi.python.org/pypi/django-asterisk/0.1

PyAudio (One way)
http://people.csail.mit.edu/hubert/pyaudio/
http://sharewebegin.blogspot.com/2013/06/real-time-voice-chat-example-in-python.html
http://www.ugw.name/?page_id=157

Twilio
https://www.twilio.com/
http://www.twilio.com/docs/libraries
https://developers.google.com/appengine/articles/twilio

GoogleVoice
https://code.google.com/p/pygooglevoice/

HTML5 SIP Client
http://sipml5.org/

P2P-SIP
http://39peers.net/
https://code.google.com/p/p2p-sip/

WebRTC (HTML5)
http://www.webrtc.org/reference/getting-started
https://code.google.com/p/py-webrtc/

Web Audio API (HTML 5)
https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html
http://www.html5rocks.com/en/tutorials/webaudio/intro/
http://updates.html5rocks.com/2012/09/Live-Web-Audio-Input-Enabled

Currently testing on: P2P-SIP (39Peers)