0008-formdef-factorize-iter_fields-methods-60706.patch
wcs/backoffice/management.py | ||
---|---|---|
1545 | 1545 |
field.has_relations = True |
1546 | 1546 |
yield UserRelatedField(field) |
1547 | 1547 | |
1548 |
def iter_fields(fields, block_field=None): |
|
1549 |
for field in fields: |
|
1550 |
# add contextual_id/contextual_varname attributes |
|
1551 |
# they are id/varname for normal fields |
|
1552 |
# but in case of blocks they are concatenation of block id/varname + field id/varname |
|
1553 |
field.contextual_id = field.id |
|
1554 |
field.contextual_varname = None |
|
1555 |
if block_field: |
|
1556 |
if field.key == 'items': |
|
1557 |
# not yet |
|
1558 |
continue |
|
1559 |
field.block_field = block_field |
|
1560 |
field.contextual_id = '%s-%s' % (field.block_field.id, field.id) |
|
1561 |
if field.varname and field.block_field.varname: |
|
1562 |
field.contextual_varname = '%s_%s' % ( |
|
1563 |
field.block_field.varname, |
|
1564 |
field.varname, |
|
1565 |
) |
|
1566 |
else: |
|
1567 |
field.contextual_varname = field.varname |
|
1568 |
yield field |
|
1569 |
if not get_publisher().is_using_postgresql(): |
|
1570 |
continue |
|
1571 |
if field.key == 'block': |
|
1572 |
yield from iter_fields(field.block.fields, block_field=field) |
|
1573 |
continue |
|
1574 |
if block_field: |
|
1575 |
continue |
|
1576 |
if not ( |
|
1577 |
field.type == 'item' |
|
1578 |
and field.data_source |
|
1579 |
and field.data_source.get('type', '').startswith('carddef:') |
|
1580 |
): |
|
1548 |
for field in self.formdef.iter_fields(include_block_fields=True): |
|
1549 |
if getattr(field, 'block_field', None): |
|
1550 |
if field.key == 'items': |
|
1551 |
# not yet |
|
1581 | 1552 |
continue |
1582 |
try: |
|
1583 |
carddef = CardDef.get_by_urlname(field.data_source['type'][8:]) |
|
1584 |
except KeyError: |
|
1553 |
yield field |
|
1554 |
if not get_publisher().is_using_postgresql(): |
|
1555 |
continue |
|
1556 |
if field.key == 'block': |
|
1557 |
continue |
|
1558 |
if getattr(field, 'block_field', None): |
|
1559 |
continue |
|
1560 |
if not ( |
|
1561 |
field.type == 'item' |
|
1562 |
and field.data_source |
|
1563 |
and field.data_source.get('type', '').startswith('carddef:') |
|
1564 |
): |
|
1565 |
continue |
|
1566 |
try: |
|
1567 |
carddef = CardDef.get_by_urlname(field.data_source['type'][8:]) |
|
1568 |
except KeyError: |
|
1569 |
continue |
|
1570 |
for card_field in carddef.get_all_fields(): |
|
1571 |
if not hasattr(card_field, 'get_view_value'): |
|
1585 | 1572 |
continue |
1586 |
for card_field in carddef.get_all_fields(): |
|
1587 |
if not hasattr(card_field, 'get_view_value'): |
|
1588 |
continue |
|
1589 |
field.has_relations = True |
|
1590 |
yield RelatedField(carddef, card_field, field) |
|
1591 | ||
1592 |
yield from iter_fields(self.formdef.get_all_fields()) |
|
1573 |
field.has_relations = True |
|
1574 |
yield RelatedField(carddef, card_field, field) |
|
1593 | 1575 | |
1594 | 1576 |
yield FakeField('status', 'status', _('Status')) |
1595 | 1577 |
yield FakeField('anonymised', 'anonymised', _('Anonymised')) |
wcs/formdef.py | ||
---|---|---|
535 | 535 |
self_ref = '%s:%s' % (self.xml_root_node, self.url_name) |
536 | 536 |
relations_by_ref = collections.defaultdict(list) |
537 | 537 | |
538 |
def _check_field(field, in_block_field=False, prefix=''):
|
|
538 |
def _check_field(field): |
|
539 | 539 |
data_source = getattr(field, 'data_source', None) |
540 | 540 |
if not data_source or not data_source.get('type', '').startswith('carddef:'): |
541 | 541 |
return |
542 |
varname = field.varname or '' |
|
543 |
if in_block_field: |
|
544 |
if prefix and field.varname: |
|
545 |
varname = '%s_%s' % (prefix, field.varname) |
|
546 |
else: |
|
547 |
varname = '' |
|
548 | 542 |
# reverse relation of data_source['type'] to this object |
549 | 543 |
obj_ref = ':'.join(data_source['type'].split(':')[:2]) # remove possible custom-view |
550 | 544 |
relations_by_ref[obj_ref].append( |
551 | 545 |
{ |
552 |
'varname': varname,
|
|
546 |
'varname': field.contextual_varname or '',
|
|
553 | 547 |
'type': field.key, |
554 | 548 |
'obj': self_ref, |
555 | 549 |
} |
556 | 550 |
) |
557 | 551 | |
558 |
for field in self.get_all_fields():
|
|
552 |
for field in self.iter_fields(include_block_fields=True):
|
|
559 | 553 |
if field.key in ['item', 'items', 'computed']: |
560 | 554 |
_check_field(field) |
561 |
if field.key == 'block': |
|
562 |
try: |
|
563 |
field.block # load block |
|
564 |
except KeyError: |
|
565 |
# blockdef not found |
|
566 |
continue |
|
567 |
for _field in field.block.fields: |
|
568 |
if _field.key not in ['item', 'items', 'computed']: |
|
569 |
continue |
|
570 |
_check_field(_field, in_block_field=True, prefix=field.varname or '') |
|
571 |
field._block = None # reset cache |
|
572 | 555 | |
573 | 556 |
# remove duplicated items |
574 | 557 |
return { |
... | ... | |
583 | 566 |
def get_all_fields(self): |
584 | 567 |
return (self.fields or []) + self.workflow.get_backoffice_fields() |
585 | 568 | |
569 |
def iter_fields(self, include_block_fields=False): |
|
570 |
def _iter_fields(fields, block_field=None): |
|
571 |
for field in fields: |
|
572 |
# add contextual_id/contextual_varname attributes |
|
573 |
# they are id/varname for normal fields |
|
574 |
# but in case of blocks they are concatenation of block id/varname + field id/varname |
|
575 |
field.contextual_id = field.id |
|
576 |
field.contextual_varname = None |
|
577 |
if block_field: |
|
578 |
field.block_field = block_field |
|
579 |
field.contextual_id = '%s-%s' % (field.block_field.id, field.id) |
|
580 |
if field.varname and field.block_field.varname: |
|
581 |
field.contextual_varname = '%s_%s' % ( |
|
582 |
field.block_field.varname, |
|
583 |
field.varname, |
|
584 |
) |
|
585 |
else: |
|
586 |
field.contextual_varname = field.varname |
|
587 |
yield field |
|
588 |
if field.key == 'block' and include_block_fields: |
|
589 |
try: |
|
590 |
field.block # load block |
|
591 |
except KeyError: |
|
592 |
# blockdef not found |
|
593 |
continue |
|
594 |
yield from _iter_fields(field.block.fields, block_field=field) |
|
595 |
field._block = None # reset cache |
|
596 | ||
597 |
yield from _iter_fields(self.get_all_fields()) |
|
598 | ||
586 | 599 |
def get_widget_fields(self): |
587 | 600 |
return [field for field in self.fields or [] if isinstance(field, fields.WidgetField)] |
588 | 601 | |
... | ... | |
903 | 916 |
def set_live_condition_sources(self, form, fields): |
904 | 917 |
live_condition_fields = {} |
905 | 918 | |
906 |
def get_all_fields(): |
|
907 |
for field in fields: |
|
908 |
yield field |
|
909 |
if field.key == 'block': |
|
910 |
yield from field.block.fields |
|
911 | ||
912 |
for field in get_all_fields(): |
|
919 |
for field in self.iter_fields(include_block_fields=True): |
|
913 | 920 |
if field.condition: |
914 | 921 |
field.varnames = field.get_condition_varnames(formdef=self) |
915 | 922 |
for varname in field.varnames: |
wcs/forms/backoffice.py | ||
---|---|---|
217 | 217 |
# get_sorted_ids is only implemented in the SQL backend |
218 | 218 |
order_by = None |
219 | 219 | |
220 |
def iter_fields(fields, block_field=None): |
|
221 |
for field in fields: |
|
222 |
field.contextual_id = field.id |
|
223 |
field.contextual_varname = None |
|
224 |
if block_field: |
|
225 |
if field.key == 'items': |
|
226 |
# not yet |
|
227 |
continue |
|
228 |
field.block_field = block_field |
|
229 |
field.contextual_id = '%s-%s' % (field.block_field.id, field.id) |
|
230 |
if field.varname and field.block_field.varname: |
|
231 |
field.contextual_varname = '%s_%s' % ( |
|
232 |
field.block_field.varname, |
|
233 |
field.varname, |
|
234 |
) |
|
235 |
else: |
|
236 |
field.contextual_varname = field.varname |
|
237 |
yield field |
|
238 |
if field.key == 'block': |
|
239 |
yield from iter_fields(field.block.fields, block_field=field) |
|
240 |
continue |
|
241 | ||
242 | 220 |
if order_by and not anonymise: |
243 | 221 |
direction = '' |
244 | 222 |
if order_by.startswith('-'): |
245 | 223 |
order_by = order_by[1:] |
246 | 224 |
direction = '-' |
247 | 225 |
field_found = False |
248 |
for field in iter_fields(self.formdef.get_all_fields()): |
|
226 |
for field in self.formdef.iter_fields(include_block_fields=True): |
|
227 |
if getattr(field, 'block_field', None): |
|
228 |
if field.key == 'items': |
|
229 |
# not yet |
|
230 |
continue |
|
249 | 231 |
if order_by in [field.contextual_varname, 'f%s' % field.contextual_id]: |
250 | 232 |
field_found = True |
251 | 233 |
if field.contextual_varname == order_by: |
252 |
- |