11.18.2011

Decorating every view at the url.py level

I think of authentication and authorization usually in entire sections of a site/url tree. So it always annoyed me that there was no mechanism in django to decorate entire sections of a site at the urls.py level.

So, I came up with the following code that does just that.

'''
Author: Stephen J. Zabel
License: BSD
This module exposed a helper function for
wrapping views at the urls.py/resolver level
My personal little itch as an example...
urlpatterns += required(
login_required,
patterns('',
(r'^api/',
include(api.urls)),
)
)
'''
def required(wrapping_functions,patterns_rslt):
'''
Used to require 1..n decorators in any view returned by a url tree
Usage:
urlpatterns = required(func,patterns(...))
urlpatterns = required((func,func,func),patterns(...))
Note:
Use functools.partial to pass keyword params to the required
decorators. If you need to pass args you will have to write a
wrapper function.
Example:
from functools import partial
urlpatterns = required(
partial(login_required,login_url='/accounts/login/'),
patterns(...)
)
'''
if not hasattr(wrapping_functions,'__iter__'):
wrapping_functions = (wrapping_functions,)
return [
_wrap_instance__resolve(wrapping_functions,instance)
for instance in patterns_rslt
]
def _wrap_instance__resolve(wrapping_functions,instance):
if not hasattr(instance,'resolve'): return instance
resolve = getattr(instance,'resolve')
def _wrap_func_in_returned_resolver_match(*args,**kwargs):
rslt = resolve(*args,**kwargs)
if not hasattr(rslt,'func'):return rslt
f = getattr(rslt,'func')
for _f in reversed(wrapping_functions):
# @decorate the function from inner to outter
f = _f(f)
setattr(rslt,'func',f)
return rslt
setattr(instance,'resolve',_wrap_func_in_returned_resolver_match)
return instance
view raw urls.py hosted with ❤ by GitHub

5.18.2011

Ext4 MVC example reader.read is not a function

in your store
Ext.define('ID.store.Users', {
extend: 'Ext.data.Store',
model: 'User',

fully qualify the model

Ext.define('ID.store.Users', {
extend: 'Ext.data.Store',
model: 'ID.model.User',

[ERROR] Ext is not defined, please verify that the library is loaded properly on the application's page

I've started playing in earnest with Ext4 and I'm going to list out errors that I hit and fix as I hit them.

When running the sencha command in Ubuntu Linux (from the Deployment section. 3 of the getting started guide)

> sencha create jsb -a index.html -p app.jsb3
[ERROR] Ext is not defined, please verify that the library is loaded properly on the application's page
undefined:0 ReferenceError: Can't find variable: Ext

use fully qualified paths

> sencha create jsb -a /home/stephen/repo/ext-id/index_dev.html -p /home/stephen/repo/ext-id/app2.jsb3
file:///home/stephen/repo/ext-id/app/controller/Users.js?_dc=1305753075380:40 The panel was rendered
file:///home/stephen/repo/ext-id/app.js:13 Launch

cheers,
Stephen

4.21.2011

Vim for Django... a hell of a good start

http://agiliq.com/blog/2010/11/seven-reasons-why-you-should-switch-to-vim-for-dja/