Projet

Général

Profil

0004-admin-do-not-synchronize-connector-slug-on-title-whe.patch

Nicolas Roche, 08 juin 2022 12:22

Télécharger (3,57 ko)

Voir les différences:

Subject: [PATCH 4/4] admin: do not synchronize connector slug on title when it
 differ (#5778)

 passerelle/forms.py   | 12 +++++++-----
 tests/test_manager.py |  7 ++++++-
 2 files changed, 13 insertions(+), 6 deletions(-)
passerelle/forms.py
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 16

  
17 17
from django import forms
18 18
from django.utils.text import slugify
19 19
from django.utils.translation import ugettext_lazy as _
20 20

  
21 21

  
22 22
class GenericConnectorForm(forms.ModelForm):
23
    def __init__(self, *args, **kwargs):
24
        super().__init__(*args, **kwargs)
25
        # tell JS to prepopulate 'slug' field using the 'title' field
26
        self.fields['title'].widget.attrs['data-slug-sync'] = 'slug'
27

  
28 23
    def save(self, commit=True):
29 24
        if not self.instance.slug:
30 25
            self.instance.slug = slugify(self.instance.title)
31 26
        return super().save(commit=commit)
32 27

  
28
    def get_initial_for_field(self, field, field_name):
29
        if field_name == 'title':
30
            # if title and slug name are in sync, keep them that way
31
            if slugify(self.initial.get('title')) == slugify(self.initial.get('slug')):
32
                field.widget.attrs['data-slug-sync'] = 'slug'
33
        return super().get_initial_for_field(field, field_name)
34

  
33 35

  
34 36
class ResourceLogSearchForm(forms.Form):
35 37
    log_level = forms.ChoiceField(
36 38
        label=_('Log level'),
37 39
        choices=(
38 40
            ('', _('All levels')),
39 41
            ('DEBUG', _('Debug')),
40 42
            ('INFO', _('Info')),
tests/test_manager.py
113 113
    resp.forms[0]['service_url'] = 'https://example.net/'
114 114
    resp = resp.forms[0].submit()
115 115
    assert resp.status_int == 302
116 116
    assert resp.location.endswith('/photon/test-connector/')
117 117
    resp = resp.follow()
118 118
    assert 'Connector updated' in resp.text
119 119

  
120 120
    resp = app.get('/manage/photon/test-connector/edit', status=200)
121
    open('/var/www/html/index.html', 'w').write(resp.text)
122 121
    resp.forms[0]['slug'] = 'test'
123 122
    resp = resp.forms[0].submit()
124 123
    assert resp.status_int == 302
125 124
    assert resp.location.endswith('/photon/test/')
126 125

  
127 126

  
128 127
def test_edit_connector_unique_slug(app, admin_user):
129 128
    Photon.objects.create(
......
143 142
    assert 'Test Connector' in resp.text
144 143
    assert resp.html.find('input', {'name': 'title'}).attrs['data-slug-sync'] == 'slug'
145 144

  
146 145
    resp.forms[0]['slug'] = 'other-connector'
147 146
    resp = resp.forms[0].submit()
148 147
    assert resp.status_int == 200
149 148
    assert 'Photon with this Identifier already exists.' in resp.text
150 149

  
150
    # if title and slug name are not in sync, disable prepopulate slug by JS
151
    resp.forms[0]['slug'] = 'another-connector'
152
    resp = resp.forms[0].submit()
153
    resp = app.get('/manage/photon/another-connector/edit', status=200)
154
    assert 'data-slug-sync' not in resp.html.find('input', {'name': 'title'}).attrs
155

  
151 156

  
152 157
def test_visit_connectors(app, admin_user):
153 158
    app = login(app)
154 159
    resp = app.get('/manage/', status=200)
155 160
    resp = resp.click('Add Connector')
156 161
    for link in re.findall('href="(/manage.*add)"', resp.text):
157 162
        resp = app.get(link, status=200)
158 163

  
159
-