1 from inspect import isclass, getargspec
2 from functools import wraps
3 from django.utils.encoding import force_unicode
4 from django.forms.widgets import Widget, Input, HiddenInput, FileInput, DateInput, TimeInput, Textarea, CheckboxInput, Select, SelectMultiple
5 from django.forms.fields import FileField
6 from django.forms.forms import BaseForm
9 def _render_ext(self, name, value):
10 ext_spec = {'name': name}
12 ext_spec['value'] = value
13 if isinstance(self, Input):
14 if isinstance(self, HiddenInput):
15 ext_spec['xtype'] = 'hidden'
16 elif isinstance(self, FileInput):
17 ext_spec['xtype'] = 'fileuploadfield'
18 elif isinstance(self, DateInput):
19 ext_spec['xtype'] = 'datefield'
20 elif isinstance(self, TimeInput):
21 ext_spec['xtype'] = 'timefield'
23 ext_spec['xtype'] = 'textfield'
24 ext_spec['inputType'] = self.input_type
25 elif isinstance(self, Textarea):
26 ext_spec['xtype'] = 'textarea'
27 elif isinstance(self, CheckboxInput):
28 ext_spec['xtype'] = 'checkbox'
29 elif isinstance(self, Select):
30 ext_spec['xtype'] = 'combo'
31 ext_spec['store'] = self.choices
32 ext_spec['typeAhead'] = True
33 if isinstance(self, SelectMultiple):
40 Widget.render_ext = _render_ext
49 label = force_unicode(bf.label)
53 if bf.field.show_hidden_initial:
58 widget = bf.field.widget
61 data = self.initial.get(bf.name, bf.field.initial)
65 if isinstance(bf.field, FileField) and bf.data is None:
66 data = self.initial.get(bf.name, bf.field.initial)
72 name = bf.html_initial_name
74 rendered = widget.render_ext(name, data)
75 if rendered is not None:
76 rendered['fieldLabel'] = label
77 fields.append(rendered)
78 ext_spec['items'] = fields
79 ext_spec['labelSeparator'] = self.label_suffix
83 BaseForm.as_ext = _as_ext
86 def is_gilbert_method(function):
87 return getattr(function, 'gilbert_method', False)
90 def gilbert_method(function=None, name=None, argc=None, form_handler=False, restricted=True):
92 setattr(function, 'gilbert_method', True)
93 setattr(function, 'name', name or function.__name__)
94 setattr(function, 'form_handler', form_handler)
95 setattr(function, 'restricted', restricted)
98 args = getargspec(function)[0]
101 if args[0] == 'self':
103 new_argc = new_argc - 1
105 if args[0] == 'request':
107 new_argc = new_argc - 1
108 setattr(function, 'argc', new_argc)
110 if function is not None:
111 return setter(function)
115 class GilbertPluginBase(type):
116 def __new__(cls, name, bases, attrs):
117 if 'methods' not in attrs:
119 for attr in attrs.values():
120 if is_gilbert_method(attr):
121 methods.append(attr.name)
122 attrs['methods'] = methods
123 return super(GilbertPluginBase, cls).__new__(cls, name, bases, attrs)
126 class GilbertPlugin(object):
127 __metaclass__ = GilbertPluginBase
129 def __init__(self, site):
132 def get_method(self, method_name):
133 method = getattr(self, method_name, None)
134 if not is_gilbert_method(method):
151 def fugue_icons(self):
155 class GilbertModelAdmin(GilbertPlugin):
156 def __init__(self, site, model):
158 self.name = model._meta.object_name
159 super(GilbertModelAdmin, self).__init__(site)
163 return list(self.model._default_manager.all().values())
166 def get(self, constraint):
167 return self.model._default_manager.all().values().get(**constraint)