Projet

Général

Profil

0001-manager-show-user-import-errors-inline-44803.patch

Benjamin Dauvergne, 14 juillet 2020 12:13

Télécharger (5,89 ko)

Voir les différences:

Subject: [PATCH] manager: show user import errors inline (#44803)

 src/authentic2/csv_import.py                  | 11 +++++
 .../manager/user_import_report.html           | 49 ++++++++++++++-----
 tests/test_user_manager.py                    | 14 ++++--
 3 files changed, 58 insertions(+), 16 deletions(-)
src/authentic2/csv_import.py
312 312
    def action_display(self):
313 313
        return self.ACTIONS.get(self.action, self.action)
314 314

  
315
    @property
316
    def has_errors(self):
317
        return self.errors or self.has_cell_errors
318

  
319
    @property
320
    def has_cell_errors(self):
321
        return any(cell.errors for cell in self)
322

  
315 323
    def __iter__(self):
316 324
        return iter(self.cells)
317 325

  
326
    def __len__(self):
327
        return len(self.cells)
328

  
318 329

  
319 330
@attrs
320 331
class CsvCell(object):
src/authentic2/manager/templates/authentic2/manager/user_import_report.html
108 108
          </thead>
109 109
          <tbody>
110 110
            {% for row in importer.rows %}
111
              <tr
112
                class="{% if row.is_valid %}row-valid{% else %}row-invalid{% endif %} row-action-{{ row.action }}"
113
                {% if not row.is_valid %}title="{% for error in row.errors %}{% firstof error.description error.code %}
114
                {% endfor %}{% for cell in row %}{% for error in cell.errors %}
115
{{ cell.header.name }}: {% firstof error.description error.code %}{% endfor %}{% endfor %}"{% endif %}
116
              >
111
              <tr class="{% if row.is_valid %}row-valid{% else %}row-invalid{% endif %} row-action-{{ row.action }}">
117 112
                <td class="row-line">{{ row.line }}</td>
118 113
                {% for cell in row %}
119
                  <td
120
                    class="{% if cell.errors %}cell-errors{% endif %} {% if cell.action %}cell-action-{{ cell.action }}{% endif %}"
121
                    {% if cell.errors %}title="{% for error in cell.errors %}{% firstof error.description error.code %}
122
{% endfor %}"{% endif %}
123
                  >
124
                    {{ cell.value }}
125
                  </td>
114
                  <td class="{% if cell.errors %}cell-errors{% endif %} {% if cell.action %}cell-action-{{ cell.action }}{% endif %}">{{ cell.value }}</td>
126 115
                {% endfor %}
127 116
                <td class="row-action">{% firstof row.action_display "-" %}</td>
128 117
              </tr>
118
              {% if not row.is_valid %}
119
                {% if row.errors %}
120
                  <tr class="row-invalid row-errors">
121
                    <td></td>
122
                    <td colspan="{{ row|length|add:"1" }}">
123
                      <ul>
124
                        {% for error in row.errors %}
125
                          <li>{% firstof error.description error.code %}</li>
126
                        {% endfor %}
127
                      </ul>
128
                    </td>
129
                    <td></td>
130
                  </tr>
131
                {% endif %}
132
                {% if row.has_cell_errors %}
133
                  <tr class="row-invalid row-cells-errors">
134
                    <td></td>
135
                    {% for cell in row %}
136
                      {% if cell.errors %}
137
                        <td class="cell-errors cell-{{ cell.header.name }}">
138
                          <ul>
139
                            {% for error in cell.errors %}
140
                              <li>{% firstof error.description error.code %}</li>
141
                            {% endfor %}
142
                          </ul>
143
                        </td>
144
                      {% else %}
145
                        <td/>
146
                      {% endif %}
147
                    {% endfor %}
148
                    <td></td>
149
                  </tr>
150
                {% endif %}
151
              {% endif %}
129 152
            {% endfor %}
130 153
          </tbody>
131 154
        </table>
tests/test_user_manager.py
415 415

  
416 416
    response = response.click(href=simulate.uuid)
417 417

  
418
    assert len(response.pyquery('table.main tbody tr')) == 3
418
    assert len(response.pyquery('table.main tbody tr')) == 4
419 419
    assert len(response.pyquery('table.main tbody tr.row-valid')) == 2
420
    assert len(response.pyquery('table.main tbody tr.row-invalid')) == 1
420
    assert len(response.pyquery('table.main tbody tr.row-invalid')) == 2
421

  
422
    assert len(response.pyquery('tr.row-errors')) == 0
423
    assert len(response.pyquery('tr.row-cells-errors')) == 1
424
    assert sum(bool(response.pyquery(td).text()) for td in response.pyquery('tr.row-cells-errors td li')) == 2
425
    assert 'Enter a valid email address' in response.pyquery('tr.row-cells-errors td.cell-email li').text()
426
    assert 'Phone number can start with' in response.pyquery('tr.row-cells-errors td.cell-phone li').text()
421 427

  
422 428
    assert User.objects.count() == user_count
423 429

  
......
582 588
    login(app, admin, '/manage/users/')
583 589
    response = import_csv(content, app)
584 590

  
585
    assert len(response.pyquery('table.main tbody tr.row-invalid')) == 1
591
    assert len(response.pyquery('table.main tbody tr.row-invalid')) == 2
592
    assert len(response.pyquery('table.main tbody tr.row-errors')) == 1
593
    assert 'matches too many user' in response.pyquery('tr.row-errors').text()
586 594

  
587 595

  
588 596
def test_manager_create_user_next(superuser_or_admin, app, ou1):
589
-