Archive for the ‘Web Programming’ Category

Selenium WebDriver won’t let you add/change request headers

Thursday, July 14th, 2011

Selenium looks like being a good tool for writing some automated browser tests.

Selenium WebDriver with the python bindings looks like it could be fantastic for doing this.

But then they go and spoil it…

I need to be able to modify the headers in my requests as part of my tests. WebDriver doesn’t let you do that. Worse, its a conscious design choice they have made to not let you do this since they want WD to only do what a user can do.

But, um, doesn’t WD allow you to set/change cookies? Go figure…

So their suggestion is to use a proxy for this.

So AFAICS they’ve taken the main benefit of using WebDriver over the original Selenium (you don’t need to run a separate server process) and removed it. As a design choice….

Looks like I’m not the only person who wants this feature..

http://code.google.com/p/selenium/issues/detail?id=141

How to import extra INSTALLED_APPS from an external settings file

Wednesday, March 23rd, 2011

from test_settings import *
applist = list(INSTALLED_APPS)
applist.extend(INSTALLED_TEST_APPS)
INSTALLED_APPS = tuple(applist)

“LESSjs will obsolete css”

Tuesday, August 31st, 2010

http://fadeyev.net/2010/06/19/lessjs-will-obsolete-css

“AttributeError: ‘module’ object has no attribute ‘blah’”

Wednesday, December 12th, 2007
  • The obvious cause of this is that the settings.py doesn’t have the directory containing blah listed in INSTALLED_APPS.
  • A less obvious cause: you’ll also get this error if the directory doesn’t contain a file __init__.py.

Dojo Custom Widget namespaces in V0.4

Tuesday, January 30th, 2007

Been beating my head against a brick wall trying to figure this out from the dojo documentation – here’s the answer…

http://blog.tijs.org/archives/2006/10/26/migrating-to-dojo-04/

Programmatic usage of FilteringTable

Friday, December 15th, 2006

I’m using FilteringTable in a dynamically generated website, here’s how I got it working…

Thanks to Tom Trenka for the original nudge in the direction of the Dojo nightly test for the programmatic usage of FilteringTable

/*
* Dynamic Filtering Table
*/

// Creates the FilteringTable widget and ties it to an existing HTML Table.
// Call once on load.

function doCreateFilteringTable(view) {
// debugger;
dojo.io.bind({
url: "/get_columns?view="+view,
load: handleCreateFilteringTable,
error: handleError,
timeout: handleTimeout,
mimetype: "application/json"
});
}

// Populates the FilteringTable widget with live data.
// Call whenever you want.

function doUpdateFilteringTable(view) {
// debugger;
var url;
url="/get_rows?view="+view;

dojo.io.bind({
url: url,
load: handleUpdateFilteringTable,
error: handleError,
timeout: handleTimeout,
mimetype: "application/json"
});
}

function handleCreateFilteringTable(type,data,evt) {
var e;

// debugger;

try {

var table_id="tbl_"+data.view;
var widget_id=table_id; // they don't need to be the same

var table;
table=dojo.widget.createWidget(
"dojo:FilteringTable",
{
// this provides unique id for table rows
valueField:"id",

// must provide this so that we can find it again,
// otherwise random id is generated
widgetId: "tbl_"+data.view,

},
dojo.byId(table_id)
);

// add the columns
for (x in data.columns)
{
var col=data.columns[x];
table.columns.push(table.createMetaData(col));
}

// set the initial data set to be empty
table.store.setData([]);

// fetch the data
doUpdateFilteringTable(data.view);
} catch (e) {
var errstr=e.name+": "+e.message;
doLog(errstr,"error");
}
}

function handleUpdateFilteringTable(type,data,evt) {
// debugger;

var e;

/* get list of tickers and build table from them */
try {
var view=data.view;

// fill in table body
var tabledata=[];
var table;
var id;
var entry;
var x;
var col;

table=dojo.widget.byId("tbl_"+view);
for (id in data.list)
{
// construct line
var line={};
entry=data.list[id];

for (x in entry)
{
// add one cell
// This is the format expected by dojo:filteringTable
var value;
col=entry[x];
line["id"]=id;
line[col["heading"]]=col["value"]
}

// now add the row
tabledata.push(line);
}

table.store.setData(tabledata);
} catch (e) {
var errstr=e.name+": "+e.message;
doLog(errstr,"error");
}
}

In this example, the server is returning this for get_columns():

{
"columns": [
{"dataType": "String", "field": "Column1"},
{"dataType": "String", "field": "Column2"},
{"dataType": "String", "field": "Column3"},
etc...
],
other stuff...
}

and get_rows() returns this from the server:

{
"list": {
"id1": [
{"heading": "Column1", "value": 25},
{"heading": "Column2", "value": "ABC.L"},
{"heading": "Column3", "value": 395}
],
"id2": [
{"heading": "Column1", "value": 57},
{"heading": "Column2", "value": "MFXYZ.L"},
{"heading": "Column3", "value": 42}
]
}
}

(I've removed non-relevant stuff from these responses for clarity.)

Don’t use eval() for reading JSON from untrusted sites

Wednesday, November 1st, 2006

Use a JSON parser instead, like this one at http://www.json.org/json.js/

Why? it protects you against malicious servers feeding you bad bad code…

Django GeoIP templatetag

Friday, October 27th, 2006

I’ve published 2 template tags for use with GeoIP at geoip.py.

Hope they may be of use…

Usage:

# Templatetag get_country_name returns the client’s country code
#
# Example:
# {% ifequal get_country “GB” %}
# do something
# {% endifequal %}

# Templatetag get_country sets the given variable name
# to the client’s country code
#
# Example:
# {% get_country as my_country %}
# {% ifequal my_country “GB” %}
# do something
# {% endifequal %}

Django/Ajax: a great simple tutorial

Friday, October 27th, 2006

An excellent tutorial from James Bennett for your first step into combining AJAX with Django

“A step-by-step walk through a simple AJAX form with Django backend”

How to make a Django ForeignKey optional

Friday, October 6th, 2006

Looks like you need to have both blank=True and null=True to make a ForeignKey optional in a django model…