Projet

Général

Profil

0001-forms-redo-date-widget-for-prefill-lock-and-display-.patch

Frédéric Péters, 04 mars 2022 16:28

Télécharger (7,29 ko)

Voir les différences:

Subject: [PATCH] forms: redo date widget for prefill/lock and display of
 readonly values (#58350)

 tests/form_pages/test_all.py                  |  6 +++-
 tests/form_pages/test_live.py                 |  6 +++-
 wcs/forms/common.py                           |  4 ++-
 wcs/qommon/static/js/qommon.forms.js          |  7 +++++
 .../templates/qommon/forms/widgets/date.html  | 31 +++++++++++--------
 5 files changed, 38 insertions(+), 16 deletions(-)
tests/form_pages/test_all.py
5423 5423
    resp = login(get_app(pub), username='foo', password='foo').get('/test/')
5424 5424
    assert resp.form['f0'].value == '2018-09-27'
5425 5425
    assert 'readonly' not in resp.form['f0'].attrs
5426
    assert resp.form['f0'].attrs['type'] == 'date'
5426 5427
    resp.form['f0'].value = '2018-09-27'
5427 5428
    resp = resp.form.submit('submit')
5428 5429
    assert 'Check values then click submit.' in resp.text
......
5433 5434

  
5434 5435
    resp = login(get_app(pub), username='foo', password='foo').get('/test/')
5435 5436
    assert resp.form['f0'].value == '2018-09-27'
5436
    assert 'readonly' in resp.form['f0'].attrs
5437
    # for readonly values there is a <input type=hidden> with the real value
5438
    # then an unnamed <input type=text> with the formatted value.
5439
    assert resp.form['f0'].attrs['type'] == 'hidden'
5440
    assert resp.pyquery('input#form_f0[type=text][readonly=readonly]')
5437 5441

  
5438 5442
    resp.form['f0'].value = '2018-09-24'  # try changing the value
5439 5443
    resp = resp.form.submit('submit')
tests/form_pages/test_live.py
918 918
    assert resp.form['f2'].value == ''
919 919
    resp.form['f1'] = str(carddata1.id)
920 920
    live_resp = app.post('/foo/live?modified_field_id=1&prefilled_2=on', params=resp.form.submit_fields())
921
    assert live_resp.json['result']['2'] == {'visible': True, 'content': '2021-10-01'}
921
    assert live_resp.json['result']['2'] == {
922
        'visible': True,
923
        'content': '2021-10-01',
924
        'text_content': '01/10/2021',
925
    }
922 926

  
923 927
    resp.form['f2'] = '2021-10-30'  # manually changed -> widget-prefilled class will be removed
924 928
    resp.form['f1'] = str(carddata2.id)
wcs/forms/common.py
826 826
                    elif field.key == 'date' and value:
827 827
                        try:
828 828
                            value = field.convert_value_from_anything(value)
829
                            text_content = field.convert_value_to_str(value)
829 830
                            # convert date to Y-m-d as expected by the <input type=date> field
830 831
                            value = field.get_json_value(value)
831 832
                        except ValueError:
832
                            pass
833
                            text_content = None
834
                        entry['text_content'] = text_content
833 835
                    elif field.key == 'item':
834 836
                        for option in field.get_options():
835 837
                            # get raw value from display value
wcs/qommon/static/js/qommon.forms.js
536 536
                if ($(widget).is('.widget-prefilled') || $(widget).is('.widget-readonly') || data.modified_field == 'user') {
537 537
                  // replace text input value
538 538
                  $(widget).find('input[type=text], input[type=tel], input[type=numeric], input[type=email], input[type=date], textarea').val(value.content);
539
                  if ($(widget).is('.DateWidget')) {
540
                    // Set both hidden input for real value, and text input for
541
                    // formatted date. This will also set the old date picker
542
                    // to the formatted value, which is expected.
543
                    $(widget).find('input[type=hidden]').val(value.content);
544
                    $(widget).find('input[type=text]').val(value.text_content);
545
                  }
539 546
                  if ($widget.hasClass('CheckboxWidget')) {
540 547
                    // replace checkbox input value
541 548
                    $widget.find('input[type=checkbox]').prop('checked', value.content);
wcs/qommon/templates/qommon/forms/widgets/date.html
1 1
{% extends "qommon/forms/widget.html" %}
2 2

  
3 3
{% block widget-control %}
4
<input id="form_{{widget.name}}" name="{{widget.name}}"
5
  type="{% if "readonly" in widget.attrs %}text{% else %}date{% endif %}"
6
  {% for attr in widget.attrs.items %}{{attr.0}}="{{attr.1}}" {% endfor %}
7
  {% if widget.required %}aria-required="true"{% endif %}
8
  {% if "readonly" in widget.attrs %}
9
    {% if widget.value %}value="{{ widget.value }}"{% endif %}
10
  {% else %}
11

  
12
  {% if widget.value %}value="{{ widget.value|date:"Y-m-d" }}" data-formatted-value="{{ widget.value|default:"" }}"{% endif %}
4
{% if "readonly" in widget.attrs %}
5
{# for readonly mode, add an hidden field with the correct value and a text #}
6
{# input with the formated date value; this will avoid displaying the date in #}
7
{# Y-m-d format, or including a date input with an ineffective reset × button. #}
8
<input type="hidden" name="{{widget.name}}" {% if widget.value %}value="{{ widget.value|date:"Y-m-d" }}"{% endif %}>
9
<input type="text" {% if widget.value %}value="{{ widget.value|date }}"{% endif %}
10
{% else %}
11
{# otherwise, use an <input type="date"> but also include js datetime picker #}
12
{# attributes, in case it's not supported. #}
13
<input name="{{widget.name}}" {% if widget.value %}value="{{ widget.value|date:"Y-m-d" }}"{% endif %}
14
  type="date"
15
  {% if widget.value %}data-formatted-value="{{ widget.value|default:"" }}"{% endif %}
13 16
  class="date-pick"
14 17
  data-date-format="{{widget.date_format}}"
15 18

  
16
  {# if the date format does not contain the time, set widget not to go #}
17
  {# into the time pages #}
18
  data-min-view="{% if 'hh' in widget.date_format %}0{% else %}2{% endif %}"
19
  {# set widget not to go into the time pages #}
20
  data-min-view="2"
19 21

  
20 22
  {# if there is no value we set the initial view to be the view of decades, #}
21 23
  {# it is more appropriate to select a far away date. #}
......
29 31
  data-end-date="{{ widget.end_date }}"
30 32
  max="{{widget.maximum_date|date:"Y-m-d"}}"
31 33
  {% endif %}
32
  {% endif %}
34
{% endif %}
35
  id="form_{{widget.name}}"
36
  {% for attr in widget.attrs.items %}{{attr.0}}="{{attr.1}}" {% endfor %}
37
  {% if widget.required %}aria-required="true"{% endif %}
33 38
  >
34 39
{% endblock %}
35
-