Projet

Général

Profil

0001-admin-autocomplete-slug-on-creation-view-5778.patch

Nicolas Roche, 08 juin 2022 12:22

Télécharger (3,13 ko)

Voir les différences:

Subject: [PATCH 1/4] admin: autocomplete slug on creation view (#5778)

 passerelle/forms.py   | 5 +++++
 passerelle/views.py   | 5 -----
 tests/test_manager.py | 2 ++
 3 files changed, 7 insertions(+), 5 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

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

  
28 33

  
29 34
class ResourceLogSearchForm(forms.Form):
30 35
    log_level = forms.ChoiceField(
passerelle/views.py
196 196
    exclude_fields = ('users',)  # slug not excluded
197 197

  
198 198
    def form_valid(self, form):
199 199
        with transaction.atomic():
200 200
            response = super().form_valid(form)
201 201
        self.object.availability()
202 202
        return response
203 203

  
204
    def init_stuff(self, request, *args, **kwargs):
205
        super().init_stuff(request, *args, **kwargs)
206
        # tell JS to prepopulate 'slug' field using the 'title' field
207
        self.get_form_class().base_fields['title'].widget.attrs['data-slug-sync'] = 'slug'
208

  
209 204

  
210 205
class GenericEditConnectorView(GenericConnectorMixin, UpdateView):
211 206
    template_name = 'passerelle/manage/service_form.html'
212 207

  
213 208
    def form_valid(self, form):
214 209
        with transaction.atomic():
215 210
            response = super().form_valid(form)
216 211
        self.object.availability()
tests/test_manager.py
75 75
    assert 'Test Connector' in resp.text
76 76

  
77 77

  
78 78
def test_add_connector_unique_slug(app, admin_user):
79 79
    app = login(app)
80 80
    resp = app.get('/manage/', status=200)
81 81
    resp = resp.click('Add Connector')
82 82
    resp = resp.click('Base Adresse Web Service')
83
    assert resp.html.find('input', {'name': 'title'}).attrs['data-slug-sync'] == 'slug'
84

  
83 85
    resp.forms[0]['title'] = 'Test Connector'
84 86
    resp.forms[0]['slug'] = 'test-connector'
85 87
    resp.forms[0]['description'] = 'Connector for a simple test'
86 88
    resp.forms[0]['service_url'] = 'https://api-adresse.data.gouv.fr/'
87 89
    resp1 = resp.forms[0].submit()
88 90
    assert resp1.status_int == 302
89 91

  
90 92
    resp2 = resp.forms[0].submit()
91
-