Archive for October, 2011

October 18, 2011

google app engine 1.5.5 sdk not a relief

task queues + backends + cron jobs don’t work well together on the development server. this has been the state for the last few months sucking up huge amount of dev + qa + release cycles.

checks like these continue to fail on the development server but work on the production (appspot) platform.

assert get_backend() == ‘consolidate_billing_units’

October 17, 2011

salesforce contact integration on google app engine

contact integration with salesforce is easy. here is how we did it recently on a Google App Engine project.

this works for enterprise salesforce accounts only (and ofcourse developer accounts)

step 1: get an authorization code from salesforce

AUTHORIZE_URI = ‘https://login.salesforce.com

def get(self, **kwargs):
code = request.args.get(“code”)
args = {
‘client_id’: SALESFORCE_CONSUMER_KEY,
‘redirect_uri’: REDIRECT_URI,
‘state’:workspace # salesforce allows user # defined to be stored in the # state variable
}

if code is not None:

# call back from salesforce …

args[“client_secret”] = SALESFORCE_CONSUMER_SECRET args[“code”] = request.args.get(“code”)
args[‘grant_type’] = “authorization_code”

url = AUTHORIZE_URI + “/services/oauth2/token?” + urllib.urlencode(args)
response = urlfetch.fetch(url, method=urlfetch.POST, validate_certificate=False) content = json.loads(response.content)

access_token = content[‘access_token’]
instance_url = content[‘instance_url’]
refresh_token = content[‘refresh_token’]

else:

# call salesforce

args[‘response_type’] = ‘code’
url = ‘%s%s’%(AUTHORIZE_URI, “/services/oauth2/authorize?%s”%urllib.urlencode(args)) return redirect(url)

step 2: use the token to call into salesforce

def salesforce_query(email, contact_fields, access_token, instance_url):

fields = ‘, ‘.join([f for f in contact_fields])

args = {
“q” :”SELECT %s from Contact where Email = ‘%s'” % (fields, email) }

headers = {
“Authorization”: “OAuth %s” % access_token
}

url = ‘%s%s%s’ % (instance_url, “/services/data/v20.0/query?”, urllib.urlencode(args))

response = urlfetch.fetch(url, headers=headers, method=urlfetch.GET, validate_certificate=False) return simplejson.loads(response.content)

October 17, 2011

a simple and elegant yes no dialog using dojo

a very common situation in ui design is to let the user confirm an action (especially one which is destructive) via a dialog. similar to microsoft windows yes/no or ok/cancel dialogs.

here is how do accomplish this using dojo, something I customized for our needs,

1. first the markup

Yes

No

2. next the handler

function yesno()
{
// assume that yesno is called when the user clicks on the object which is key // to the elements being destroyed.

var obj = this;

// my yesno is reusable, you could arbitrarily bind the same // dialog to more than one object. i store the title and the // description of the dialog in the object itself.

var description = obj.node.dialog_description;
var title = obj.node.dialog_title;

// tie the user action handlers – here if the user clicks on yes, // the object has to be destroyed.

dojo.byId(‘yesbutton’).onclick = dojo.hitch(obj, remove);

// no? simply hide the dialog.

dojo.byId(‘nobutton’).onclick = dojo.hitch(obj, function(){yndialog.hide()}); dojo.byId(‘yncontent’).innerHTML = description;
yndialog.set(‘title’, title);
yndialog.show();
}

function remove()
{

// this is the remove handler. i associate this handler with the // object itself such that ‘this’ points to the object.

var obj = this;

dojo.destroy(obj); // or trigger a server side action and then destroy this node. }