Initial commit of Gilbert, an advanced Django administration interface.
[philo.git] / contrib / gilbert / utils.py
1 from inspect import isclass, getargspec
2
3
4 def is_gilbert_plugin(class_or_instance):
5         from philo.contrib.gilbert.options import GilbertPluginBase, GilbertPlugin
6         return (isclass(class_or_instance) and issubclass(class_or_instance, GilbertPlugin)) or isinstance(class_or_instance, GilbertPlugin) or (getattr(class_or_instance, '__metaclass__', None) is GilbertPluginBase) or (getattr(class_or_instance, 'gilbert_plugin', False) and (getattr(class_or_instance, 'gilbert_plugin_name', None) is not None) and (getattr(class_or_instance, 'gilbert_plugin_classes', None) is not None))
7
8
9 def is_gilbert_class(class_or_instance):
10         from philo.contrib.gilbert.options import GilbertClassBase, GilbertClass
11         return (isclass(class_or_instance) and issubclass(class_or_instance, GilbertClass)) or isinstance(class_or_instance, GilbertClass) or (getattr(class_or_instance, '__metaclass__', None) is GilbertClassBase) or (getattr(class_or_instance, 'gilbert_class', False) and (getattr(class_or_instance, 'gilbert_class_name', None) is not None) and (getattr(class_or_instance, 'gilbert_class_methods', None) is not None))
12
13
14 def is_gilbert_method(function):
15         return getattr(function, 'gilbert_method', False)
16
17
18 def gilbert_method(function=None, name=None, argc=None, restricted=True):
19         def wrapper(function):
20                 setattr(function, 'gilbert_method', True)
21                 setattr(function, 'gilbert_method_name', name or function.__name__)
22                 setattr(function, 'gilbert_method_restricted', restricted)
23                 new_argc = argc
24                 if new_argc is None:
25                         args = getargspec(function)[0]
26                         new_argc = len(args)
27                         if new_argc > 0:
28                                 if args[0] == 'self':
29                                         args = args[1:]
30                                         new_argc = new_argc - 1
31                                 if new_argc > 0:
32                                         if args[0] == 'request':
33                                                 args = args[1:]
34                                                 new_argc = new_argc - 1
35                 setattr(function, 'gilbert_method_argc', new_argc)
36                 return function
37         if function is not None:
38                 return wrapper(function)
39         return wrapper
40
41
42 def call_gilbert_method(method, cls, request, *args, **kwargs):
43         arg_list = getargspec(method)[0]
44         if len(arg_list) > 0:
45                 if arg_list[0] == 'self':
46                         if len(arg_list) > 1 and arg_list[1] == 'request':
47                                 return method(cls, request, *args, **kwargs)
48                         return method(cls, *args, **kwargs)
49                 elif arg_list[0] == 'request':
50                         return method(request, *args, **kwargs)
51         else:
52                 return method(*args, **kwargs)