Utility Functions

json_helpers

djqgrid.json_helpers.function(funcname)

Wraps a JavaScript function name with our token, so it can be unquoted when rendering to JSON.

The jqGrid option object is actually a Python dictionary that is rendered to JSON in the jqgrid template tag.

Unfortunately, Python’s json module can’t output such JSON, it will always put quotes around strings (without the quotes, it’s not legal JSON), so we need to work around it to support unquoted strings.

The solution is simple and a bit ugly - we use a token that we wrap around function names. We serialize the dictionary to a JSON string, look for the token and remove the quotes around it.

Our token is @@. So in the example about, our Python dict will be: {'loadComplete': '@@loadCompleteHandler@@'). When we create the JSON, we remove the quotes and the @@, and end up with the expected string.

The function helper function puts the token around the string, so that we can build our dictionary like so: d = {'loadComplete': function('loadCompleteHandler')) .

Args:
funcname - the function’s name
Returns:
The wrapped function name
djqgrid.json_helpers.dumps(o, *args, **kwargs)

Serializes an object to JSON, unquoting function names.

Args:
o: Object to serialize *args: Additional arguments passed to json.dumps **kwargs: Additional arguments passed to json.dumps

grid_registrar

This module handles all the grid registration.

A Grid object is created multiple times - when the HTML containing the grid is rendered, and every time a the grid’s data needs to be retrieved. Since each of these times is an independent HTTP request, we need to somehow pass the information of the grid’s class. This is done with a Grid ID.

Each HTTP request gets its own Grid instance, we just pass the information of the Grid’s class around. Since we can’t create a class just because we’ve received its name in an HTTP request (it’s a huge security hole), we only create classes that have been registered before. We also don’t want to pass class names in HTTP requests, so we pass class IDs.

djqgrid.grid_registrar.get_grid_class(id)

Returns a class for a given ID.

Args:
Grid ID
Returns:
The grid’s class
Raises:
KeyError if the ID hasn’t been registered
djqgrid.grid_registrar.register_grid(gridcls)

Registers a Grid class in the registry.

Calls gridcls.get_grid_id to get the class’s ID.

Args:
gridcls: Class of Grid to registry

views

class djqgrid.views.JsonResponse(content, status=None, content_type='application/json')

Returns a JSON Response

Takes the content object, json.dumps it and returns it an a response.

Args:
content: The object to be serialized into JSON mimetype: Response mime type. Default is application/json status: HTTP status code. Default is NONE. content_type: The response content type. Default is application/json
Returns:
A Django response object
djqgrid.views.query(request, grid_id)

Returns the grid content in a JSON response

query creates a new Grid instance based on the grid_id (which represents the Grid class), and calls grid.get_json_data to perform the actual query.

Args:
request: Django request grid_id: ID of the grid. The ID is generated by Grid.get_grid_id
Returns:
The JSON serialized grid contents.