Package pyjamas :: Package django :: Module Form
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.django.Form

  1  """ Pyjamas Django Forms Integration 
  2   
  3      Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  4   
  5  """ 
  6   
  7  from pyjamas.ui.TextBox import TextBox 
  8  from pyjamas.ui.Grid import Grid 
  9  from pyjamas.ui.FormPanel import FormPanel 
 10  #from pyjamas.ui.Composite import Composite 
 11  from pyjamas.log import writebr 
 12   
 13  from pyjamas.ui.TextBox import TextBox 
 14   
15 -class CharField(TextBox):
16 - def __init__(self, **kwargs):
17 TextBox.__init__(self) 18 self.max_length = kwargs.get('max_length', None) 19 self.min_length = kwargs.get('min_length', None) 20 self.required = kwargs.get('required', None) 21 if kwargs.get('initial'): 22 self.setValue(kwargs['initial'])
23
24 - def setValue(self, val):
25 if val is None: 26 val = '' 27 self.setText(val)
28
29 - def getValue(self):
30 return self.getText()
31
32 -class IntegerField(TextBox):
33 - def __init__(self, **kwargs):
34 #writebr("MAKING INTEGERFIELD from kwargs: %s" % kwargs) 35 TextBox.__init__(self) 36 self.required = kwargs.get('required', None) 37 if kwargs.get('initial'): 38 self.setValue(kwargs['initial'])
39
40 - def setValue(self, val):
41 if val is None: 42 val = '' 43 self.setText(val)
44
45 - def getValue(self):
46 writebr('GETTING VALUE< self.text: %s' % self.getText()) 47 return int(self.getText())
48
49 -class FloatField(TextBox):
50 - def __init__(self, **kwargs):
51 TextBox.__init__(self) 52 self.max_length = kwargs.get('max_length', None) 53 self.min_length = kwargs.get('min_length', None) 54 self.required = kwargs.get('required', None) 55 if kwargs.get('initial'): 56 self.setValue(kwargs['initial'])
57
58 - def setValue(self, val):
59 if val is None: 60 val = '' 61 self.setText(val)
62
63 - def getValue(self):
64 return self.getText()
65 66 widget_factory = {'CharField': CharField, 67 'IntegerField': IntegerField, 68 'FloatField': FloatField 69 } 70
71 -class FormSaveGrid:
72
73 - def __init__(self, sink):
74 self.sink = sink
75
76 - def onRemoteResponse(self, response, request_info):
77 78 method = request_info.method 79 80 writebr(repr(response)) 81 writebr("%d" % len(response)) 82 writebr("%s" % repr(response.keys())) 83 84 self.sink.save_respond(response)
85
86 -class FormGetGrid:
87
88 - def __init__(self, sink):
89 self.sink = sink
90
91 - def onRemoteResponse(self, response, request_info):
92 93 method = request_info.method 94 95 writebr(method) 96 writebr(repr(response)) 97 writebr("%d" % len(response)) 98 writebr("%s" % repr(response.keys())) 99 100 self.sink.do_get(response)
101
102 - def onRemoteError(self, code, message, request_info):
103 writebr("Server Error or Invalid Response: ERROR %d" % code + " - " + message + ' - Remote method : ' + request_info.method)
104
105 -class FormDescribeGrid:
106
107 - def __init__(self, sink):
108 self.sink = sink
109
110 - def onRemoteResponse(self, response, request_info):
111 112 method = request_info.method 113 114 115 self.sink.do_describe(response)
116
117 - def onRemoteError(self, code, message, request_info):
118 writebr("Server Error or Invalid Response: ERROR %d" % code + " - " + message + ' - Remote method : ' + request_info.method)
119
120 -class Form(FormPanel):
121
122 - def __init__(self, svc, **kwargs):
123 124 self.describe_listeners = [] 125 if kwargs.has_key('listener'): 126 listener = kwargs.pop('listener') 127 self.addDescribeListener(listener) 128 129 if kwargs.has_key('data'): 130 data = kwargs.pop('data') 131 else: 132 data = None 133 134 FormPanel.__init__(self, **kwargs) 135 self.svc = svc 136 self.grid = Grid() 137 self.grid.resize(0, 3) 138 self.add(self.grid) 139 self.describer = FormDescribeGrid(self) 140 self.saver = FormSaveGrid(self) 141 self.getter = FormGetGrid(self) 142 self.formsetup(data)
143
144 - def addDescribeListener(self, l):
145 self.describe_listeners.append(l)
146
147 - def add_widget(self, description, widget):
148 """ adds a widget, with error rows interspersed 149 """ 150 151 num_rows = self.grid.getRowCount() 152 self.grid.resize((num_rows+1), 3) 153 self.grid.setHTML(num_rows, 0, description) 154 self.grid.setWidget(num_rows, 1, widget)
155
156 - def get(self, **kwargs):
157 writebr(repr(kwargs)) 158 self.svc({}, {'get': kwargs}, self.getter)
159
160 - def save(self, data=None):
161 self.clear_errors() 162 if data is None: 163 data = self.getValue() 164 self.data = data 165 writebr(repr(self.data)) 166 self.svc(data, {'save': None}, self.saver)
167
168 - def save_respond(self, response):
169 170 if not response['success']: 171 errors = response['errors'] 172 self.set_errors(errors) 173 for l in self.describe_listeners: 174 l.onErrors(self, errors) 175 return 176 177 for l in self.describe_listeners: 178 l.onSaveDone(self, response)
179
180 - def formsetup(self, data=None):
181 182 if data is None: 183 data = {} 184 self.data = data 185 self.svc(data, {'describe': None}, self.describer)
186
187 - def clear_errors(self):
188 189 for idx, fname in enumerate(self.fields): 190 self.grid.setHTML(idx, 2, None)
191
192 - def set_errors(self, errors):
193 194 offsets = {} 195 for idx, fname in enumerate(self.fields): 196 offsets[fname] = idx 197 for k, err in errors.items(): 198 err = "<br />".join(err) 199 idx = offsets[k] 200 self.grid.setHTML(idx, 2, err)
201
202 - def update_values(self, data = None):
203 if data is not None: 204 self.data = data 205 206 for idx, fname in enumerate(self.fields): 207 val = None 208 if self.data.has_key(fname): 209 val = self.data[fname] 210 w = self.grid.getWidget(idx, 1) 211 w.setValue(val)
212
213 - def do_get(self, response):
214 fields = response.get('instance', None) 215 if fields: 216 self.update_values(fields) 217 for l in self.describe_listeners: 218 l.onRetrieveDone(self, fields)
219
220 - def do_describe(self, fields):
221 222 self.fields = fields.keys() 223 for idx, fname in enumerate(self.fields): 224 field = fields[fname] 225 if self.data and self.data.has_key(fname): 226 field['initial'] = self.data[fname] 227 field_type = field['type'] 228 widget_kls = widget_factory.get(field_type, CharField) 229 fv = {} 230 for (k, v) in field.items(): 231 fv[str(k)] = v 232 w = widget_kls(**fv) 233 self.add_widget(field['label'], w) 234 235 for l in self.describe_listeners: 236 l.onDescribeDone(self)
237
238 - def getValue(self):
239 240 res = {} 241 for idx, fname in enumerate(self.fields): 242 w = self.grid.getWidget(idx, 1) 243 val = w.getValue() 244 res[fname] = val 245 self.data[fname] = val 246 247 return res
248