Project

General

Profile

0001-manager-make-slug-conflict-resolution-simpler-57138.patch

Thomas Noël, 23 Sep 2021 06:57 PM

Download (2.58 KB)

View differences:

Subject: [PATCH] manager: make slug conflict resolution simpler (#57138)

 src/authentic2/manager/forms.py |  6 ++++--
 tests/test_manager.py           | 12 ++++++++++--
 2 files changed, 14 insertions(+), 4 deletions(-)
src/authentic2/manager/forms.py
74 74
            qs = instance.__class__.objects.all()
75 75
            if instance.pk:
76 76
                qs = qs.exclude(pk=instance.pk)
77
            new_slug = instance.slug
77 78
            i = 1
78
            while qs.filter(slug=instance.slug).exists():
79
                instance.slug += str(i)
79
            while qs.filter(slug=new_slug).exists():
80
                new_slug = '%s-%d' % (instance.slug, i)
80 81
                i += 1
82
            instance.slug = new_slug
81 83
        if len(instance.slug) > 256:
82 84
            instance.slug = instance.slug[:252] + hashlib.md5(instance.slug).hexdigest()[:4]
83 85
        return super().save(commit=commit)
tests/test_manager.py
71 71
    assert 'Old OU' in response
72 72
    assert 'New OU' in response
73 73
    assert OU.objects.get(name='Old OU').slug == 'new-ou'
74
    assert OU.objects.get(name='New OU').slug == 'new-ou1'
74
    assert OU.objects.get(name='New OU').slug == 'new-ou-1'
75 75
    assert OU.objects.count() == 3
76 76

  
77 77

  
......
100 100
    assert 'New role' in role_list
101 101
    assert 'Old role' in role_list
102 102
    assert non_admin_roles.count() == 2
103
    assert non_admin_roles.get(name='New role').slug == 'new-role1'
103
    assert non_admin_roles.get(name='New role').slug == 'new-role-1'
104 104
    assert non_admin_roles.get(name='Old role').slug == 'new-role'
105 105

  
106
    assert non_admin_roles.filter(name='New role').update(name='New role 0')
107
    role3_add = app.get(reverse('a2-manager-role-add'))
108
    form = role3_add.form
109
    form.set('name', 'New role')
110
    form.submit().follow()
111
    assert non_admin_roles.count() == 3
112
    assert non_admin_roles.get(name='New role').slug == 'new-role-2'
113

  
106 114
    # Test multi-ou form
107 115
    OU.objects.create(name='New OU', slug='new-ou')
108 116
    ou_add = app.get(reverse('a2-manager-role-add'))
109
-