Projet

Général

Profil

0001-fields-use-accessor-to-get-prefill-configuration-678.patch

Frédéric Péters, 09 août 2022 06:54

Télécharger (7,1 ko)

Voir les différences:

Subject: [PATCH 1/2] fields: use accessor to get prefill configuration
 (#67843)

This allows an uniform handling of the unexpected {'type': 'none'}
situation.
 wcs/fields.py       | 12 +++++++++---
 wcs/formdata.py     |  6 +++---
 wcs/forms/common.py |  6 +++---
 wcs/forms/root.py   | 16 +++++++---------
 4 files changed, 22 insertions(+), 18 deletions(-)
wcs/fields.py
486 486
            return None
487 487
        return data.get('%s_structured' % self.id)
488 488

  
489
    def get_prefill_configuration(self):
490
        if self.prefill and self.prefill.get('type') == 'none':
491
            # make sure a 'none' prefill is not considered as a value
492
            self.prefill = None
493
        return self.prefill or {}
494

  
489 495
    def get_prefill_value(self, user=None, force_string=True):
490 496
        # returns a tuple with two items,
491 497
        #  1. value[str], the value that will be used to prefill
......
564 570
        return (None, False)
565 571

  
566 572
    def get_prefill_attributes(self):
567
        if not self.prefill:
573
        if not self.get_prefill_configuration():
568 574
            return
569 575
        t = self.prefill.get('type')
570 576

  
......
747 753
            return str(value)
748 754

  
749 755
    def get_prefill_parameter_view_value(self, widget):
750
        value = getattr(self, 'prefill', None)
751
        if not value or value.get('type') == 'none':
756
        value = self.get_prefill_configuration()
757
        if not value:
752 758
            return
753 759
        r = TemplateIO(html=True)
754 760
        r += htmltext('<ul>')
wcs/formdata.py
517 517
            for field in get_all_fields():
518 518
                if not hasattr(field, 'prefill'):
519 519
                    continue
520
                if field.prefill and field.prefill.get('type') == 'user':
520
                if field.get_prefill_configuration().get('type') == 'user':
521 521
                    block = getattr(field, 'block', None)
522 522
                    if block:
523 523
                        sub_data = self.data.get(block.id)
......
526 526
                        for sub_line_data in sub_data.get('data'):
527 527
                            sub_field_data = sub_line_data.get(field.id)
528 528
                            if sub_field_data:
529
                                form_user_data[field.prefill['value']] = sub_field_data
529
                                form_user_data[field.get_prefill_configuration()['value']] = sub_field_data
530 530
                    else:
531
                        form_user_data[field.prefill['value']] = self.data.get(field.id)
531
                        form_user_data[field.get_prefill_configuration()['value']] = self.data.get(field.id)
532 532
            user_label = ' '.join(
533 533
                [form_user_data.get(x) for x in field_name_values if isinstance(form_user_data.get(x), str)]
534 534
            )
wcs/forms/common.py
843 843
                entry = result[field.id]
844 844
            if field.key == 'comment':
845 845
                entry['content'] = widget.content
846
            elif field.prefill and field.prefill.get('type') == 'string':
847
                if 'request.GET' in (field.prefill.get('value') or ''):
846
            elif field.get_prefill_configuration().get('type') == 'string':
847
                if 'request.GET' in (field.get_prefill_configuration().get('value') or ''):
848 848
                    # Prefilling with a value from request.GET cannot be compatible with
849 849
                    # live updates of prefill values. Skip those. (a "computed data" field
850 850
                    # should be used as replacement).
......
870 870
                        if id_value:
871 871
                            value = id_value
872 872
                    entry['content'] = value
873
            elif field.prefill and field.prefill.get('type') == 'user':
873
            elif field.get_prefill_configuration().get('type') == 'user':
874 874
                update_prefill = bool(get_request().form.get('modified_field_id') == 'user')
875 875
                if update_prefill:
876 876
                    value = field.get_prefill_value(user=formdata.user)[0]
wcs/forms/root.py
415 415
            prefilled = False
416 416
            locked = False
417 417

  
418
            if field.prefill and field.prefill.get('type') == 'none':
419
                field.prefill = {}
420

  
421
            if field.prefill:
418
            if field.get_prefill_configuration():
422 419
                prefill_user = get_request().user
423 420
                if get_request().is_in_backoffice():
424 421
                    prefill_user = get_publisher().substitutions.get_context_variables().get('form_user')
......
441 438
                    # "commited" to data when an "add row" button is clicked
442 439
                    continue
443 440

  
444
            should_prefill = bool(field.prefill)
441
            should_prefill = bool(field.get_prefill_configuration())
445 442

  
446 443
            has_current_value = False
447 444
            if block:
......
469 466
                    should_prefill = False
470 467

  
471 468
            if should_prefill:
472
                if get_request().is_in_backoffice() and (
473
                    field.prefill and field.prefill.get('type') == 'geoloc'
469
                if (
470
                    get_request().is_in_backoffice()
471
                    and field.get_prefill_configuration().get('type') == 'geoloc'
474 472
                ):
475 473
                    # turn off prefilling from geolocation attributes if
476 474
                    # the form is filled from the backoffice
......
632 630
                    widget.attrs['readonly'] = 'readonly'
633 631

  
634 632
        for field, field_key, widget, dummy, dummy in self.iter_with_block_fields(form, displayed_fields):
635
            if field.prefill:
633
            if field.get_prefill_configuration():
636 634
                # always set additional attributes as they will be used for
637 635
                # "live prefill", regardless of existing data.
638 636
                widget.prefill_attributes = field.get_prefill_attributes()
......
1350 1348
        for field, field_key, widget, block, block_idx in self.iter_with_block_fields(
1351 1349
            form, self.formdef.fields
1352 1350
        ):
1353
            if not field.prefill:
1351
            if not field.get_prefill_configuration():
1354 1352
                continue
1355 1353
            post_key = 'f%s' % field_key
1356 1354
            if block:
1357
-