Cleanup of Gilbert plugins API and JavaScript.
[philo.git] / contrib / gilbert / plugins.py
1 from inspect import isclass, getargspec
2
3
4 def is_gilbert_method(function):
5         return getattr(function, 'gilbert_method', False)
6
7
8 def gilbert_method(function=None, name=None, argc=None, restricted=True):
9         def wrapper(function):
10                 setattr(function, 'gilbert_method', True)
11                 setattr(function, 'name', name or function.__name__)
12                 setattr(function, 'restricted', restricted)
13                 new_argc = argc
14                 if new_argc is None:
15                         args = getargspec(function)[0]
16                         new_argc = len(args)
17                         if new_argc > 0:
18                                 if args[0] == 'self':
19                                         args = args[1:]
20                                         new_argc = new_argc - 1
21                                 if new_argc > 0:
22                                         if args[0] == 'request':
23                                                 args = args[1:]
24                                                 new_argc = new_argc - 1
25                 setattr(function, 'argc', new_argc)
26                 return function
27         if function is not None:
28                 return wrapper(function)
29         return wrapper
30
31
32 class GilbertPluginBase(type):
33         def __new__(cls, name, bases, attrs):
34                 if 'methods' not in attrs:
35                         methods = []
36                         for attr in attrs.values():
37                                 if is_gilbert_method(attr):
38                                         methods.append(attr.name)
39                         attrs['methods'] = methods
40                 return super(GilbertPluginBase, cls).__new__(cls, name, bases, attrs)
41
42
43 class GilbertPlugin(object):
44         __metaclass__ = GilbertPluginBase
45         
46         def __init__(self, site):
47                 self.site = site
48         
49         def get_method(self, method_name):
50                 method = getattr(self, method_name, None)
51                 if not is_gilbert_method(method):
52                         return None
53                 return method
54         
55         @property
56         def urls(self):
57                 return []
58         
59         @property
60         def js(self):
61                 return []
62         
63         @property
64         def css(self):
65                 return []
66         
67         @property
68         def fugue_icons(self):
69                 return []
70
71
72 class GilbertModelAdmin(GilbertPlugin):
73         def __init__(self, site, model):
74                 self.model = model
75                 self.name = model._meta.object_name
76                 super(GilbertModelAdmin, self).__init__(site)
77         
78         @gilbert_method
79         def all(self):
80                 return list(self.model._default_manager.all().values())
81         
82         @gilbert_method
83         def get(self, constraint):
84                 return self.model._default_manager.all().values().get(**constraint)