71 from yams.schema import KNOWN_METAATTRIBUTES, role_name |
71 from yams.schema import KNOWN_METAATTRIBUTES, role_name |
72 from yams.constraints import (SizeConstraint, StaticVocabularyConstraint, |
72 from yams.constraints import (SizeConstraint, StaticVocabularyConstraint, |
73 FormatConstraint) |
73 FormatConstraint) |
74 |
74 |
75 from cubicweb import Binary, tags, uilib |
75 from cubicweb import Binary, tags, uilib |
|
76 from cubicweb.utils import support_args |
76 from cubicweb.web import INTERNAL_FIELD_VALUE, ProcessFormError, eid_param, \ |
77 from cubicweb.web import INTERNAL_FIELD_VALUE, ProcessFormError, eid_param, \ |
77 formwidgets as fw, uicfg |
78 formwidgets as fw, uicfg |
78 |
79 |
79 |
80 |
80 class UnmodifiedField(Exception): |
81 class UnmodifiedField(Exception): |
330 """Return the correctly typed value for this field in the form context. |
331 """Return the correctly typed value for this field in the form context. |
331 """ |
332 """ |
332 if self.eidparam and self.role is not None: |
333 if self.eidparam and self.role is not None: |
333 entity = form.edited_entity |
334 entity = form.edited_entity |
334 if form._cw.vreg.schema.rschema(self.name).final: |
335 if form._cw.vreg.schema.rschema(self.name).final: |
335 if entity.has_eid() or self.name in entity: |
336 if entity.has_eid() or self.name in entity.cw_attr_cache: |
336 value = getattr(entity, self.name) |
337 value = getattr(entity, self.name) |
337 if value is not None or not self.fallback_on_none_attribute: |
338 if value is not None or not self.fallback_on_none_attribute: |
338 return value |
339 return value |
339 elif entity.has_eid() or entity.cw_relation_cached(self.name, self.role): |
340 elif entity.has_eid() or entity.cw_relation_cached(self.name, self.role): |
340 value = [r[0] for r in entity.related(self.name, self.role)] |
341 value = [r[0] for r in entity.related(self.name, self.role)] |
343 return self.initial_typed_value(form, load_bytes) |
344 return self.initial_typed_value(form, load_bytes) |
344 |
345 |
345 def initial_typed_value(self, form, load_bytes): |
346 def initial_typed_value(self, form, load_bytes): |
346 if self.value is not _MARKER: |
347 if self.value is not _MARKER: |
347 if callable(self.value): |
348 if callable(self.value): |
348 return self.value(form) |
349 if support_args(self.value, 'form', 'field'): |
|
350 return self.value(form, self) |
|
351 else: |
|
352 warn("[3.10] field's value callback must now take form and " |
|
353 "field as argument (%s)" % self, DeprecationWarning) |
|
354 return self.value(form) |
349 return self.value |
355 return self.value |
350 formattr = '%s_%s_default' % (self.role, self.name) |
356 formattr = '%s_%s_default' % (self.role, self.name) |
351 if hasattr(form, formattr): |
357 if hasattr(form, formattr): |
352 warn('[3.6] %s.%s deprecated, use field.value' % ( |
358 warn('[3.6] %s.%s deprecated, use field.value' % ( |
353 form.__class__.__name__, formattr), DeprecationWarning) |
359 form.__class__.__name__, formattr), DeprecationWarning) |
420 option = list(option) |
426 option = list(option) |
421 option[1] = unicode(value) |
427 option[1] = unicode(value) |
422 vocab[i] = option |
428 vocab[i] = option |
423 return vocab |
429 return vocab |
424 |
430 |
425 def format(self, form): |
431 # support field as argument to avoid warning when used as format field value |
|
432 # callback |
|
433 def format(self, form, field=None): |
426 """return MIME type used for the given (text or bytes) field""" |
434 """return MIME type used for the given (text or bytes) field""" |
427 if self.eidparam and self.role == 'subject': |
435 if self.eidparam and self.role == 'subject': |
428 entity = form.edited_entity |
436 entity = form.edited_entity |
429 if entity.e_schema.has_metadata(self.name, 'format') and ( |
437 if entity.e_schema.has_metadata(self.name, 'format') and ( |
430 entity.has_eid() or '%s_format' % self.name in entity): |
438 entity.has_eid() or '%s_format' % self.name in entity.cw_attr_cache): |
431 return form.edited_entity.cw_attr_metadata(self.name, 'format') |
439 return form.edited_entity.cw_attr_metadata(self.name, 'format') |
432 return form._cw.property_value('ui.default-text-format') |
440 return form._cw.property_value('ui.default-text-format') |
433 |
441 |
434 def encoding(self, form): |
442 def encoding(self, form): |
435 """return encoding used for the given (text) field""" |
443 """return encoding used for the given (text) field""" |