1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 import re
22 from itertools import chain
23
24 from django import forms
25 from django.forms.widgets import SelectMultiple, CheckboxInput, MultipleHiddenInput
26 from django.utils.encoding import force_unicode
27 from django.utils.html import escape, conditional_escape
28 from django.utils.safestring import mark_safe
29
30 from django.forms.fields import Field, EMPTY_VALUES
31 from django.forms.widgets import Select
32 from django.forms import ModelChoiceField, ValidationError
33 from django.utils.translation import ugettext_lazy as _
34 from django.utils.encoding import smart_unicode
35
36 from omero_model_FileAnnotationI import FileAnnotationI
37 from omero_model_TagAnnotationI import TagAnnotationI
38 from omero_model_LongAnnotationI import LongAnnotationI
39
40
41
42
45 if not value:
46 raise forms.ValidationError('No email.')
47 if value.count(' ') > 0:
48 raise forms.ValidationError('Use only separator ";". Remove every spaces.')
49 emails = value.split(';')
50 for email in emails:
51 if not self.is_valid_email(email):
52 raise forms.ValidationError('%s is not a valid e-mail address. Use separator ";"' % email)
53 return emails
54
56 email_pat = re.compile(r"(?:^|\s)[-a-z0-9_.]+@(?:[-a-z0-9]+\.)+[a-z]{2,6}(?:\s|$)",re.IGNORECASE)
57 return email_pat.match(email) is not None
58
61 if not value:
62 raise forms.ValidationError('No url.')
63 if not self.is_valid_url(value):
64 raise forms.ValidationError('%s is not a valid url' % value)
65 return value
66
68 url_pat = re_http = re.compile(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',re.IGNORECASE)
69 return url_pat.match(url) is not None
70
71
72
73
85
86
87
88
124
126
127 - def __init__(self, queryset, empty_label, cache_choices):
128 self.queryset = queryset
129 self.empty_label = empty_label
130 self.cache_choices = cache_choices
131
133 if self.empty_label is not None:
134 yield (u"", self.empty_label)
135 for obj in self.queryset:
136 textValue = None
137 if isinstance(obj._obj, FileAnnotationI):
138 textValue = (len(obj.getFileName()) < 45) and (obj.getFileName()) or (obj.getFileName()[:42]+"...")
139 elif isinstance(obj._obj, TagAnnotationI):
140 if obj.textValue is not None:
141 if obj.ns is not None and obj.ns!="":
142 textValue = (len(obj.textValue) < 45) and ("%s (tagset)" % obj.textValue) or ("%s (tagset)" % obj.textValue[:42]+"...")
143 else:
144 textValue = (len(obj.textValue) < 45) and (obj.textValue) or (obj.textValue[:42]+"...")
145 elif isinstance(obj._obj, LongAnnotationI):
146 textValue = obj.longValue
147 else:
148 textValue = obj.textValue
149
150 if isinstance(textValue, str):
151 l = len(textValue)
152 if l > 55:
153 textValue = "%s..." % textValue[:55]
154 oid = obj.id
155 yield (oid, smart_unicode(textValue))
156
157
158
159
161
163
164
165 if hasattr(self, '_choices'):
166 return self._choices
167
168
169
170
171
172
173 return AnnotationQuerySetIterator(self.queryset, self.empty_label,
174 self.cache_choices)
175
177
178
179
180 self._choices = self.widget.choices = list(value)
181
182 choices = property(_get_choices, _set_choices)
183
185 Field.clean(self, value)
186 if value in EMPTY_VALUES:
187 return None
188 res = False
189 for q in self.queryset:
190 if long(value) == q.id:
191 res = True
192 if not res:
193 raise ValidationError(self.error_messages['invalid_choice'])
194 return value
195
197 """A MultipleChoiceField whose choices are a model QuerySet."""
198 hidden_widget = MultipleHiddenInput
199 default_error_messages = {
200 'list': _(u'Enter a list of values.'),
201 'invalid_choice': _(u'Select a valid choice. That choice is not one of the'
202 u' available choices.'),
203 }
204 - def __init__(self, queryset, cache_choices=False, required=True,
205 widget=SelectMultiple, label=None, initial=None,
206 help_text=None, *args, **kwargs):
207 super(AnnotationModelMultipleChoiceField, self).__init__(queryset, None,
208 cache_choices, required, widget, label, initial, help_text,
209 *args, **kwargs)
210
212 if self.required and not value:
213 raise ValidationError(self.error_messages['required'])
214 elif not self.required and not value:
215 return []
216 if not isinstance(value, (list, tuple)):
217 raise ValidationError(self.error_messages['list'])
218 final_values = []
219 for val in value:
220 try:
221 long(val)
222 except:
223 raise ValidationError(self.error_messages['invalid_choice'])
224 else:
225 res = False
226 for q in self.queryset:
227 if long(val) == q.id:
228 res = True
229 if not res:
230 raise ValidationError(self.error_messages['invalid_choice'])
231 else:
232 final_values.append(val)
233 return final_values
234
235
237 - def __init__(self, queryset, empty_label, cache_choices):
238 self.queryset = queryset
239 self.empty_label = empty_label
240 self.cache_choices = cache_choices
241
243 if self.empty_label is not None:
244 yield (u"", self.empty_label)
245 for obj in self.queryset:
246 if hasattr(obj.id, 'val'):
247 yield (obj.id.val, smart_unicode(obj.id.val))
248 else:
249 yield (obj.id, smart_unicode(obj.id))
250
251
252
253
255
257
258
259 if hasattr(self, '_choices'):
260 return self._choices
261
262
263
264
265
266
267 return ObjectQuerySetIterator(self.queryset, self.empty_label,
268 self.cache_choices)
269
271
272
273
274 self._choices = self.widget.choices = list(value)
275
276 choices = property(_get_choices, _set_choices)
277
279 Field.clean(self, value)
280 if value in EMPTY_VALUES:
281 return None
282 res = False
283 for q in self.queryset:
284 if hasattr(q.id, 'val'):
285 if long(value) == q.id.val:
286 res = True
287 else:
288 if long(value) == q.id:
289 res = True
290 if not res:
291 raise ValidationError(self.error_messages['invalid_choice'])
292 return value
293
295 """A MultipleChoiceField whose choices are a model QuerySet."""
296 hidden_widget = MultipleHiddenInput
297 default_error_messages = {
298 'list': _(u'Enter a list of values.'),
299 'invalid_choice': _(u'Select a valid choice. That choice is not one of the'
300 u' available choices.'),
301 }
302
303 - def __init__(self, queryset, cache_choices=False, required=True,
304 widget=SelectMultiple, label=None, initial=None,
305 help_text=None, *args, **kwargs):
306 super(ObjectModelMultipleChoiceField, self).__init__(queryset, None,
307 cache_choices, required, widget, label, initial, help_text,
308 *args, **kwargs)
309
311 if self.required and not value:
312 raise ValidationError(self.error_messages['required'])
313 elif not self.required and not value:
314 return []
315 if not isinstance(value, (list, tuple)):
316 raise ValidationError(self.error_messages['list'])
317 final_values = []
318 for val in value:
319 try:
320 long(val)
321 except:
322 raise ValidationError(self.error_messages['invalid_choice'])
323 else:
324 res = False
325 for q in self.queryset:
326 if hasattr(q.id, 'val'):
327 if long(val) == q.id.val:
328 res = True
329 else:
330 if long(val) == q.id:
331 res = True
332 if not res:
333 raise ValidationError(self.error_messages['invalid_choice'])
334 else:
335 final_values.append(val)
336 return final_values
337